vba - VBA 中的类型 13 数据不匹配错误
问题描述
下面是我为在 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
推荐阅读
- amazon-web-services - 在 lambda 授权方返回错误的请求响应。可能吗?
- php - 带有 OBDC 连接的 PHP 查询返回带有无法识别符号的字符串
- r - 连接到 R 中的 redis 集群
- c# - MediaElement 不在 WPF 中播放在线 .mp3 文件
- json - Jackson json 反序列化为 Seq[Double] 但得到整数而不是双精度
- azure-devops - Azure DevOps CD 中未收到/未收到预部署批准电子邮件
- r - 在R中的列之间提取唯一值
- amazon-web-services - 如何允许 cognito 用户访问他们自己的 S3 文件夹
- ios - 如何设置 Firebase iOS 身份验证电子邮件
- android - 如何测试协程正确?