选择案例的运行时错误 9

Posted

技术标签:

【中文标题】选择案例的运行时错误 9【英文标题】:Run Time Error 9 with Select Case 【发布时间】:2016-07-13 19:27:57 【问题描述】:

以下脚本有时会起作用,但通常会在标题中产生错误消息。我已经在对象和变量上设置了监视,当错误发生时,它们似乎都已定义和/或具有正确的值,因此令人困惑。有什么建议么?

Public Sub PopulateDepositDetails()

    Set BT = Workbooks("US98 1650 Backup Template.xlsx")

    Set DD = BT.Sheets("Deposit Details")
    Set RDI = BT.Sheets("Raw Database Info")
    LastRowRDI = RDI.Cells(Rows.Count, 1).End(xlUp).Row

    'Define Trading Partner(Company Number) Range
    Dim x, ColX As Range, nrDD, rownum As Integer
    Set ColX = RDI.Range(Cells(4, 24).Address, Cells(LastRowRDI, 24).Address)

    nrDD = 2    ' sets Deposit Details sheet next row

    Dim COName As String

    COName = InputBox(Prompt:="Enter Company to Process", _
                      Title:="ENTER COMPANY")

    If COName = vbNullString Then
        Exit Sub

    Else

        Select Case COName

            Case "US96"

                For Each x In ColX
                    If x = "US96" And x.Offset(0, 4) = "YES" Then
                        nrDD = nrDD + 1
                        x.EntireRow.Copy Destination:=Sheets("Deposit Details").Range("A" & nrDD)
                    End If
                Next

            Case "US97"
                For Each x In ColX
                    If x = "US97" And x.Offset(0, 4) = "YES" Then
                        nrDD = nrDD + 1
                        x.EntireRow.Copy Destination:=Sheets("Deposit Details").Range("A" & nrDD)
                    End If
                Next

            Case "US98"
                For Each x In ColX
                    If x = "US98" And x.Offset(0, 4) = "YES" Then
                        nrDD = nrDD + 1
                        x.EntireRow.Copy Destination:=Sheets("Deposit Details").Range("A" & nrDD)
                    End If
                Next

            Case "US99"
                For Each x In ColX
                    If x = "US99" And x.Offset(0, 4) = "YES" Then
                        nrDD = nrDD + 1
                        x.EntireRow.Copy Destination:=Sheets("Deposit Details").Range("A" & nrDD)
                    End If
                Next

            Case "USZ0"
                For Each x In ColX
                    If x = "USZ0" And x.Offset(0, 4) = "YES" Then
                        nrDD = nrDD + 1
                        x.EntireRow.Copy Destination:=Sheets("Deposit Details").Range("A" & nrDD)
                    End If
                Next

        End Select

    End If

End Sub

【问题讨论】:

当错误发生在哪一行停止? x.EntireRow.Copy Destination:=Sheets("存款详情").Range("A" & nrDD) 【参考方案1】:

Sheets("Deposit Details")Workbooks("US98 1650 Backup Template.xlsx") 中使用你已经设置的DD 变量来引用Workbooks("US98 1650 Backup Template.xlsx").Sheets("Deposit Details")

Public Sub PopulateDepositDetails()

    Set BT = Workbooks("US98 1650 Backup Template.xlsx")

    Set DD = BT.Sheets("Deposit Details")
    Set RDI = BT.Sheets("Raw Database Info")
    LastRowRDI = RDI.Cells(Rows.Count, 1).End(xlUp).Row

    'Define Trading Partner(Company Number) Range
    Dim x, ColX As Range, nrDD, rownum As Integer

    Set ColX = RDI.Range(RDI.Cells(4, 24), RDI.Cells(LastRowRDI, 24))

    nrDD = 2    ' sets Deposit Details sheet next row

    Dim COName As String

    COName = Application.InputBox(Prompt:="Enter Company to Process", _
                                  Title:="ENTER COMPANY", Type:=2)

    If COName = "US96" Or COName = "US97" Or COName = "US98" Or COName = "US99" Or COName = "USZ0" Then

        For Each x In ColX
            If x = COName And x.Offset(0, 4) = "YES" Then
                nrDD = nrDD + 1
                x.EntireRow.Copy Destination:=DD.Range("A" & nrDD)
            End If
        Next

    End If

End Sub

Application.InputBox 与 Type:=2 将始终返回一个字符串。如果用户取消输入框,它将返回一个空字符串。

【讨论】:

感谢 Thomas,但我尝试使用“BT.Range”代替较长的脚本,但出现运行时错误 438:对象不支持此属性或方法。 我意识到它应该是:x.EntireRow.Copy Destination:=DD.Range("A" & nrDD) 没错。抱歉,我自己使用了错误的参考。 我已经使用“DD”工作表对象对其进行了测试,现在它每次都能正常工作。感谢您的回答 Thomas,因为它使我找到了正确的方法。

以上是关于选择案例的运行时错误 9的主要内容,如果未能解决你的问题,请参考以下文章

使用celery4.2.0,redis3.0.1 运行时一直报这样的错误,官网给的案例,请问怎么解决?

使用 Ruby 1.9.3 运行“捆绑安装”时出错

第 1034 行:字符 9:运行时错误:引用绑定到类型为“int”的空指针 (stl_vector.h)

excel vba 运行时错误

C++ 异常处理

excel运行VBA,出现1004错误