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
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
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
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.
I have a search box that auto opens when the file is started requesting the target value. I have tried many times to write something that will hide all rows above and below the value once found, with no avail.
Private Sub Summary_Click()
Dim EMPLID As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data").Range("E:E")
Set Rng = .Find(What:=EMPLID, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
Unload Me
End Sub
I would like to search for EMPLID 12345, return only that row (including the header on Row 1, with all other rows hidden.
Add an autofilter.
Private Sub Summary_Click()
Dim EMPLID As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data").Range("E:E")
Set Rng = .Find(What:=EMPLID, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
.autofilter
.autofilter field:=1, criteria1:=EMPLID
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
Unload Me
End Sub
just plain use of Autofilter():
Private Sub Summary_Click()
Dim EMPLID As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data").Range("E:E")
.AutoFilter field:=1, Criteria1:=EMPLID
If WorksheetFunction.Subtotal(103, .Cells) = 1 Then ' if only header row filtered -> no match found
MsgBox "Nothing found"
.Parent.AutoFilterMode = False ' remove AutoFilter and show all data
End If
End With
Unload Me
End Sub
BTW I'd suggest you some little enhancements:
limit the searching range to the actual data extensions, instead of the whole column (some 1 million row)
Don't use Unload Me inside a UserForm code. Adopt Hide.Me and move Unload Me to the Userform calling sub (the one where you place some With New MyUserform statement or the likes)
like follows:
Private Sub Summary_Click()
Dim EMPLID As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data")
With .Range("E1", .Cells(.Rows.Count, "E").End(xlUp))
.AutoFilter field:=1, Criteria1:=EMPLID
If WorksheetFunction.Subtotal(103, .Cells) = 1 Then ' if only header row filtered -> no match found
MsgBox "Nothing found"
.Parent.AutoFilterMode = False ' remove AutoFilter and show all data
End If
End With
End With
Me.Hide
End Sub
I like the autofilter answer just posted. But a more-literal answer that actually hides the rows, except row 1 and the one where 'Rng' is, goes like this:
Sub tst()
Dim rng As Range, bottom As Range
Set rng = [D3] ' Just example data
rng.Activate ' put cursor on rng
' Assumes Column A has data, otherwise use column with Rng in it
Set bottom = Range("A" & Rows.Count).End(xlUp) ' finds last row in A with any data in it
If rng.Row > 2 Then Range(Rows(2), Rows(rng.Row - 1)).Hidden = True ' Hide all rows above RNG
If rng.Row < bottom.Row Then Range(Rows(rng.Row + 1), Rows(bottom.Row)).Hidden = True ' Hide rows below
End Sub
Another simple way to accomplish your task.
Private Sub Summary_Click()
Dim EMPLID As String, cl As Range
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data")
For Each cl In .Range("E2", .Range("E" & .Rows.Count).End(xlUp))
If Not cl.Value = EMPLID Then
cl.EntireRow.Hidden = True
End If
Next cl
End With
End Sub
Try
Sub test()
Dim EMPLID As String
Dim rngDB As Range, Rng As Range, rngU As Range
Dim Ws As Worksheet
Dim strAddress As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
Set Ws = Sheets("Tracking Data")
With Ws
Set rngDB = .Range("e1", .Range("e" & Rows.Count).End(xlUp))
End With
With rngDB
.EntireRow.Hidden = False
Set Rng = .Find(What:=EMPLID, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
strAddress = Rng.Address
Do
If rngU Is Nothing Then
Set rngU = Rng
Else
Set rngU = Union(rngU, Rng)
End If
Set Rng = .FindNext(Rng)
Loop While Rng.Address <> strAddress
End If
End With
If rngU Is Nothing Then
MsgBox "Nothing found"
Else
rngDB.EntireRow.Hidden = True
rngU.EntireRow.Hidden = False
End If
End Sub
Find vs Parent
To not cramp your style, I've only removed the arguments SearchDirection and MatchCase because they were using default parameters and I've added the 'Parent' part which is referring to the worksheet (Tracking Data).
Private Sub Summary_Click()
Dim EMPLID As String
EMPLID = Application.InputBox("Enter Your Employee Number", "Employee Number")
With Sheets("Tracking Data").Range("E:E")
Set rng = .Find(What:=EMPLID, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows)
If Not rng Is Nothing Then
With .Parent
.Cells(2, 1).Resize(rng.Row - 2).EntireRow.Hidden = True
.Cells(rng.Row + 1, 1).Resize(.Rows.Count - rng.Row) _
.EntireRow.Hidden = True
End With
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
Unload Me
End Sub
I have created a userform and one of the commandbuttons launches another userform in which data can be entered into. This data is then added to a table in a worksheet, the userform is then unloaded and the user is returned to the original userform. The error occurs when the data is meant to be entered into the worksheet. This userform works perfectly on its own, but when it is launched from the first userform, this is when the error occurs.
Private Sub CommandButton1_Click()
'check all fields are filled
Dim nextRow As Integer
Dim nextCell As String
If Len(Trim(ComboBox1.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
If Len(Trim(TextBox1.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
If Len(Trim(TextBox2.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
'Check if supplier ID already exists
Dim FindString As String
Dim Rng As Range
FindString = TextBox1.Value
If Trim(FindString) <> "" Then
With Sheet4.Range("B: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
Application.Goto Rng, False
MsgBox "Sorry Bro, " & FindString & " already exists!"
Exit Sub
Else
FindString = TextBox2
If Trim(FindString) <> "" Then
With Sheet4.Range("D:D")
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
Application.Goto Rng, False
MsgBox "Sorry Bro, the Ordering Details you entered:" & vbNewLine & _
"'" & FindString & "'" & vbNewLine & _
"Already exists in our Database!" & vbNewLine & _
"U wanna check ur data?"
Exit Sub
End If
End With
End If
End If
End With
End If
'enter supplier ID into sheet
Sheet4.Activate
nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count
nextCell = Cells(nextRow + 2, 2).Activate
'this is where the error occurs
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Value = ComboBox1.Value
ActiveCell.Offset(0, 2).Value = TextBox2.Value
Sheet2.Activate
Unload Me
End Sub
I'm not sure why it doesn't work because personally I avoid the use of "Activate". Maybe you can try if this works:
'Previous code that worked fine
nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count
With ActiveSheet.Cells(nextRow + 2, 2)
.Value = TextBox1.Value
.Offset(0, 1).Value = ComboBox1.Value
.Offset(0, 2).Value = TextBox2.Value
End With
Sheet2.Activate
Unload Me
End Sub
Hope this does the job! (Note that this is my first answer so I'm very open to feedback)