Excel 下面的粗体代码有什么问题。我应该使用什么代码向范围添加未知值
如何修复此代码“rFound.Offset(0,2)。Value=rFound.Offset(0,2)。Value+1”。我正在尝试扫描多个项目的库存。从一张纸上抓取数据并放在另一张纸上,但如果我多次扫描同一条形码。我得到一个错误代码91。下面是完整代码的副本Excel 下面的粗体代码有什么问题。我应该使用什么代码向范围添加未知值,excel,vba,offset,Excel,Vba,Offset,如何修复此代码“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 strPric
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
rFound
在当时什么都不是。Find
没有返回匹配项。这将有助于准确描述用户正在做什么-他们扫描到哪里?到固定范围还是?准确地说,扫描后会发生什么?嗨,我将扫描我们位于sheet2的仓库的库存。扫描后的信息应填写在表1上。这种情况确实会发生,但如果条形码被扫描两次,即当我得到“错误代码91”时。我尝试过使用公式,但是只有数量字段填充,而不是描述或条形码。rFound应该是扫描过的任何项目。下面的代码显示。rFound set set rFound=Columns(1)。Find(What:=Item,After:=Cells(1,1)uu,LookIn:=xlValues,LookAt:=xlWhole,SearchOrder:=xlByRows u,SearchDirection:=xlNext,MatchCase:=False,SearchFormat:=False)Sheet1设置为A列为条形码,B列为说明,C列为数量。表格2设置为A列为条形码,B列为说明,C列为项目#。