0

Sometimes the below script works, but generally it yields the error message in the title. I have set watches on the objects and variables, and they all appear to be defined and/or have proper values when the error occurs, so it is baffling. Any suggestions?

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
Community
  • 1
  • 1
Brian
  • 1
  • 1

1 Answers1

0

Sheets("Deposit Details") is in Workbooks("US98 1650 Backup Template.xlsx") use the DD variable to you already set to refer to 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 with Type:=2 will always return a string. If the user cancels the inputbox it will return an empty string.

  • Thanks Thomas, but I tried using "BT.Range" in place of the longer script, but I got Run-Time Error 438: Object doesn't support this property or method. – Brian Jul 13 '16 at 20:00
  • I realized it should be: x.EntireRow.Copy Destination:=DD.Range("A" & nrDD) – Brian Jul 13 '16 at 20:03
  • That's right. Sorry I used the wrong reference myself. –  Jul 13 '16 at 20:06
  • I've tested it with using the "DD" worksheet object and it now works everytime. Thanks for your answer Thomas since it lead me to the proper approach. – Brian Jul 13 '16 at 20:07