运行时错误'13' - 类型不匹配执行查找

时间:2018-02-01 16:19:27

标签: excel vba range

我为工作表上的一些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

1 个答案:

答案 0 :(得分:0)

在我看来,你应该只在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