delete row function - excel

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

Related

Using VBA in Userform, I have searched and found data, made changes, and now want to save the updated data back to the worksheet

I have VBA for a Userform that searches a worksheet, finds data via a combobox, and populates the controls. How do I save any updates to that data back to the worksheet?
Also, my checkboxes aren't working correctly. They don't show what was previously checked or not checked. How do I fix that?
I appreciate any light someone can shed on this. Thank you in advance.
Here is the code I have so far:
Private Sub UserForm_Initialize()
'PURPOSE: Populate Combox with data from Excel Table
Dim Rng As Range
Dim WksWorksheet As Worksheet
Set WksWorksheet = Worksheets("DataSheet")
Set Rng = WksWorksheet.Range("HOH_Name_List")
CB_HOH_Name.List = Rng.Value
End Sub
'sets both values from selected line of combobox into text boxes for search
Private Sub CB_HOH_Name_Change()
HOH_FirstName.Text = CB_HOH_Name.Column(0)
HOH_LastName.Text = CB_HOH_Name.Column(1)
End Sub
Private Sub AMI_Enter()
AMI.Value = Format(((Val(HIncome.Value) / Val(HSize.Value)) / 118200), "#0%")
AMI.SelStart = Len(AMI.Value) - 1
End Sub
Private Sub B_Update_Click()
'I need something here maybe?
Rng.Offset(0, -1).Value = ApplDate.Text
Rng.Offset(0, 0).Value = HOH_FirstName.Text
Rng.Offset(0, 1).Value = HOH_LastName.Text
Rng.Offset(0, 2).Value = HSize.Text
Rng.Offset(0, 3).Value = ApplSource.Text
Rng.Offset(0, 4).Value = ReferPartner.Text
Rng.Offset(0, 5).Value = CkB_LeaseMortgage.Value
Rng.Offset(0, 6).Value = CkB_HOH_ID.Value
Rng.Offset(0, 7).Value = CkB_Adult1_ID.Value
Rng.Offset(0, 8).Value = CkB_Adult2_ID.Value
Rng.Offset(0, 9).Value = CkB_Adult3_ID.Value
Rng.Offset(0, 10).Value = CkB_Adult4_ID.Value
Rng.Offset(0, 11).Value = CkB_HOH_Income.Value
Rng.Offset(0, 12).Value = CkB_Adult1_Income.Value
Rng.Offset(0, 13).Value = CkB_Adult2_Income.Value
Rng.Offset(0, 14).Value = CkB_Adult3_Income.Value
Rng.Offset(0, 15).Value = CkB_Adult4_Income.Value
Rng.Offset(0, 16).Value = OrientationDoneDate.Text
Rng.Offset(0, 17).Value = SubsidyStartDate.Text
Rng.Offset(0, 18).Value = HIncome.Text
Rng.Offset(0, 19).Value = MaxIncome.Text
Rng.Offset(0, 20).Value = AMI.Text
Rng.Offset(0, 21).Value = RentOwn.Text
Rng.Offset(0, 22).Value = LoanNbr.Text
Rng.Offset(0, 23).Value = Staff.Text
Rng.Offset(0, 24).Value = NotesComments.Text
Rng.Offset(0, 25).Value = AddtlServicesRqstd.Text
Rng.Offset(0, 26).Value = AddtlServicesDclnd.Text
End Sub
Private Sub B_Search_Click()
'SEARCH AND DISPLAY - FORM
'Search for matching data from the textboxes
'Declarations.
Dim VarCriteria As Variant
Dim WksTarget As Worksheet
Dim WksWorksheet01 As Worksheet
Dim WksWorksheet02 As Worksheet 'don't need this
Dim RngSearch As Range
Dim RngTarget As Range
Dim RngPin As Range
'Setting variables.
VarCriteria = Array(HOH_FirstName.Text, HOH_LastName.Text)
Set WksWorksheet01 = Worksheets("DataSheet")
Set WksWorksheet02 = Worksheets("Datasheet2") 'don't need this
Set WksTarget = WksWorksheet01
'Checkpoint for the second run (with second worksheet) 'I don't need this
CP_Worksheet_Restart:
'Focusing on WksTarget.
With WksTarget
'Setting RngSearch for the area to be searched in the given worksheet (WksTarget).
'searching for first name 1st which is in column 2
Set RngSearch = Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp))
'Checking if there are no data that match the criteria.
If Excel.WorksheetFunction.CountIfs(RngSearch, VarCriteria(0), RngSearch.Offset(0, 1),
VarCriteria(1)) = 0 Then
'if no match is found, checks if we are focused on WksWorksheet02 'I don't need this
If WksTargetName = WksWorksheet02.Name Then
'if we are focusing on WksWorksheet02, the code is sent to CP_No_Match_Found
GoTo CP_No_Match_Found
Else
'if we are not focusing on WksWorksheet02, WksTarget is reset and the code is sent back to CP_Worksheet_Restart
Set WksTarget = WksWorksheet02
GoTo CP_Worksheet_Restart
End If
End If
'Setting RngPin as the first cell that matches the first criteria.
Set RngPin = Nothing
Set RngPin = RngSearch.Find(What:=VarCriteria(0), _
After:=RngSearch.Cells(RngSearch.Rows.Count, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'Checking if RngPin has been set.
If Not (RngPin Is Nothing) Then
'Setting RngTarget
Set RngTarget = RngPin
Else
'if RngPin is still nothing, checks if we are focused on WksWorksheet02
If WksTargetName = WksWorksheet02.Name Then
'if we are focusing on WksWorksheet02, the code is sent to CP_No_Match_Found
GoTo CP_No_Match_Found
Else
'if we are not focusing on WksWorksheet02, WksTarget is reset and the code is sent back to CP_Worksheet_Restart
Set WksTarget = Worksheet02
GoTo CP_Worksheet_Restart
End If
End If
'Checkpoint for the next targeted range.
CP_Next_Target:
'Checking if RngTarget and the cell next to it matches both criteria
If RngTarget.Offset(0, 1).Value = VarCriteria(1) Then
'If a match is found, the data are reported and the macro is terminated
ApplDate.Text = RngTarget.Offset(0, -1).Value
HSize.Text = RngTarget.Offset(0, 2).Value
ApplSource.Text = RngTarget.Offset(0, 3).Value
ReferPartner.Text = RngTarget.Offset(0, 4).Value
CkB_LeaseMortgage.Value = RngTarget.Offset(0, 5).Value
CkB_HOH_ID.Value = RngTarget.Offset(0, 6).Value
CkB_Adult1_ID.Value = RngTarget.Offset(0, 7).Value
CkB_Adult2_ID.Value = RngTarget.Offset(0, 8).Value
CkB_Adult3_ID.Value = RngTarget.Offset(0, 9).Value
CkB_Adult4_ID.Value = RngTarget.Offset(0, 10).Value
CkB_HOH_Income.Value = RngTarget.Offset(0, 11).Value
CkB_Adult1_Income.Value = RngTarget.Offset(0, 12).Value
CkB_Adult2_Income.Value = RngTarget.Offset(0, 13).Value
CkB_Adult3_Income.Value = RngTarget.Offset(0, 14).Value
CkB_Adult4_Income.Value = RngTarget.Offset(0, 15).Value
OrientationDoneDate.Text = RngTarget.Offset(0, 16).Value
SubsidyStartDate.Text = RngTarget.Offset(0, 17).Value
HIncome.Text = RngTarget.Offset(0, 18).Value
MaxIncome.Text = RngTarget.Offset(0, 19).Value
AMI.Text = RngTarget.Offset(0, 20).Value
RentOwn.Text = RngTarget.Offset(0, 21).Value
LoanNbr.Text = RngTarget.Offset(0, 22).Value
Staff.Text = RngTarget.Offset(0, 23).Value
NotesComments.Text = RngTarget.Offset(0, 24).Value
AddtlServicesRqstd.Text = RngTarget.Offset(0, 25).Value
AddtlServicesDclnd.Text = RngTarget.Offset(0, 26).Value
Exit Sub
Else
'If no match is found, RngTarget is reset to the next cell that matches the first criteria.
Set RngTarget = RngSearch.Find(What:=VarCriteria(0), _
After:=RngTarget, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If RngTarget has been set back to RngPin and so no match has been found (it could hardly be the case), an error message is displayed and the macro si terminated. Otherwise the code is sent back to CP_Next_Target.
If RngTarget.Address = RngPin.Address Then
MsgBox "No match found for" & vbCrLf & VarCriteria(0) & vbCrLf &
VarCriteria(1), vbCritical + vbOKOnly, "No match found"
Else
GoTo CP_Next_Target
End If
End If
End With
Exit Sub
CP_No_Match_Found:
'An error message is displayed and the macro terminated.
MsgBox "No match found for" & vbCrLf & VarCriteria(0) & vbCrLf & VarCriteria(1),
vbCritical + vbOKOnly, "No match found"
Exit Sub
End Sub
Private Sub B_Clear_Click()
'Clear all the text boxes
ApplDate.Text = ""
HOH_FirstName.Text = ""
HOH_LastName.Text = ""
ApplSource.Text = ""
ReferPartner.Text = ""
Staff.Text = ""
OrientationDoneDate.Text = ""
SubsidyStartDate.Text = ""
HSize.Text = ""
HIncome.Text = ""
MaxIncome.Text = ""
AMI.Text = ""
RentOwn.Text = ""
LoanNbr.Text = ""
'Uncheck CheckBoxes
CkB_LeaseMortgage.Value = False
CkB_HOH_Income.Value = False
CkB_HOH_ID.Value = False
CkB_Adult1_Income.Value = False
CkB_Adult1_ID.Value = False
CkB_Adult2_Income.Value = False
CkB_Adult2_ID.Value = False
CkB_Adult3_Income.Value = False
CkB_Adult3_ID.Value = False
CkB_Adult4_Income.Value = False
CkB_Adult4_ID.Value = False
NotesComments.Text = ""
AddtlServicesRqstd.Text = ""
AddtlServicesDclnd.Text = ""
End Sub
Here is my Userform:
enter image description here
Private Sub B_Update_Click()
Dim VarCriteria As Variant
Dim WksTarget As Worksheet
Dim WksWorksheet As Worksheet
Dim RngSearch As Range
Dim RngTarget As Range
Dim RngPin As Range
'Setting variables.
VarCriteria = Array(HOH_FirstName.Text, HOH_LastName.Text)
Set WksWorksheet = Worksheets("DataSheet")
'Setting WksTarget.
Set WksTarget = WksWorksheet
With WksTarget
'Setting RngSearch for the area to be searched in the given worksheet (WksTarget). 'searching for first name 1st which is in column 2
Set RngSearch = Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp))
'Checking if there are no data that match the criteria.
If Excel.WorksheetFunction.CountIfs(RngSearch, VarCriteria(0), RngSearch.Offset(0, 1), VarCriteria(1)) = 0 Then
'the code is sent to CP_No_Match_Found
GoTo CP_No_Match_Found
End If
'Setting RngPin as the first cell that matches the first criteria.
Set RngPin = Nothing
Set RngPin = RngSearch.Find(What:=VarCriteria(0), _
After:=RngSearch.Cells(RngSearch.Rows.Count, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'Checking if RngPin has been set.
If Not (RngPin Is Nothing) Then
'Setting RngTarget
Set RngTarget = RngPin
Else
'if RngPin is still nothing, the code is sent to CP_No_Match_Found
GoTo CP_No_Match_Found
End If
'Checkpoint for the next targeted range.
CP_Next_Target:
'Checking if RngTarget and the cell next to it matches both criteria
If RngTarget.Offset(0, 1).Value = VarCriteria(1) Then
RngTarget.Offset(0, -1).Value = ApplDate.Text
RngTarget.Offset(0, 0).Value = HOH_FirstName.Text
RngTarget.Offset(0, 1).Value = HOH_LastName.Text
RngTarget.Offset(0, 2).Value = HSize.Text
RngTarget.Offset(0, 3).Value = ApplSource.Text
RngTarget.Offset(0, 4).Value = ReferPartner.Text
RngTarget.Offset(0, 5).Value = CkB_LeaseMortgage.Value
RngTarget.Offset(0, 6).Value = CkB_HOH_ID.Value
RngTarget.Offset(0, 7).Value = CkB_Adult1_ID.Value
RngTarget.Offset(0, 8).Value = CkB_Adult2_ID.Value
RngTarget.Offset(0, 9).Value = CkB_Adult3_ID.Value
RngTarget.Offset(0, 10).Value = CkB_Adult4_ID.Value
RngTarget.Offset(0, 11).Value = CkB_HOH_Income.Value
RngTarget.Offset(0, 12).Value = CkB_Adult1_Income.Value
RngTarget.Offset(0, 13).Value = CkB_Adult2_Income.Value
RngTarget.Offset(0, 14).Value = CkB_Adult3_Income.Value
RngTarget.Offset(0, 15).Value = CkB_Adult4_Income.Value
RngTarget.Offset(0, 16).Value = OrientationDoneDate.Text
RngTarget.Offset(0, 17).Value = SubsidyStartDate.Text
RngTarget.Offset(0, 18).Value = HIncome.Text
RngTarget.Offset(0, 19).Value = MaxIncome.Text
RngTarget.Offset(0, 20).Value = AMI.Text
RngTarget.Offset(0, 21).Value = RentOwn.Text
RngTarget.Offset(0, 22).Value = LoanNbr.Text
RngTarget.Offset(0, 23).Value = Staff.Text
RngTarget.Offset(0, 24).Value = NotesComments.Text
RngTarget.Offset(0, 25).Value = AddtlServicesRqstd.Text
RngTarget.Offset(0, 26).Value = AddtlServicesDclnd.Text
Exit Sub
Else
'If no match is found, RngTarget is reset to the next cell that matches the first criteria.
Set RngTarget = RngSearch.Find(What:=VarCriteria(0), _
After:=RngTarget, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If RngTarget has been set back to RngPin and so no match has been found (it could hardly be the case), an error message is displayed and the macro si terminated. Otherwise the code is sent back to CP_Next_Target.
If RngTarget.Address = RngPin.Address Then
MsgBox "No match found for" & vbCrLf & VarCriteria(0) & vbCrLf & VarCriteria(1), vbCritical + vbOKOnly, "No match found"
Else
GoTo CP_Next_Target
End If
End If
End With
Exit Sub
CP_No_Match_Found:
'An error message is displayed and the macro si terminated.
MsgBox "No match found for" & vbCrLf & VarCriteria(0) & vbCrLf & VarCriteria(1), vbCritical + vbOKOnly, "No match found"
Exit Sub
End Sub

Excel and Visual Basic Barcode In/Out checkout system

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

Worksheet_change shows date as FALSE

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

How to find a string value in a column and return values in all cells from that row?

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.

Counting the total number of search results found with Excel's Find Method

Is it possible to return the total number of matches found using Excel's find method? If so, how would that look or how would I go about counting the total number of search results?
Here's what I have so far that i'd like to build on:
Private Sub btnSearch_Click()
With Sheet1
Set foundCell = .Cells.Find(What:="B-32", After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found in row " & foundCell.Row)
UserForm1.location.Text = Cells(foundCell.Row, 3).Value
UserForm1.office.Value = Cells(foundCell.Row, 2).Value
UserForm1.floor.Value = Cells(foundCell.Row, 1).Value
UserForm1.status.Value = Cells(foundCell.Row, 4).Value
UserForm1.telephone.Value = Cells(foundCell.Row, 5).Value
UserForm1.mobile.Value = Cells(foundCell.Row, 6).Value
UserForm1.owner.Value = Cells(foundCell.Row, 7).Value
UserForm1.notes.Value = Cells(foundCell.Row, 8).Value
Else
MsgBox ("Bingo not found")
End If
End Sub
Counting the total number of search
you could use CountIF()
also, always explicitly qualify range references up to wanted worksheet
finally, mind Mathieu Guindon's piece of advice
as follows:
With Sheet1
Set foundCell = .Cells.Find(What:="B-32", After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found " & WorksheetFunction.CountIf(.Cells, "*B-32*") & " times")
MsgBox ("first ""Bingo"" found in row " & foundCell.Row)
Me.Location.Text = .Cells(foundCell.Row, 3).Value
Me.Office.Value = .Cells(foundCell.Row, 2).Value
Me.Floor.Value = .Cells(foundCell.Row, 1).Value
Me.Status.Value = .Cells(foundCell.Row, 4).Value
Me.telephone.Value = .Cells(foundCell.Row, 5).Value
Me.mobile.Value = .Cells(foundCell.Row, 6).Value
Me.owner.Value = .Cells(foundCell.Row, 7).Value
Me.Notes.Value = .Cells(foundCell.Row, 8).Value
Else
MsgBox ("Bingo not found")
End If
End With
I was thinking:
Option Explicit
'Global Variables
Dim foundCell
Private Sub btnSearch_Click()
Dim Str
Dim FirstAddr As String
Str = "B-32"
With Sheet1
Set foundCell = .Cells.Find(What:=Str, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found in row " & foundCell.Row)
UserForm1.location.Text = Cells(foundCell.Row, 3).Value
UserForm1.office.Value = Cells(foundCell.Row, 2).Value
UserForm1.floor.Value = Cells(foundCell.Row, 1).Value
UserForm1.status.Value = Cells(foundCell.Row, 4).Value
UserForm1.telephone.Value = Cells(foundCell.Row, 5).Value
UserForm1.mobile.Value = Cells(foundCell.Row, 6).Value
UserForm1.owner.Value = Cells(foundCell.Row, 7).Value
UserForm1.notes.Value = Cells(foundCell.Row, 8).Value
FirstAddr = foundCell.Address
Else
MsgBox ("Bingo not found")
End If
Dim i As Integer
Do Until foundCell Is Nothing
Set foundCell = Sheet1.Cells.FindNext(After:=foundCell)
i = i + 1
If foundCell.Address = FirstAddr Then Exit Do
Loop
MsgBox (i)
End Sub

Resources