excel vba find first empty cell in column range - moving down column

Hello Experts,

I have a worksheet that I have set ranges.  My issue is I can not seem to find the code to start at the first cell in range and go down to find the first instance of blank-empty cell in that range and set it as part of the range to copy.

All ranges are in Column A
(First Range to evaluate)
range1 = sheet1.Range("A2:A52")
I need to start at Range("A2") and go down column range and find the first empty cell and use that cell above that as the end of the range to copy .... then I would set my new range to copy.

If "A30" was "" or blank then "A29" would be the end of my new range.

Not to muddy the water too much, but I need to add that I identify my first cell to copy by using a find:
Set xFind_First = sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart) then I want to use the xFind_First.offset(0,1) as the beginning of my new range to copy.

And my xFind_Last would be the first cell in that range that is blank with .offset(0,-1)

How would the range be called if the
xFind_First.offset(0,1) : xFind_Last.offset(0,-1) is my new range to copy??

Then I move onto my 2nd range to evaluate that is also in Column A that will have a blank cell as the end of the range.  I was limited from using an xlup process.

I hope you can assist me!
Michael
mike637Asked:
Who is Participating?
 
Saqib Husain, SyedConnect With a Mentor EngineerCommented:
Now try this

Set xFind_First = sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart).offset(1)

Set xFind_Last = sheet1.Range("A"&xFind_First.row&":A52").Find(What:=" ", lookat:=xlPart).offset(-1)
0
 
tdlewisCommented:
This statement will select from A2 to the last non-empty cell in column A:
    oSheet.Range("A2:A" & oSheet.Range("A" & oSheet.Rows.Count).End(-4162).Row).Select

Just delete to refer to the range or replace the trailing .Select to perform some other action on the range.
0
 
mike637Author Commented:
Greetings,

Not exactly what I am looking for - I think I may have been too ambiguous.

This line finds my first cell in range:
   Set xFind_First = sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart)
(however, I need to offset down 1 cell and call that the first cell in range - I just don't know how to do that)

The 2nd cell in the range needs to be the cell above the first empty cell in range("A2:A52")

Then I need to select this range [(xFind_First).offset(1,0)] to cell above the emptycell and do something with it.

After that - I will use the same code and change the range to Range("A53:A103") and do the same thing.

I will always have empty cells between my ranges of data - they change week to week, that is why I need the code to find that empty cell - go up one and that becomes the end of my range.

After that - I have 5 more blocks of ranges to process. With all ranges residing in column A.

I hope this helps in explaining what I intended - sorry for the confusion -
Michael
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Saqib Husain, SyedEngineerCommented:
Set xFind_First = sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart).offset(1)
0
 
tdlewisCommented:
Try this:
Set xFind_first = Sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart)
With Sheet1.Range(xFind_first, xFind_first).CurrentRegion
    Set xFind_last = Sheet1.Range("A" & .Row + .Rows.Count - 1)
End With
Sheet1.Range(xFind_first, xFind_last).Select

Open in new window

0
 
Saqib Husain, SyedEngineerCommented:
And

set xFind_Last = xFind_First.end(xldown)

Set xRange= Range(xFind_First, xFind_Last)
0
 
mike637Author Commented:
Expert ssaqibh

the xFind_Last is going all the way to end of the sheet data.  How can I get it to go to the first empty cell in the Range("A2:A52").offset(-1).  This will be my xFind_Last cell.
0
 
tdlewisCommented:
What cells does this select?

Set xFind_first = Sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart)
Sheet1.Range(xFind_first, xFind_first).CurrentRegion.Select
0
 
mike637Author Commented:
The cell that it selects is "A1"
0
 
mike637Author Commented:
Thank you -

You make is seem so easy!  You are so smart.

Michael
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.