Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Go to the End of a database in Excel

Status
Not open for further replies.

dyana

Technical User
Mar 26, 2002
27
US
How do you write a VBA code that highlights an entire data range within Excel and names it "datarange"? I need this code to basically start at column "G" through "AF" and move to the bottom of the database. The database size could vary, meaning it will occupy different numbers of rows.

Thanks
dp
 
If you're trying to name the range by creating a named range on a spreadsheet, you don't have to use VBA.

Create a name range name "datarange" (or whatever) and enter this formula, "datarange" will reflect the proper size of your data and it will update automatically.

Code:
=OFFSET(Sheet1!$G$1:$AF$1,0,0,COUNTA(Sheet1!$G:$G)-1)

 
I tried this and it did not work.

Help Please!
 
Hi dyana

You can try this code

Sub nameRange()

Dim myCol As Integer, myRng As Range
Dim myRow As Integer, myRng2 As Range
Dim initialRow As Integer, initialCol As Integer
Dim referRng As String

Set myRng = Sheets("sheet1").Range("G1:AF1")
myCol = Application.CountA(myRng) + 6 'the offset is 6 from the first column

Set myRng2 = Sheets("sheet1").Range("G:G")
myRow = Application.CountA(myRng2)

Sheets("sheet1").Range("G1").Select
initialRow = ActiveCell.Row
initialCol = ActiveCell.Column

ActiveSheet.Range(Cells(initialRow, initialCol), Cells(myRow, myCol)).Select
referRng = "=Sheet1!R" & initialRow & "C" & initialCol & ":R" & myRow & "C" & myCol
ActiveWorkbook.Names.Add Name:="dataRange", RefersToR1C1:=referRng

ActiveSheet.Cells(myRow, myCol).Select

End Sub

Hope this helps.

rgrds
LSTAN
 
LSTAN,

You have really helped me out this week. My first week at Tek-tips has been great because of your support.

I appreciate your assistance.

Dyana
 
You are welcome. That's what this forum for.

rgrds
LSTAN
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top