First Non-Blank Row# between Multiple Columns

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
So I've seen this question a lot, but I haven't found a solution that applies to multiple columns.

I'm looking to obtain the row number that represents the first cell that contains any data, search between a pre-defined range of columns, for instance B:G.

Most of what I find applies to one column only, like this:

Code:
Columns("A").Find("*", Cells(Rows.Count, "A"), xlValues, xlWhole, , xlNext).Address
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Would look something like this:
Code:
Sub Test1()
Dim Rng As Range

Set Rng = Columns("B:G").Find("*", Cells(Rows.Count, "B"), xlValues, xlWhole, , xlNext)
If Not Rng Is Nothing Then Debug.Print Rng.Address
End Sub
 
Last edited:
Upvote 0
Sub Test1()
MsgBox Range("B:G").Find("*", searchdirection:=xlPrevious).Row + 1
End Sub
 
Upvote 0
Would look something like this:
Rich (BB code):
Sub Test1()
Dim Rng As Range

Set Rng = Columns("B:G").Find("*", Cells(Rows.Count, "B"), xlValues, xlWhole, , xlNext)
If Not Rng Is Nothing Then Debug.Print Rng.Address
End Sub

Hey just checking, should this be:
Rich (BB code):
Sub Test1()
Dim Rng As Range

Set Rng = Columns("B:G").Find("*", Cells(Rows.Count, "B:G"), xlValues, xlWhole, , xlNext)
If Not Rng Is Nothing Then Debug.Print Rng.Address
End Sub
 
Upvote 0
Hey just checking, should this be:
Rich (BB code):
Sub Test1()
Dim Rng As Range

Set Rng = Columns("B:G").Find("*", Cells(Rows.Count, "B:G"), xlValues, xlWhole, , xlNext)
If Not Rng Is Nothing Then Debug.Print Rng.Address
End Sub
No, in the find method you need to search after or before a particular cell sometimes. In this case you are trying to go to the very end of your range so that you can find the next used cell.
Rich (BB code):
Cells(Rows.Count, "B")
refers to cell B1048576. There could be an argument made that I really should have used
Rich (BB code):
Cells(Rows.Count, "G")
to use G1048576 because if data is found in any of the row 1048576 and in the columns B:G then it will return that, but I assumed there is very little chance that the last row will contain any data.
 
Upvote 0
Great, I appreciate the feedback. I guess while I have your ear, why use

If Not Rng is Nothing

Is that faster code than saying if Rng = vbNullString? or <> "" or empty(Rng) ? Just curious

and what's with the , , in the .find function? just no parameter provided?
 
Last edited:
Upvote 0
This seems to ring the bell:

Rich (BB code):
Sub Test1()
Dim Rng As Range

Set Rng = Columns("B:G").Find("*", Cells(2, "B:G"), xlValues, xlWhole, , xlNext)
If Not Rng Is Nothing Then Debug.Print Rng.Address
End Sub
 
Upvote 0
Great, I appreciate the feedback. I guess while I have your ear, why use

If Not Rng is Nothing

Is that faster code than saying if Rng = vbNullString? or <> "" or empty(Rng) ? Just curious
Okay, first we are testing to make sure an instance was found. We do this before we apply a .address or .row (or whatever assignment you are trying to make) to ensure we don't throw errors. Rng <> Empty and Rng <> vbNullString will error for the same reasons that Rng.Address would error because it is looking for the value of the cell found (Rng.Value); since Rng doesn't exist, it will cause an error in the code.

and what's with the , , in the .find function? just no parameter provided?
The find method has several optional modifiers, the only required one being what it is you are looking for. There are 2 ways to handle the modifiers if you want to use some but not all of them. You can use commas to hold each position or you can name each modifier. An example of naming each modifier would be
Code:
sRange.Find(what:=lRange.Value,  After:=oSheet.Cells(1, 1), Lookat:=xlWhole, MatchCase:=True)

Crud... any way to omit 1st row???
Code:
Set Rng = Columns("B:G").Find("*", Cells(1, "G"), xlValues, xlWhole, , xlNext)
Essentially you are using the last cell in row 1 in your range and starting from there to find the next used cell.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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