0

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

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
4

1 回答 1

0

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 将始终返回一个字符串。如果用户取消输入框,它将返回一个空字符串。

于 2016-07-13T19:49:59.113 回答