Need code

atf32

Board Regular
Joined
Apr 13, 2011
Messages
157
:confused:Using MS Excel 2007

Need code to go to the first row of a named range. I'm looking for something that I can pass the name of the range and the code will highlight the first row of the targeted range. The range will have three columns and a fluctuating number of rows (may be 5, may be 15). One column is test, the last two are numbers. So whenever I refer to it, I want to highlight the first row.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Re-reading your post, it sounds like you might be wanting a function that you can call instead of code that you place inside your main procedure.

If that's correct, you could do something like this.

Code:
Function ColorTopRow(sRangeName As String)
    With Range(sRangeName)
        .Resize(1).Interior.Color = vbYellow
    End With
End Function

The you call the function with your range name from another procedure like this...
Code:
ColorTopRow "MyRange"
 
Upvote 0
you can try this macro suppose "rv" is the name of the range

Code:
Sub test()
Range("rv").Cells(1, 1).EntireRow.Select
End Sub

but the first row may be the headings if so then change the second line as
Code:
range("rv").cells(2,1).entirerow.select
 
Upvote 0
Is it possible to only include the columns of the range? So if the range it 10 rows, 5 columns, the code will only select the first row and five columns.
 
Upvote 0
Not quite. your code is great, but perhaps I worded the question incorrectly. I want to select the first row of the range (and just the columns of the range).....not highlight the row.
 
Upvote 0
Not quite. your code is great, but perhaps I worded the question incorrectly. I want to select the first row of the range (and just the columns of the range).....not highlight the row.

You could SELECT the top row of cells within the range like this:

Code:
Range("MyRange").Resize(1).Select

...However if you are planning on having your VBA code do anything with those Cells,
then using .Select is inefficient compared to just applying the Method or Properties to the range reference directly.

So please don't do this...
Code:
Range("MyRange").Resize(1).Select
Selection.Copy
Sheets("Sheet2").Select
Range("A2").Select
Selection.PasteSpecial (xlPasteAll)

Instead do this...
Code:
Range("MyRange").Resize(1).Copy _
   Destination:=Sheets("Sheet2").Range("A2")
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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