Code to Grab All Used Rows

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
How do I write code so that my macro selects all used rows? For example, I have to import data into an Excel workbook every morning and the number of rows are never the same. I need to select all used rows (I do this by manually with ctrl + shift + down arrow) and adjust the row height to 60.00. How do I write this in VBA?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Maybe just….

Code:
ActiveSheet.UsedRange.EntireRow.RowHeight = 60

or even
Code:
ActiveSheet.UsedRange.RowHeight = 60
 
Upvote 0
To learn a little about macros, you can activate the macro recorder and perform the steps, the result will be something like this:

Code:
Sub Macro1()
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.RowHeight = 60
End Sub


You can simplify trial and error to get to this:
Code:
Sub Macro2()
    Range("A1", Range("A1").End(xlDown)).Select
    Selection.RowHeight = 60
End Sub

And so:
Code:
Sub Macro3()
    Range("A1", Range("A1").End(xlDown)).RowHeight = 60
End Sub
 
Upvote 0
@Justinian, it should also be pointed out that "ctrl + shift + down arrow" doesn't actually select all the used cells, it selects down to the first blank cell and so if you had a blank cell half way down your column then it would only select until there.
 
Upvote 0
To learn a little about macros, you can activate the macro recorder and perform the steps, the result will be something like this:

Code:
Sub Macro1()
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.RowHeight = 60
End Sub


You can simplify trial and error to get to this:
Code:
Sub Macro2()
    Range("A1", Range("A1").End(xlDown)).Select
    Selection.RowHeight = 60
End Sub

And so:
Code:
Sub Macro3()
    Range("A1", Range("A1").End(xlDown)).RowHeight = 60
End Sub

Ok, that works. Thank you.
 
Last edited:
Upvote 0
How does this differ from this?

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Range(Selection, Selection.End(xlDown)).Select
Selection.RowHeight = 60[/FONT]
 
Upvote 0
Maybe just….

Code:
ActiveSheet.UsedRange.EntireRow.RowHeight = 60

or even
Code:
ActiveSheet.UsedRange.RowHeight = 60

How does that differ from this?

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Range(Selection, Selection.End(xlDown)).Select
Selection.RowHeight = 60[/FONT]
 
Upvote 0
How does that differ from this?

Set up 2 sheets as per the below and run one of the codes I posted on one sheet and the xldown code on the other :biggrin: Obviously select A1 for the xldown code before using the code,



Book1
ABCD
1aaa
2aaa
3aaa
4aaa
5aaa
6aaa
7aaa
8aaa
9aaa
10
11
12
13
14aaa
15aaa
16aaa
17aaa
18aaa
19aaa
20aaa
21aaa
22aaa
23aaa
24aaa
25aaa
Sheet4
 
Last edited:
Upvote 0
Duh, I should have been able to figure that out! Haha.

Thank you all for your help!
 
Upvote 0
Your welcome, there are a few other options but they can wait until another day (unless there is ever a chance that there isn't any data in Row 1, if that can be the case then you might have an issue with UsedRange (also if you find you have any issues with it retaining a memory of the last row) then post back).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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