【发布时间】: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,以防它是单元格中的数据破坏它。但它仍然首先出错。这有意义吗?
-
这是一条重要线索
-
无法重现...