This is my workbook, so I have a code, I'm using a scanner to scan barcodes. When I scan a barcode it adds "1" to the qty(Column c), I also want to record the date in column F, almost everything works fine except it does not type the date, it types "FALSE". I tried with macro+if formula (if cellrange=1,=(now),""). This works but unfortunately I use the workbook in Shared Mode and you cannot use macros in Shared Mode and vba is my last solution.
I am a beginner in VBA.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Item 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 the 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:=xlPart, 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.Offset(0, 5).Value = rFound.Offset(0, 5).Value2 = Now
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:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
On Error GoTo 0
If Not rFound Is Nothing Then
'Writes the Product Name and puts 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
End If
End With
End If
'Enable the Events again:
Application.EnableEvents = True
End Sub
Le:
Private Sub Worksheet_change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("C:C"), Target)
xOffsetColumn = 3
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
Please replace this row:
rFound.Offset(0, 5).Value = rFound.Offset(0, 5).Value2 = Now
with this one:
rFound.Offset(0, 5).Value = Format(Now, "dd-mm-yyyy hh:mm:ss")
Then comment the line of the Worksheet_change:
codetwo Target
and do the same with all rows of Module module
Related
I'm using visual basic to create a checkout system in an excel sheet. The sheet will be filled with information for a project, each of the projects requires that we send out a kit. This excel sheet will allow for a barcode to be scanned, when this happens, it checks for puts an "out" time. When that barcode is scanned again it puts an "in" time. The issue I'm having is that if that barcode is scanned a third time, it will only update the out time.
How do I set it up where it will see that an "in" and "out" time have been recorded and thus go the next blank cell in the row and add the barcode + new "in" or "out" time. Any help would be greatly appreciated!
This is the code I am using.
Code for on the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
Application.EnableEvents = False
Call inout
Application.EnableEvents = True
End If
End Sub
code for the macro
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
barcode = Worksheets("Sheet1").Cells(2, 2)
Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
ActiveSheet.Columns("a:a").Find("").Select
ActiveCell.Value = barcode
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
Else
rownumber = rng.Row
Worksheets("Sheet1").Cells(rownumber, 1).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
End If
Worksheets("Sheet1").Cells(2, 2).Select
End Sub
All this goes in the worksheet code module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
inout 'use of Call is deprecated
End If
End Sub
Sub inout()
Dim barcode As String
Dim rng As Range
Dim newRow As Boolean
barcode = Me.Cells(2, 2)
'find the *last* instance of `barcode` in ColA
Set rng = Me.Columns("A").Find(What:=barcode, after:=Me.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
'figure out if we need to add a new row, or update an existing one
If rng Is Nothing Then
newRow = True 'no matching barcode
Else
'does the last match already have an "in" timestamp?
If Len(rng.Offset(0, 2).Value) > 0 Then newRow = True
End If
If newRow Then
Set rng = Me.Cells(Me.Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
SetTime rng.Offset(0, 1) 'new row, so set "out"
Else
SetTime rng.Offset(0, 2) 'existing row so set "in"
End If
Me.Cells(2, 2).Select
End Sub
'set cell numberformat and set value to current time
Sub SetTime(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub
update: This is what i have so far, its supposed to search if an entry exists and if it has an in time, it throws an error 91 on line 3. i want the code to search for preexisting entries and update the time.
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
barcode = ws.Cells(2, 2).Value
Set rng = ws.Columns("A").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not (rng Is Nothing) Or rng.Offset(0, 2) > 0 Then
'checking out...
Set rng = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
rng.Offset(0, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 1).Value = Date
rng.Offset(0, 2).Clear
ElseIf rng Is Nothing Then
'checking out...
Set rng = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
rng.Offset(0, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 1).Value = Date
Else
'checking in...
rng.Offset(0, 1).ClearContents
rng.Offset(0, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 2).Value = Date
End If
ws.Cells(2, 2) = ""
End Sub
Please review How to avoid using Select in Excel VBA - it will make your code more reliable
Without the Select/Activate:
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
barcode = ws.Cells(2, 2).Value
Set rng = ws.Columns("A").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
'checking out...
Set rng = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
rng.Offset(0, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 1).Value = Date
Else
'checking in...
rng.Offset(0, 1).ClearContents
rng.Offset(0, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 2).Value = Date
End If
ws.Cells(2, 2) = ""
End Sub
My worksheet looks like this
First code, example, cell a2 is "123" when I type somewhere in the sheet (example K2) "123" then text "123" matches to A2 and it adds "1" to the quantity column in this case C2.
Second code: I want, when in Qty Column(C2) some cell is filled with "1" then in Date Column(F), to show the date when Qty row was filled with "1". This code only works if I type manually "1" and not by searching the barcode with the first code.
Private Sub Worksheet_Change(ByVal target As Range)
FirstCode target
SecondCode target
End Sub
Private Sub FirstCode(ByVal target As Range)
Dim Item 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 the 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:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Adds one to the Quantity:
rFound.Offset(0, 2).Value = rFound.Offset(0, 2).Value + 1
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:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
On Error GoTo 0
If Not rFound Is Nothing Then
'Writes the Product Name and puts 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
End If
End With
End If
'Enable the Events again:
Application.EnableEvents = True
End Sub
Private Sub SecondCode(ByVal target As Range)
If Target.Column <> 3 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target.Offset(0, 3)
.Value = Now
.NumberFormat = "DD/MM/YYYY"
End With
End Sub
I'm creating a user form that will ask for a quote number, populate the data after the quote number has been found, and update any information. The macro code I am currently using doesn't exactly work with this new user form.
I managed to get the textboxes to populate with the code below, but now I need it to actually update the cells if I change any text box values.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim strSearch As String
Dim aCell As Range
Dim Sold As String, Soldlr As Long
Set ws = Sheets("Data Entry")
With ws
strSearch = Me.TextBox1.Value
Set aCell = .Columns(2).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Me.TextBox1.Text = aCell.Value
Me.TextBox2.Text = aCell.Offset(, -1).Value
Me.TextBox3.Text = aCell.Offset(, 0).Value
Me.TextBox4.Text = aCell.Offset(, 1).Value
Me.TextBox5.Text = aCell.Offset(, 2).Value
Me.TextBox6.Text = aCell.Offset(, 3).Value
Me.TextBox7.Text = aCell.Offset(, 4).Value
Me.TextBox8.Text = aCell.Offset(, 5).Value
Else
MsgBox "Quote Number " & strSearch & " Not Found. Try Again"
End If
Exit Sub
End With
End Sub
Private Sub CommandButton2_Click()
Dim ws As Worksheet
Dim strSearch As String
Dim aCell As Range
Dim Sold As String, Soldlr As Long
Set ws = Sheets("Data Entry")
With ws
strSearch = Me.TextBox1.Value
Set aCell = .Columns(2).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(, -1).Value = Me.TextBox2.Text
aCell.Offset(, 0).Value = Me.TextBox3.Text
aCell.Offset(, 1).Value = Me.TextBox4.Text
aCell.Offset(, 2).Value = Me.TextBox5.Text
aCell.Offset(, 3).Value = Me.TextBox6.Text
aCell.Offset(, 4).Value = Me.TextBox7.Text
aCell.Offset(, 5).Value = Me.TextBox8.Text
MsgBox "Quote Number " & strSearch & " Has Been Updated"
End If
End With
Exit Sub
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label6_Click()
End Sub
Private Sub Label8_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Once modified, I can easily change the data in the text boxes and update the information.
In essence you're using Offsets so that if your number was found in B10 and you wanted C10 to go in Textbox1 you'd use
Me.Textbox1.Value = aCell.Offset(, 1).value
assuming this code lies behind the form.
My vba code below, how do it faster ? (obs: i have +- 33000 lines of values)
I search codes from products to my company, i need help to do it faster.
Private Sub TextBox1_Enter()
Dim FindString As String
Dim Rng As Range
FindString = TextBox1.Text
If Trim(FindString) <> "" And Len(TextBox1.Text) = 6 Then
With Sheets("CADMAT").Range("B:B") 'searches all of column B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Dim ultimalinha As Object
Set ultimalinha = Plan3.Range("A35565").End(xlUp)
ultimalinha.Offset(1, 0).Value = TextBox1.Text
ultimalinha.Offset(1, 1).Value = TextBox2.Text
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
Else
MsgBox "Produto não existe na tabela!" 'value not found
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
End If
End With
End If
End Sub
Option Explicit
Private Sub TextBox1_Enter()
Application.ScreenUpdating = False
Code here ...
Application.ScreenUpdating = True
End Sub