Discussion Locked

Excel+ VBA - Selecting a range

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How do i refer to a range of cells (row..or column)whose location on the screen (row) and size, varies depending on other variables.

i wish to use this range within a sub procedure....but i always need to refer to that specific data...

7 replies

by jmiller1

Public Sub Select_Range()
Dim a As String
Dim b As String
a = "A1"
b = "B8"
Sheet1.Range(a, b).Select
End Sub

by srobertson

I use this type of thing a lot when I know I have a static start point and need to find the end of a list and complete some actions with it. Below is a sample of code that I use to create a named range for later use. Essentially it go's to the know static cell, tests for data in the cell below and then steps to the last row in the series. To work properly there can be no blank rows.

Sub Set_Transaction_Range()

ThisWorkbook.Sheets("Transaction List").Activate
Range("A4").Select
If ActiveCell.Offset(1, 0).Value <> "" Then
Selection.End(xlDown).Select
Else
ActiveCell.Offset(1, 0).Select
End If

ActiveWorkbook.Names.Add Name:="Transaction_Data", _
RefersTo:="='Transaction List'!$A$3:$F$" & ActiveCell.Row

End Sub

Make sure you include the $ sign when setting the name otherwise the range has tendency to 'move', caused me no end of grief until I worked out what was happening. Naming the range means you can refer back to it later without having to repeat the process.

Hope this helps,

Scott.

This discussion is closed to new replies.