VBA Dynamic String Search

L

Legacy 321674

Guest
Please forgive the length of this request, as I will try to give as much information as possible.

I have a database that consists of three different sheets of information that correlate together. In one of the sheets, is a list of IP addresses that form a path through a network. I need to set up a search function that will go through each row and search for the string in the cell on the "Search" worksheet, input by the user. The issue that I am running into is that the IP addresses are not always in the same row/column for each entry. The user input search string might be in row 2, column H for one entry row in the data base, and in row 4, column O. I have used INDEX(MATCH()) in the past, but the database has always been a static listing in each column (i.e. Part Number, Item number, Vendor, Price, etc) I am curious if there is a function that works best for what I am looking to do... Thus far I have tried creating lastrow and lastcolumn variables for the array of data, but am having issues with nested FOR loops, wherein the variables are setting themselves to the last row or column of data at the outset and thus causing an error during execution. Below I've pasted a portion of the database sheet (TE-Tunnels), I've changed the IP addresses for security sake, but the idea remains the same.

Capture_zpsh4o8itrb.png


An excerpt from my VBA for this portion of the search is as follows:

'''Test if the search field is blank'''
If cellempty3 = False Then

yfill = 9

'''Loop to search for text entered in the Router Hostname field'''
For x = 2 To lastrowLink
'''Test if the search criteria entered matches the current row in the database'''
If Sheets("Link Addresses").Cells(x, "A") = Sheets("Search").Range("A10") Then
Sheets("Search").Cells(xfill, yfill) = Sheets("Link Addresses").Cells(x, 1)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("Link Addresses").Cells(x, 3)
yfill = 3

For v = 3 To lastrowTE
For w = 7 To lastcolTE
If Sheets("TE-Tunnels").Cells(v, w) = Sheets("Link Addresses").Cells(x, 2) Then
For y = 2 To lastrowCID
'''Test if the search criteria entered matches the current row in the database'''
If Sheets("CID").Cells(y, "F") = Sheets("Search").Range("A14") Then
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 1)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 2)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 3)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 4)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 5)
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("CID").Cells(y, 6)
yfill = yfill + 1

'''Loop to search for the Hostname field in the Link Addresses Sheet to cross-reference the sheets'''
For Z = 3 To lastrowTE
'''Test if the "link address" field matches in the Link Addresses Sheet to populate the Hostname and Lb IP information'''
If Sheets("TE-Tunnels").Cells(Z, "D") = Sheets("Search").Cells(xfill, "H") Then
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "F")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "E")
yfill = yfill + 2
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "G") & " / " & Sheets("TE-Tunnels").Cells(Z, "R")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "H") & " / " & Sheets("TE-Tunnels").Cells(Z, "S")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "I") & " / " & Sheets("TE-Tunnels").Cells(Z, "T")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "J") & " / " & Sheets("TE-Tunnels").Cells(Z, "U")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "K") & " / " & Sheets("TE-Tunnels").Cells(Z, "V")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "L") & " / " & Sheets("TE-Tunnels").Cells(Z, "W")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "M") & " / " & Sheets("TE-Tunnels").Cells(Z, "X")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "N") & " / " & Sheets("TE-Tunnels").Cells(Z, "Y")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "O") & " / " & Sheets("TE-Tunnels").Cells(Z, "Z")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "P") & " / " & Sheets("TE-Tunnels").Cells(Z, "AA")
yfill = yfill + 1
Sheets("Search").Cells(xfill, yfill) = Sheets("TE-Tunnels").Cells(Z, "Q") & " / " & Sheets("TE-Tunnels").Cells(Z, "AB")
yfill = 3

End If
Next Z

xfill = xfill + 1
End If
Next y
End If
Next w
Next v

xfill = xfill + 1
End If

Next x

search3 = ""

Else
End If

This current code fails to run and I am fairly certain that this is the least efficient way to do this search. My searches on forums and youtube for what I am looking for have turned up little. I may be searching for the wrong keywords. Any help would be most appreciated. I am not adverse to getting rid of this process completely for something more dynamic.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top