I have some code which works very well when a search is made through a user form and the search criteria is limited to one column (Column A) in a worksheet. This happily populates all details to the corresponding fields in the UserForm
Extract of the code
FIRST SEARCH COMMAND (Workng as required)
Private Sub cmdUnitSearch_Click()
Dim lastrow
Dim myfind As String
lastrow = Sheets("Passenger").Range("A" & Rows.Count).End(xlUp).Row
myfind = txtUnitNumber
For currentrow = 2 To lastrow
If Cells(currentrow, 1).Text = myfind Then
txtUnitNumber.Value = Cells(currentrow, 1).Value
cboUnitStockType.Value = Cells(currentrow, 2).Value
txtUnitClass.Value = Cells(currentrow, 3).Value
THERE ARE ANOTHER 25 PLUS RECORDS
I now wish to search for a unique record which may be stored in a wider range of columns (12 to be precise)
The variable "myfind" searches for a match to a value in textbox "txtUnitCoachSearch"
I have thought about using IF with ElseIF but this means that I need to repeat a large number of lines of code for each ElseIF.
Also, I beleive that I may have the Range in the lastrow variable incorrect as a Runtime Error 1004 appears. I have checked the field names and other input have been typed correctly so I assume that this must be an issue with the Range.
(currentrow, 15) refers to Column "o" in the worksheet
SECOND SEARCH COMAND
Private Sub cmdCoachNumberSearch_Click()
Dim lastrow
Dim myfind As String
lastrow = Sheets("Passenger").Range("O:Z" & Rows.Count).End(xlUp).Row
myfind = txtUnitCoachSearch
For currentrow = 2 To lastrow
If Cells(currentrow, 15).Text = myfind Then
txtUnitNumber.Value = Cells(currentrow, 1).Value
cboUnitStockType.Value = Cells(currentrow, 2).Value
txtUnitClass.Value = Cells(currentrow, 3).Value
THERE ARE ANOTHER 25 PLUS RECORDS
I would appreciate any help
Extract of the code
FIRST SEARCH COMMAND (Workng as required)
Private Sub cmdUnitSearch_Click()
Dim lastrow
Dim myfind As String
lastrow = Sheets("Passenger").Range("A" & Rows.Count).End(xlUp).Row
myfind = txtUnitNumber
For currentrow = 2 To lastrow
If Cells(currentrow, 1).Text = myfind Then
txtUnitNumber.Value = Cells(currentrow, 1).Value
cboUnitStockType.Value = Cells(currentrow, 2).Value
txtUnitClass.Value = Cells(currentrow, 3).Value
THERE ARE ANOTHER 25 PLUS RECORDS
I now wish to search for a unique record which may be stored in a wider range of columns (12 to be precise)
The variable "myfind" searches for a match to a value in textbox "txtUnitCoachSearch"
I have thought about using IF with ElseIF but this means that I need to repeat a large number of lines of code for each ElseIF.
Also, I beleive that I may have the Range in the lastrow variable incorrect as a Runtime Error 1004 appears. I have checked the field names and other input have been typed correctly so I assume that this must be an issue with the Range.
(currentrow, 15) refers to Column "o" in the worksheet
SECOND SEARCH COMAND
Private Sub cmdCoachNumberSearch_Click()
Dim lastrow
Dim myfind As String
lastrow = Sheets("Passenger").Range("O:Z" & Rows.Count).End(xlUp).Row
myfind = txtUnitCoachSearch
For currentrow = 2 To lastrow
If Cells(currentrow, 15).Text = myfind Then
txtUnitNumber.Value = Cells(currentrow, 1).Value
cboUnitStockType.Value = Cells(currentrow, 2).Value
txtUnitClass.Value = Cells(currentrow, 3).Value
THERE ARE ANOTHER 25 PLUS RECORDS
I would appreciate any help