I need assistance finding the next instance of an exact string within a cell.
To be precise, I want to look through a series of headings and find the next instance of a declared variable to obtain the column number, and I want to look through that series of headings to find the next empty cell and save that number, and finally, I’d like to take that first column number, and search from second row until I find the first instance of an empty cell, and save that number into a variable. What I have been doing is this:
With Rows(1) Set found = .Find(what:=Target, After:=.Cells(1, 1)) End With
But it seems that if I accidentally type “s” it will find the first instance of a cell that contains a string that contains the substring “s” (LastName), and not the first cell that contains only “s”.
my fear is that if there are columns with ” ” in them then my program will not function correctly.
Besides that, I sort by a column and when a cell in that column is empty my program pushes it all the way to the bottom of the list and I am trying to delete that empty cell space.
I tried doing Application.WorksheetFunction.Match, HLookup and VLookup and in general the worksheet functions aren’t working for me.
So just to give an example of what I want to do:
I have 10 Columns with headings. I want to find the first instance of a column that contains exactly the string I send into this class. For instance, if the columns are "FirstName | LastName | Name", I want it to return "Name" and not "FirstName". I want to find a column that the user requests as a sort key and verify it's existence I also want to find a column that is empty (last column) Finally, I want to find the last row that has a value in relation to the SortColumn.
If you set the lookat parameter to xlWhole, it will only match the whole contents of the cell, so for example:
With Rows(1) Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole) End With
To check whether a value was found, you can check whether found is nothing.
Dim exists As Boolean If Not found Is Nothing Then exists = True
To locate the first empty cell at the end of a row or column of values, I would use the End property to find the last cell in the row/column containing data, then use Offset to find the next cell:
With Rows(1) Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole) End With Dim emptyCell As Range If Not found Is Nothing Then Dim col As Integer col = found.Column Set emptyCell = Columns(col).End(xlDown) emptyCell.Offset(1, 0).Select End If
However, you can’t use this if there are some empty cells in the middle of your table of values. (eg if you have values in A1,A2,A3, then A4 is blank and you have more values in A5,A6,A7).
You can use a do loop:
headerToFind = "Name" 'or whatever header you're looking for x = 1 'or whatever header row is y = 1 'or whatever first column with header is Do Until Cells(x,y) = "" If Cells(x,y) = headerToFind then MsgBox "The header you are looking for is in row " & x & ", column " & y Exit Sub End If y = y + 1 Loop MsgBox "Header not found"
In place of the message boxes, put whatever code you want to do with what you find. The first MsgBox will execute if and when the header is found (with x being equal to the row number and y being the column number). The second MsgBox will execute if the desired header is not found.