【发布时间】: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)
标签: excel select-case vba