VBA中的类型13数据不匹配错误
下面是我为在excel工作表中搜索注释而编写的代码,注释也包含特殊字符。有人能帮我摆脱类型不匹配错误吗。下面是我粘贴以供参考的代码VBA中的类型13数据不匹配错误,vba,excel,Vba,Excel,下面是我为在excel工作表中搜索注释而编写的代码,注释也包含特殊字符。有人能帮我摆脱类型不匹配错误吗。下面是我粘贴以供参考的代码 Option Explicit Sub Match_ProjCode() Dim CSAT_Comments As Workbook Dim comment As Worksheet Dim matchcomment As Worksheet Dim comment_string As String 'To store the comment Dim C
Option Explicit
Sub Match_ProjCode()
Dim CSAT_Comments As Workbook
Dim comment As Worksheet
Dim matchcomment As Worksheet
Dim comment_string As String 'To store the comment
Dim Column As Integer
Dim Row As Integer
Dim match_Row As Integer
Dim comments_Column_Name As String '
Dim Comments_Column_Value As String
Dim Comments_ProjCode As String 'To store the project code
Dim RangeObj As Range
Set CSAT_Comments = ActiveWorkbook
Set comment = CSAT_Comments.Worksheets("Qualitative Analysis_2018 Cycle") '
Set matchcomment = CSAT_Comments.Worksheets("Consolidated Comments") '
Dim range1 As Range
Dim rng As Range
matchcomment.Range("A2").Select
Set range1 = matchcomment.Range(Selection, Selection.End(xlDown))
For Each rng In range1.SpecialCells(xlCellTypeVisible)
comment_string = rng.Value ' Comment text will be stored
match_Row = rng.Row 'comment row will be stored
With comment
.Activate
Columns("AK:BL").Select
Set RangeObj = Selection.Find(What:=comment_string, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) ' to search for the comment in the comment worksheet
If Not RangeObj Is Nothing Then
.Range(RangeObj.Address).Select 'Select the cell of the searched value
Column = ActiveCell.Column 'Get the column number of the searched value
Row = ActiveCell.Row ' Get the row number of the searched value
comments_Column_Name = Split(Cells(, Column).Address, "$")(1) ' Trim the column name from the cell address
Comments_Column_Value = .Range("" & comments_Column_Name & 1) ' Get the comment heading
Comments_ProjCode = .Range("A" & Row) 'Get the project code
With matchcomment
.Activate
.Range("C" & match_Row) = Comments_Column_Value ' Paste the comment heading name in the match sheet
.Range("D" & match_Row) = Comments_ProjCode 'Paste the project code in the match sheet
End With
Else
End If
End With
Next rng
End Sub
当您不使用
选项Explicit
时,问题就从这里开始。未声明RangeObj
,因此VBA将其“声明”为变量。但是,如果可能,它至少应该是对象
和范围
类型的对象
因此,为了确保代码能够进一步工作,请明确地如下声明RangeObj
:
Dim RangeObj作为范围
要确保每个变量都明确声明,请在模块顶部写入Option Explicit
问题是Find()
的长度限制为255
您可以按如下方式绕过它:
For Each rng In range1.SpecialCells(xlCellTypeVisible)
comment_string = Left(rng.Value, 255) ' <<<<Comment text will be stored up to 255 length
match_Row = rng.Row 'comment row will be stored
With comment
.Activate
Columns("AK:BL").Select
Set RangeObj = Selection.Find(What:=comment_string, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) ' to search for the comment in the comment worksheet
If Not RangeObj Is Nothing Then
If RangeObj.Text = rng.Value Then '<<<< be sure the whole text matches
.Range(RangeObj.Address).Select 'Select the cell of the searched value
Column = ActiveCell.Column 'Get the column number of the searched value
Row = ActiveCell.Row ' Get the row number of the searched value
comments_Column_Name = Split(Cells(, Column).Address, "$")(1) ' Trim the column name from the cell address
Comments_Column_Value = .Range("" & comments_Column_Name & 1) ' Get the comment heading
Comments_ProjCode = .Range("A" & Row) 'Get the project code
With matchcomment
.Activate
.Range("C" & match_Row) = Comments_Column_Value ' Paste the comment heading name in the match sheet
.Range("D" & match_Row) = Comments_ProjCode 'Paste the project code in the match sheet
End With
Else
End If
End If
End With
Next rng
适用于范围1.特殊单元格(xlCellTypeVisible)中的每个rng
comment_string=Left(rng.Value,255)“Dim comment_string作为字符串而不是Variant
?这样行吗?然后Dim RangeObj as Range
。前面我已将其声明为字符串。它为下面的评论值起到了作用-覆盖经验处于良好水平。虽然在项目的不同阶段,体验是不同的——从某种程度上说是满意的,直到项目结束,并且对下面的注释值不起作用——但是/。。这个团队很有天赋,他们工作很努力。我很欣赏这个团队的努力。他们已经工作了很多周末和晚上。团队需要做出一些改变,但几乎没有得到通知。这些团队规模不大,但他们相互协作,以专业精神和良好的态度完成工作。如果您将Dim RangeObj写入范围
?您是否尝试在代码中将Dim RangeObj写入范围
?比如@vityta已经说过这应该有效。此外,如果我创建这些工作表并运行代码,我也无法重现错误,因为它运行得很好。当错误发生时,检查变量的值,并检查错误点的数据是否存在差异您可以在这里查看一下,使其更加稳定和快速。我将RangeObj声明为Range并添加了Option Explicit,但仍然得到了运行时错误。@KarthikPB-这很奇怪-您能否在模块顶部写入Option Explicit
,并尝试声明VBA要求的任何变量,单击Debug>Compile
?非常感谢@DisplayName,现在它工作正常。我没有注意到find功能的长度限制。再次感谢:)