Courtesy to wideboydixon for the below code, and as he's busy with other projects and until he can be able to further assist, I'll appreciate the entire community help.
As you can imagine, I'm working on a self-motivated project and greatly need your help.
The code is able to search multiple sheets (that has their name starting with "Zone_" and return one result at a time.
Kindly assist me modify the script to:
#1 : To be able to search multiple sheets within the workbook irrespective of the sheet name, as some sheets are named "Zone_1" , "a.b.c" , "10.10.10.8", etc, etc
#2 : I'll need the search to display all the searched results found on a give sheet along with the sheet title.
Thanking you in advance....
++++++++++start ++++
Public Sub searchdata()
Dim lastRow As Long
Dim thisCol As Long
Dim thisZone As Long
Dim zoneSheet As Worksheet
Dim foundData As Boolean
Dim failedSheet As Worksheet
Dim matchRow As Variant
' Flag indicating whether we found the data
foundData = False
' Work through all zone sheets with the name " Zone_*"
For thisZone = 1 To 100
' Set the sheet reference
Set zoneSheet = Sheets("Zone_" & CStr(thisZone))
' Find the last row
lastRow = zoneSheet.Cells(zoneSheet.Rows.Count, "A").End(xlUp).Row
' Use MATCH() to find a match
matchRow = Application.Match(Sheet2.Range("A3").Value, zoneSheet.Range(zoneSheet.Cells(2, "A"), zoneSheet.Cells(lastRow, "A")), 0)
' Did we find it?
If Not IsError(matchRow) Then
' Set the flag to say we found data
foundData = True
' Copy the data from columns A:F into Sheet2
For thisCol = 1 To 6
Sheet2.Cells(11, thisCol).Value = zoneSheet.Cells(matchRow + 1, thisCol).Value
Next thisCol
' Stop searching - remove this line if you want to keep searching
Exit For
End If
Next thisZone
' If we didn't find it then add a row to the Failed_search tab
If Not foundData Then
' Set the sheet reference
Set failedSheet = Sheets("Failed_search")
' Find the last row and add 1 to go to the next blank row
lastRow = failedSheet.Cells(failedSheet.Rows.Count, "A").End(xlUp).Row + 1
' Log the date and the search term
failedSheet.Cells(lastRow, "A").Value = Date
failedSheet.Cells(lastRow, "B").Value = Sheet2.Range("A3").Value
' Clear the results from Sheet2
Sheet2.Range("A11:F11").ClearContents
End If
End Sub
+++++++++
As you can imagine, I'm working on a self-motivated project and greatly need your help.
The code is able to search multiple sheets (that has their name starting with "Zone_" and return one result at a time.
Kindly assist me modify the script to:
#1 : To be able to search multiple sheets within the workbook irrespective of the sheet name, as some sheets are named "Zone_1" , "a.b.c" , "10.10.10.8", etc, etc
#2 : I'll need the search to display all the searched results found on a give sheet along with the sheet title.
Thanking you in advance....
++++++++++start ++++
Public Sub searchdata()
Dim lastRow As Long
Dim thisCol As Long
Dim thisZone As Long
Dim zoneSheet As Worksheet
Dim foundData As Boolean
Dim failedSheet As Worksheet
Dim matchRow As Variant
' Flag indicating whether we found the data
foundData = False
' Work through all zone sheets with the name " Zone_*"
For thisZone = 1 To 100
' Set the sheet reference
Set zoneSheet = Sheets("Zone_" & CStr(thisZone))
' Find the last row
lastRow = zoneSheet.Cells(zoneSheet.Rows.Count, "A").End(xlUp).Row
' Use MATCH() to find a match
matchRow = Application.Match(Sheet2.Range("A3").Value, zoneSheet.Range(zoneSheet.Cells(2, "A"), zoneSheet.Cells(lastRow, "A")), 0)
' Did we find it?
If Not IsError(matchRow) Then
' Set the flag to say we found data
foundData = True
' Copy the data from columns A:F into Sheet2
For thisCol = 1 To 6
Sheet2.Cells(11, thisCol).Value = zoneSheet.Cells(matchRow + 1, thisCol).Value
Next thisCol
' Stop searching - remove this line if you want to keep searching
Exit For
End If
Next thisZone
' If we didn't find it then add a row to the Failed_search tab
If Not foundData Then
' Set the sheet reference
Set failedSheet = Sheets("Failed_search")
' Find the last row and add 1 to go to the next blank row
lastRow = failedSheet.Cells(failedSheet.Rows.Count, "A").End(xlUp).Row + 1
' Log the date and the search term
failedSheet.Cells(lastRow, "A").Value = Date
failedSheet.Cells(lastRow, "B").Value = Sheet2.Range("A3").Value
' Clear the results from Sheet2
Sheet2.Range("A11:F11").ClearContents
End If
End Sub
+++++++++