Selecting all cells with content using a variable

C

Colin Hayes

Hi all

At the end of a macro , I need to select all the cells with content in
the worksheet excluding the first row.

The problem is that the amount of rows and columns to be selected will
vary each time I run the macro.

Sometimes it might need to select for example cells A2:Z270 , or next
time it might need to select cells A2:Y4785 and so on.

Can someone help with some code to select all the cells with content via
variables , whatever the spread , at the end of my macro?

Thanks
 
J

James Ravenswood

How about:

Sub dural()
Dim rSelect As Range, r As Range
Set rSelect = Nothing
For Each r In ActiveSheet.UsedRange
If r.Row = 1 Or IsEmpty(r) Then
Else
If rSelect Is Nothing Then
Set rSelect = r
Else
Set rSelect = Union(r, rSelect)
End If
End If
Next
rSelect.Select
End Sub
 
D

Don Guillett

This will select all without the top row

Sub selectem()
ActiveSheet.UsedRange.Offset(1).Select
'or
'ActiveSheet.UsedRange.Offset(1).clear
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top