Что не так с жирным кодом ниже. Какой код я должен использовать, чтобы добавить неизвестное значение в диапазон - PullRequest
0 голосов
/ 18 октября 2019

Как я могу исправить этот код "rFound.Offset (0, 2) .Value = rFound.Offset (0, 2) .Value + 1" . Я пытаюсь сканировать несколько предметов для инвентаря. Получение данных с одного листа и размещение их на другом, но если я сканирую один и тот же штрих-код более одного раза. Я получаю код ошибки 91. Ниже приведена копия полного кода.

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Item As String
    Dim strDscrpt As String
    Dim strPrice As String
    Dim SearchRange As Range
    Dim rFound As Range

   'Don't run the macro if:
    'Target is not a single cell:
    If Target.Cells.Count > 1 Then Exit Sub
    'or Target belongs to the A1.CurrentRegion:
    If Not Intersect(Target, Range("A1").CurrentRegion) Is Nothing Then Exit Sub

    'Avoid the endless loop:
    Application.EnableEvents = False

    'Looks for matches from here first:
    Set SearchRange = Range("A1:A" & Range("A1").CurrentRegion.Rows.Count)

    Item = Target.Value

    'Clears the Target:
    Target.Value = ""

    If Application.WorksheetFunction.CountIf(SearchRange, Item) > 0 Then
    'There's a match already:
     Set rFound = Columns(1).Find(What:=Item, After:=Cells(1, 1) _
            , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows _
            , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    'Adds one to the Quantity:
        rFound.Offset(0, 2).Value = rFound.Offset(0, 2).Value + 1
        rFound.Activate
          Application.Goto ActiveCell, True
    Else

    'Writes the value for the Barcode-list:
    Range("A" & SearchRange.Rows.Count + 1).Value = Item

    'Looks for the match from sheet "Inventory" column A
    With Sheets("Inventory")
        Set rFound = .Columns(1).Find(What:=Item, After:=.Cells(1, 1) _
                , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

        On Error GoTo 0

            If Not rFound Is Nothing Then
    'Writes the Product Name and adds 1 to the Quantity column:
                Range("B" & SearchRange.Rows.Count + 1).Value = rFound.Offset(0, 1).Value
                Range("C" & SearchRange.Rows.Count + 1).Value = 1

                'Scroll worksheet to the current item in case user want to see it or work with it.
                Range("C" & SearchRange.Rows.Count + 1).Activate
                Application.Goto ActiveCell, True

            Else
                'The Product isn't in the Inventory sheet.
                'sound beep to alert user to add description and price.
                Range("C" & SearchRange.Rows.Count + 1).Value = 1

        'Hack out the delay
                'Beep
                'For i = 1 To 15000000
                    'just killing one second or so, so we can get a second 'beep' in.
                '    Next i
                'Beep
        'End hack out delay

                ' IF user is quick to scan another barcode then the description would be entered as that        barcode.
                ' So we avoid this by checking if the discription entered is a number or if its blank    and loop
                ' until we get the user to enter some text. If a description is actually a number then user should procede the number
                ' with a single quote mark. This ensures that the user really want to enter a number as a description.

        'Hack Out New Product Prompt...Description will be set to the "Need Descripiton"
                'Do
                '    strDscrpt = InputBox("Enter Description for Barcode: " & Range("A" & SearchRange.Rows.Count + 1).Value & vbCr & vbLf & "(24 characters max)", "Item Not found in Inventory List")
                'Loop While IsNumeric(strDscrpt) Or (Len(Trim(strDscrpt)) = 0)

                'Range("B" & SearchRange.Rows.Count + 1).Value = strDscrpt
                strDscrpt = "NEW-Need Description"
                Range("B" & SearchRange.Rows.Count + 1).Value = strDscrpt
        'End Hack out New product prompt

            'Begin Hack add to master
                'With Sheets(?Inventory?)
                'Finds the first empty cell from the bottom up:
                .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2).Value = Array(Item, strDscrpt)
                'End With
            'End hack add to master

        'Hack Out New Product PRICING Prompt...no pricing column updated now
                'Beep
                'Do
                    'strPrice = InputBox("Now enter the regular PRICE for: " & UCase(strDscrpt) & vbCr & vbLf & "(With decimal point. example: 12.99)", "Price for Barcode: " & Range("A" & SearchRange.Rows.Count + 1).Value)
                'Loop While Not IsNumeric(strPrice)

                'Range("D" & SearchRange.Rows.Count + 1).Value = Val(strPrice)

                'Scroll worksheet to the current item in case user want to see it or work with it.
                Range("C" & SearchRange.Rows.Count + 1).Activate
                Application.Goto ActiveCell, True
            End If
    End With
End If

Range("F1").Value = "Scan Barcode Here"
Range("F1").Select

'Enable the Events again:
Application.EnableEvents = True


End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...