VBA 中的类型 13 数据不匹配错误

卡西克 PB

下面是我为在 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 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
显示名称

问题是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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VBA数据类型不匹配错误13

VBA Excel“错误13:类型不匹配”

Excel VBA数据透视缓存类型不匹配运行时错误'13'

VBA中带有变量的动态范围的“运行时错误13”。数据类型不匹配

VBA错误13类型不匹配

Excel VBA运行时错误类型不匹配13

继承上的 VBA 类型不匹配(错误 13),用户来自

Excel VBA运行时错误“ 13”:类型不匹配

无法解决 VBA 数组错误 13 类型不匹配

运行时错误 13' 类型不匹配 VBA excel

VBA Excel中的“运行时错误13:类型不匹配”错误

Excel运行时错误13中的VBA:类型不匹配

类型不匹配错误#13

VBA宏:错误13类型不匹配,单元格上出现错误2023

Access 2010 VBA类型不匹配(错误13)或运行时错误424(必需的对象)

尝试检测是否关闭了用户窗体时在Excel VBA中得到“运行时错误'13':类型不匹配”

更新汇率-VBA-运行时错误13类型不匹配

带有数组的VBA Application.Index导致类型不匹配错误13

Excel VBA Target.Address被修改并导致错误13类型不匹配

Application.Wait和Excel VBA运行时错误'13'类型不匹配

VBA调试打印ParamArray错误13类型不匹配值

VBA Scripting.dictionary运行时错误'13'类型不匹配

QueryTables 中的 VBA 类型不匹配错误

Excel VBA中的类型不匹配错误

从合并单元格中删除时键入不匹配 13 错误 - VBA

运行时错误“13”:宏中的类型不匹配

错误 13 筛选的 Excel 工作表中的类型不匹配

VBA 过滤器函数在从范围创建的数组上引发“运行时错误 13:类型不匹配”错误

Excel VBA-变量类型上的类型不匹配错误(13),但仅在特定情况下