VBA 400错误 – 无法debuggingsearch/复制function

我是新的/学习VBA。 对于这个问题,我没有得到一个debuggingbutton,当我运行我的代码,所以我无法find错误。 当我运行下面的macros时,错误只是说'400',之后没有debugging选项。

Sub Search_and_Copy_Discount_and_Shopper() Dim strStartingCell As String Dim strCustomer As String Dim strDiscount As String Dim strShopper As String 'store starting place 'look at column A, store value strStartingCell = ActiveCell.Address strCustomer = ActiveCell.Offset(0, -1).Value 'go to customer sheet Sheets("Customer").Select Range("A2").Select 'compare values in A to strCustomer 'if it matches, copy cells 1 and 2 to the right in values 'if no match, go down one cell and check again Do While IsEmpty(ActiveCell.Value) = False If ActiveCell.Value = strCustomer Then strDiscount = ActiveCell.Offset(0, 1).Value strShopper = ActiveCell.Offset(0, 2).Value Exit Sub Else ActiveCell.Offset(1, 0).Select End If Loop 'go back to purchases sheet Sheets("Purchases").Select 'copy in current cell and cell 1 to the right Range(strStartingCell).Select 'paste strDiscount in current cell 'paste strShopper in cell 1 to the right ActiveCell.Value = strDiscount ActiveCell.Offset(0, 1).Value = strShopper End Sub 

任何指针或错误,你可以识别将不胜感激!

你有没有尝试添加:

 On Error GoTo error_handler 

在第一行代码和:

 Exit Sub error_handler: MsgBox Err.Description 

就在“End Sub”之前。

你应该testing你的ActiveCell是否在第一列。 Excel会产生一个错误,因为Offset(0, -1)将不存在。

这是一个代码解决scheme:

 Sub Search_and_Copy_Discount_and_Shopper() Dim strStartingCell As String Dim strCustomer As String Dim strDiscount As String Dim strShopper As String 'store starting place 'look at column A, store value If Acticell.Column = 1 Then MsgBox ("You cannot pick a cell on column A") Exit Sub End If strStartingCell = ActiveCell.Address strCustomer = ActiveCell.Offset(0, -1).Value 'end of your code