VBA类型的不匹配错误与范围。值和范围。
我有以下VBA代码,似乎有部分工作?有时它有效,有时不行。我在“如果Len(C.Value)> 30和C.row<> 1”上遇到类型的不匹配错误。任何帮助都会很棒,谢谢。
Sub GSSLength()
Dim c As Range
Dim ws As Worksheet
Dim j As Long
j = 0
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "BOM" Then
For Each c In ws.Range("A2:R10000")
If Len(c.Value) > 30 And c.Row <> 1 Then
ws.Cells(c.Row, c.Column).Interior.ColorIndex = 3
j = j + 1
End If
Next c
End If
Next ws
If j > 0 Then
MsgBox j & " errors found where the length of cell is longer than 30 characthers, please correct in columns that are red."
End If
End Sub
I have the following VBA code and it seems to work partially? Sometimes it works, and sometimes it doesnt. I am getting a type mismatch error on the "If Len(c.Value) > 30 And c.Row <> 1" Then line. Any help would be great, thanks.
Sub GSSLength()
Dim c As Range
Dim ws As Worksheet
Dim j As Long
j = 0
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "BOM" Then
For Each c In ws.Range("A2:R10000")
If Len(c.Value) > 30 And c.Row <> 1 Then
ws.Cells(c.Row, c.Column).Interior.ColorIndex = 3
j = j + 1
End If
Next c
End If
Next ws
If j > 0 Then
MsgBox j & " errors found where the length of cell is longer than 30 characthers, please correct in columns that are red."
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设这不是您的最终代码..?您指定的范围永远不会具有
row = 1
,并且您正在使用“ bom”
在喜欢
语句中似乎是不必要的。也许您只是为了这个问题而将其放在其中。忽略这些奇数,您可能有一个包含错误的单元格。您可以像这样将其捕获:
I'm assuming this isn't your final code..? The range you've specified will never have
Row = 1
and you're using"BOM"
in aLike
statement that seems unnecessary. Maybe you've put this in just for this question.Ignoring those oddities, you've probably got a cell in that range that contains an error. You can trap these out like so:
感谢所有提示,我只是注意到我的单元格中有一个错误格式而不是字符串,而Len函数在那里丢了错误。
Thanks for all the tips, I just noticed that I had an error in my cells where it was a number format instead of a string, and the LEN function was throwing an error there.