生成批次编号时,我的VBA代码中的运行时错误13

问题描述 投票:1回答:1

我的代码中的数据类型有问题。我将6转换为字符串,但我得到了相同的错误。有什么建议吗?

Sub test1()
' Generates batch number based on company code

Dim x As Integer
Dim a As Integer    

a = Cells(Rows.Count, 2).End(xlUp).Row    

For x = 6 To a    
    If Cells(x, 2).Value = "US1K" Then
        Cells(x, 1).Value = 1

    ElseIf Cells(x, 2).Value = "USYB" Then
        Cells(x, 1).Value = 2

    ElseIf Cells(x, 2).Value = "US1A" Then
        Cells(x, 1).Value = 3

    ElseIf Cells(x, 2).Value = "US4U" Then
        Cells(x, 1).Value = 4

    ElseIf Cells(x, 2).Value = "US3D" Then
        Cells(x, 1).Value = 5
 Else
        Cells(x, 1).Value = " "

    End If
   Next x
End Sub
excel vba excel-vba
1个回答
0
投票

很可能其中一个单元格出错。只尝试这一部分,看看你是否得到了MsgBox()。然后,在代码中加入IsError()部分:

Sub TestMe()

    Dim x As Long
    Dim a As Long

    a = Cells(Rows.Count, 2).End(xlUp).Row

    For x = 6 To a
        If IsError(Cells(x, 2)) Then
            MsgBox Cells(x, 2).Address
            Exit Sub
        End If
        'do the rest of your code here
    Next x

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.