【问题标题】:Run-time error '13' - Type Mismatch doing a Find运行时错误 '13' - 执行查找的类型不匹配
【发布时间】:2018-02-01 16:19:26
【问题描述】:

我为工作表上的一些 ActiveX 文本框编写了以下代码,这些文本框从另一个工作表中提取数据并通过几个按钮(上一个和下一个)进行控制。

一切正常,除了我不断收到以下错误:

运行时错误“13”:

类型不匹配

这很奇怪,因为单元格只包含文本并且被格式化为文本,所以我不确定为什么我不断收到类型不匹配。

导致错误的行是:

Set sectionfound = .Find(what:=sectiontextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
Set titlefound = .Find(what:=titletextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
Set controlfound = .Find(what:=controltextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
Set guidancefound = .Find(what:=guidancetextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)

整个子:

Sub UpdateTextBox(shift As Long)

Dim sectionfound As Range
Dim titlefound As Range
Dim controlfound As Range
Dim guidancefound As Range
Dim titlerange As Range
Dim sectionrange As Range
Dim controlrange As Range
Dim guidancerange As Range
Dim commentsrange As Range
Dim titletextbox As OLEObject
Dim sectiontextbox As OLEObject
Dim controltextbox As OLEObject
Dim guidancetextbox As OLEObject
Dim commentstextbox As OLEObject
Dim index As Long

With Worksheets("Tool")
    Set sectiontextbox = .OLEObjects("Section_Textbox")
    Set sectionrange = Worksheets("Annex_A").Range("B3:B165")
    Set titletextbox = .OLEObjects("Title_Textbox")
    Set titlerange = Worksheets("Annex_A").Range("C3:C165")
    Set controltextbox = .OLEObjects("Control_Textbox")
    Set controlrange = Worksheets("Annex_A").Range("D3:D165")
    Set guidancetextbox = .OLEObjects("Guidance_Textbox")
    Set guidancerange = Worksheets("Annex_A").Range("E3:E165")
    Set commentstextbox = .OLEObjects("Comments_Textbox")
    Set commentsrange = Worksheets("Annex_A").Range("F3:F165")
End With

With sectionrange
    If sectiontextbox.Object.Text <> "" Then
        Set sectionfound = .Find(what:=sectiontextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
        If Not sectionfound Is Nothing Then index = sectionfound.Row - .Rows(1).Row + 1
    End If

    index = index + shift
    Select Case index
        Case Is > .Rows.Count
            index = .Rows.Count
        Case Is < 1
            index = 1
    End Select

    sectiontextbox.Object.Text = .Rows(index)
End With

With titlerange
    If titletextbox.Object.Text <> "" Then
        Set titlefound = .Find(what:=titletextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
        If Not titlefound Is Nothing Then index = titlefound.Row - .Rows(1).Row + 1
    End If

    index = index + shift
    Select Case index
        Case Is > .Rows.Count
            index = .Rows.Count
        Case Is < 1
            index = 1
    End Select

    titletextbox.Object.Text = .Rows(index)
End With

With controlrange
    If controltextbox.Object.Text <> "" Then
        Set controlfound = .Find(what:=controltextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
        If Not controlfound Is Nothing Then index = controlfound.Row - .Rows(1).Row + 1
    End If

    index = index + shift
    Select Case index
        Case Is > .Rows.Count
            index = .Rows.Count
        Case Is < 1
            index = 1
    End Select

    controltextbox.Object.Text = .Rows(index)
End With

With guidancerange
    If guidancetextbox.Object.Text <> "" Then
        Set guidancefound = .Find(what:=guidancetextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole)
        If Not guidancefound Is Nothing Then index = guidancefound.Row - .Rows(1).Row + 1
    End If

    index = index + shift
    Select Case index
        Case Is > .Rows.Count
            index = .Rows.Count
        Case Is < 1
            index = 1
    End Select

    guidancetextbox.Object.Text = .Rows(index)
End With

End Sub


Private Sub Next_Button_Click()
UpdateTextBox -1
End Sub

Private Sub Previous_Button_Click()
UpdateTextBox 1
End Sub

【问题讨论】:

  • 你不应该在这些特定的行上得到那个特定的错误,我无法重现它。
  • 您是否将msgbox sectiontextbox.Object.Text 行放在第一个错误行之前,运行它并报告结果?
  • @n8。错误出现在不同的点上。所以第一个出错的是guidetextbox。放入消息框时,它会正确显示数据,直到到达第一个破坏它的单元格(E6)。我什至尝试将“测试”放入 E6,以防它是单元格中的数据破坏它。但它仍然首先出错。这有意义吗?
  • 这是一条重要线索
  • 无法重现...

标签: excel vba range


【解决方案1】:

在我看来,您应该只在 sub 中有此代码一次:

index = index + shift
Select Case index
    Case Is > 165 '.Rows.Count
        index = 165 '.Rows.Count
    Case Is < 1
        index = 1
End Select

在没有看到表单或数据的情况下,多次迭代该行是没有意义的,似乎该行应该每个shift 参数只移动一个。

我会把它放在你的 With Worksheets("Tool") 块之后和你的 With sectionrange 块之前

我的理论是,这些多次迭代导致 sub 尝试查看声明的范围之外,即大于 165 的行。

编辑

编辑 编辑

我添加了一系列消息框,让我们逐步了解会发生什么。

试试这个,告诉我会发生什么?

With guidancerange
        msgbox("guidancetextbox.Object.Text row: """ & guidancetextbox.Object.Text & """") 

    If guidancetextbox.Object.Text <> "" Then
        Index = .Find(what:=guidancetextbox.Object.Text, LookIn:=xlValues, lookat:=xlWhole).Row
        msgbox("Index row: """ & Index & """") 
        msgbox(".Rows(1).Row + 1: """ & .Rows(1).Row + 1 & """") 
    End If

    Index = Index + shift
    msgbox(".Rows.Count: """ & .Rows.Count & """") 
    Select Case Index + shift
        Case Is > .Rows.Count
            Index = .Rows.Count
        Case Is < 1
            Index = 1
    End Select

    msgbox("Index row: """ & Index & """") 
    guidancetextbox.Object.Text = .Rows(Index)
End With

【讨论】:

  • 谢谢你,我喜欢你的理论!但是,除非我将其放在另一个“with”语句中,否则将上述内容放在这两个部分之间不会编译。如果我确实将它放在 with 语句中,则按下按钮时根本不会更改任何文本。再次感谢您的帮助。
  • 由于您的所有范围都以 165 结尾,因此我在重要的地方硬编码了 165,这将允许您的代码运行。 .Rows.Count 需要一个对象来获得最大行数。这严格用于调试。顺便说一句,这些范围会有不同的最大行吗?
  • 使用 165 硬编码,不存在编译器问题,但是按下按钮时文本框数据不会发生变化。我可以确认最大行将始终为 165,因为它是我要显示的一组数据。
  • 您能在您的帖子中描述每个With 块算法的作用吗?我感觉你的目标有一条更简洁的路径。
  • 这个位的具体用途我不清楚:index = sectionfound.Row - .Rows(1).Row + 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多