VBA类型的不匹配错误与范围。值和范围。

发布于 2025-02-13 17:48:44 字数 816 浏览 0 评论 0原文

我有以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糖 2025-02-20 17:48:44

我假设这不是您的最终代码..?您指定的范围永远不会具有row = 1,并且您正在使用“ bom”喜欢语句中似乎是不必要的。也许您只是为了这个问题而将其放在其中。

忽略这些奇数,您可能有一个包含错误的单元格。您可以像这样将其捕获:

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "BOM" Then
        For Each c In ws.Range("A2:R10000")
            
            If Not (IsError(c)) Then

                If Len(c.Value) > 30 And c.Row <> 1 Then
                    ws.Cells(c.Row, c.Column).Interior.ColorIndex = 3
                    j = j + 1
                End If
            End If
         Next c
    End If
Next ws

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 a Like 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:

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "BOM" Then
        For Each c In ws.Range("A2:R10000")
            
            If Not (IsError(c)) Then

                If Len(c.Value) > 30 And c.Row <> 1 Then
                    ws.Cells(c.Row, c.Column).Interior.ColorIndex = 3
                    j = j + 1
                End If
            End If
         Next c
    End If
Next ws
土豪我们做朋友吧 2025-02-20 17:48:44

感谢所有提示,我只是注意到我的单元格中有一个错误格式而不是字符串,而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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文