Ошибка VBA 400 - не удается отладить функцию поиска / копирования - PullRequest
1 голос
/ 09 февраля 2012

Я новичок / учусь на VBA.Для этой проблемы я не получаю кнопку отладки при запуске моего кода, поэтому я не могу найти ошибку.Ошибка просто говорит «400», когда я запускаю макрос ниже, без опции отладки после.

    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

Будем очень благодарны за любые указатели или ошибки, которые вы можете определить!

Ответы [ 2 ]

3 голосов
/ 09 февраля 2012

Вы пробовали добавить:

On Error GoTo error_handler

в первой строке кода и:

Exit Sub

error_handler:
  MsgBox Err.Description

прямо перед "End Sub".

0 голосов
/ 09 февраля 2012

Вы должны проверить, находится ли ваш ActiveCell в первом столбце.В Excel возникает ошибка, поскольку Offset(0, -1) не существует.

Вот кодовое решение:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...