A very basic syntax question about ranges

Joined
May 6, 2011
Messages
25
The VBA line:

Range(Cells(Application.ActiveCell.SpecialCells(xlLastCell).Row, "E").Address).Select

works perfectly, selecting the cell in the "E" column of the last row in the worksheet with data in it.

So why doesn't the following select the range from E2 to that cell?

Range("E2:Cells(Application.ActiveCell.SpecialCells(xlLastCell).Row, "E").Address").Select

When I try it, I get a syntax error. I must be missing something blindingly obvious.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
Range("E2:E" & Cells.SpecialCells(xlLastCell).Row).Select
 
Last edited:
Upvote 0
Seems to me there is a cleaner (more understandable) way to write this, of course always depending on what you are doing.

Code:
Sub SelectCells()
    Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row).Select
End Sub
 
Upvote 0
Thank you for the quick replies, Sektor and Jeffery!

Unfortunately, Sektor's method didn't do the trick. I ran it, and got:

Run-time error '1004':
Method 'Range' of object '_Global' failed


Jeffery, your method didn't return any errors, but it didn't do what I needed, either. My worksheet has some completely blank rows in it (the first of which is row 3), but there's more data below them. For some reason, your method ended up just selecting E1 and E2.

What I'm trying to do is select the E column from E2 to the last row with any data at all (at the moment, that's row 346), which is why I'm straining to use "SpecialCells(xlLastCell)" in the code.
 
Upvote 0
Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Select

(by the way - you usually don't need to select ranges to perform actions on them)
 
Upvote 0
It works for me like you describe.

I have data in E2, E3 is blank, and E4 has data.

Range("E2:E4") is selected.

Run this on column E. What does the msgbox return?

Code:
Sub SelectCells()
    Dim LR As Long
    LR = Range("E" & Rows.Count).End(xlUp).Row
    MsgBox LR
    Range("E2:E" & LR).Select
End Sub
 
Last edited:
Upvote 0
I love how quickly the conversation moves on this board - the challenge is just keeping up. :)

First, let me say that I was able to make Sektor's method work, with just a slight modification - I used "ActiveCell" instead of "Cells," and that did the trick.

Range("E2:E" & ActiveCell.SpecialCells(xlLastCell).Row).Select


This correctly selected E2:E346. :)

Jeffrey, I ran your code, and message box returned "1." When I exited the message box, the cells selected were still E1:E2, instead of E2:E346.

Njimack, your code is sadly giving me a syntax error. The error message that pops up in Visual Basic is:

"Compile error: Expected: list separator or )"

As for not needing to select a range - I appreciate the tip. I'm actually using this as a test; now that it's working, I'm going to plug it into a sort function in a bigger piece of code (without selecting the range).
 
Upvote 0
Sektor's code was edited 5 mins after the original post - which version did you try? Jeff's code assumes your last populated row is also populated in column E - I suspect that is not the case.
 
Upvote 0
That's what I get for typing code straight onto here instead of the VBE.

Range(Range("E2"), Range("E" & Rows.Count).End(xlUp)).Select
 
Upvote 0
Rory,

I originally tried Sektor's pre-edited version. I tried it again with the edited version, and it worked perfectly.

And you're right, the E column is empty in row 346 - only the A column has data.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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