简体   繁体   English

VBA Excel中的类型13不匹配错误

[英]Type 13 mismatch error in VBA excel

Dear friends I'm trying to display photos of the persons as per the name selected in a combo box. 亲爱的朋友,我正在尝试按照组合框中选择的姓名显示人员的照片。 I'm successful in doing that but my problem is that while continuously choosing different names in combo box suddenly at times it displays ** error 13, type mismatch** and my combo box too disappearing. 我这样做很成功,但是我的问题是,有时突然在组合框中连续选择不同的名称时,它会显示**错误13,键入不匹配**,并且我的组合框也消失了。 But after that making the visibility of Mypics(Name defined to the table of person names and pictures) "TRUE" its appearing again after compiling 2 to 3 times. 但是在此之后,将Mypics(名称定义为人的名字和图片的表)的可见性“ TRUE”编译2至3次后再次出现。

here is my code 这是我的代码

Private Sub Worksheet_Calculate()
Dim Mypics As Picture

Me.Pictures.Visible = False

 With Range("B8")

   For Each Mypics In Me.Pictures

    If (Mypics.Name = .Text) Then

      Mypics.Visible = True
      Mypics.Top = .Top
      Mypics.Left = .Left

   Exit For
    End If
  Next Mypics
 End With  
End Sub

The cell "B8" is where the name of the picture appears according to the selected person name in combo box with reference to the Index number. 单元格“ B8”是图片的名称出现的地方,该名称根据参考索引编号的组合框中的所选人物姓名出现。

Often, cleaning up your code can produce wonders. 通常,清理代码会产生奇迹。 I sincerely suggest avoiding using With if you're just aiming to use it once, as in your original code. 如果您仅打算一次使用它,那么我真诚地建议避免使用With ,就像您的原始代码一样。 How about trying the following: 如何尝试以下操作:

Private Sub Worksheet_Calculate()
    Dim Mypics As Picture
    Dim TargetName As String
    TargetName = Range("B8").Text
    Me.Pictures.Visible = False
    For Each Mypics In Me.Pictures
        If Mypics.Name = TargetName Then
            With Mypics
                .Visible = True
                .Top = .Top
                .Left = .Left
            End With
            Exit For
        End If
    Next Mypics
End Sub

Let us know if this works. 让我们知道这是否有效。 Also, try to Dim everything you can properly Dim . 另外,尽量Dim一切你可以适当Dim Often, a type mismatch error is thrown due to a variable being declared wrongly at the beginning of a code. 通常,由于在代码开头错误地声明了变量而引发type mismatch错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM