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