[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-02
10
Medium Priority
?
2,906 Views
Last Modified: 2012-09-02
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
0
Comment
Question by:mike637
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:tdlewis
ID: 38359048
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
 

Author Comment

by:mike637
ID: 38359082
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
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38359106
Set xFind_First = sheet1.Range("A2:A52").Find(What:="MAINLINE", lookat:=xlPart).offset(1)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:tdlewis
ID: 38359107
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
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38359113
And

set xFind_Last = xFind_First.end(xldown)

Set xRange= Range(xFind_First, xFind_Last)
0
 

Author Comment

by:mike637
ID: 38359128
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
 
LVL 10

Expert Comment

by:tdlewis
ID: 38359250
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
 

Author Comment

by:mike637
ID: 38359300
The cell that it selects is "A1"
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 2000 total points
ID: 38359347
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
 

Author Closing Comment

by:mike637
ID: 38359370
Thank you -

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

Michael
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question