Selecting first and last row in a range

Mondi_2000

New Member
Joined
May 4, 2017
Messages
7
Hi Forum,
I am trying to select a range of rows in a spreadsheet, via a macro.
My macro button is in the London 2017 cell.
Once I click on the button I'd like the macro to select Rows 2 to 5
Rows 2-5 are empty so I'm sure the code will include Select XLDown but that selects Row 6
I would like to use the same code to be able to select Rows 7 to 8 etc etc
I've looked at so much code but I've not found anything to help me.. I'm not trained in VBA so simple is best :rolleyes:
Thank you in advance
Raymond


1 London20172018
21020
31020
41020
51020
6 New York20172018
71020
81020
9 Sydney20172018

<tbody>
</tbody>
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
What are you going to do with the range(s) you are selecting?
 
Upvote 0
Right-click the sheet tab, select View Code, paste this code in. It works by double-clicking "London" or "New York" or "Sydney" etc.
No buttons necessary.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Value = "" Then Exit Sub
lastused = Range("B1000000").End(xlUp).Row
rw = Target.Row
rw2 = Target.End(xlDown).Row - 1
Target.Offset(1).Resize(rw2 - rw).Select
n = lastused - rw
If n = 0 Then n = 1
If Selection.Rows.Count > 100000 Then Target.Offset(1).Resize(n).Select
End Sub
 
Upvote 0
Right-click the sheet tab, select View Code, paste this code in. It works by double-clicking "London" or "New York" or "Sydney" etc.
No buttons necessary.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Value = "" Then Exit Sub
lastused = Range("B1000000").End(xlUp).Row
rw = Target.Row
rw2 = Target.End(xlDown).Row - 1
Target.Offset(1).Resize(rw2 - rw).Select
n = lastused - rw
If n = 0 Then n = 1
If Selection.Rows.Count > 100000 Then Target.Offset(1).Resize(n).Select
End Sub
Here is another way you could have written your DoubleClick event code...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim LastUsedRow As Long
  If Target.Value = "" Or Target(1).Offset(1) <> "" Then Exit Sub
  LastUsedRow = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
  If Target.Row < LastUsedRow Then Range(Target, Cells(LastUsedRow, Target.Column)).SpecialCells(xlBlanks).Areas(1).EntireRow.Select
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Thanks for the code...However:

1) The code doesn't select the entire row, only those cells in the the first column
2) It fails if there is data in the first cell below London, New York, Sydney

Apologies but I suppose I should have added factor 2) into the Query.

The actual data is below...
Select 2 rows for Aldershot
Select 5 rows for Bulford
Select 8 rows for Larkhill (including the row that contains Tue 18-April-17

ALDERSHOT

<tbody>
</tbody>
MN 0 610JUNIOR RANK SLA (40 beds)NB
NN A 060VEHICLE GARAGINGA
BULFORD
WI 0 625PACKAGE PLANTROOMNB
WI 0 626PACKAGE PLANTROOMNB
WI 0 627PACKAGE PLANTROOMNB
WI 0 613ARMOURYNB
WI 0 212VEHICLE GARAGINGA
LARKHILL

<tbody>
</tbody>
Tue 18-Apr-17LA 0 238JUNIOR & SENIOR OFFICER SLAA
LA 0 680JUNIOR & SENIOR OFFICER SLA (24 beds)NB
LA 0 259VEHICLE GARAGINGA
LA 0 254VEHICLE GARAGINGA
LA H 003MILITARY VEHICLE HARDSTANDINGNB
LA 0 310VEHICLE GARAGINGA
LX C 665PRIVATE PARKING (52 bays)NB
LA 0 636JUNIOR RANK SLA (48 beds)NB
Thanks Stirling

<tbody>
</tbody>

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Rick, that works great, thanks but... if there is data in the first cell below London etc, then it doesn't work .. can you please update the code to incorporate this.. they will be times when there is no data, and other times when data will be in rows, as per the above
 
Upvote 0
Rick, that works great, thanks but... if there is data in the first cell below London etc, then it doesn't work .. can you please update the code to incorporate this.. they will be times when there is no data, and other times when data will be in rows, as per the above
Do you mean like what I show in red below? If so, and I double click the word "London", what rows should be selected, 2:5 or 3:5?

1 London20172018
2 Data1020
31020
41020
51020
6 New York20172018
71020
81020
9 Sydney20172018

<tbody>
</tbody>
 
Last edited:
Upvote 0
Yes, if there is DATA in 2, then select 2:5.. then I click on New York and it selects 7:8 (even if there is no data in 7...)
I have code for the SORT already.. can I paste that after the code for the Row selection?
 
Upvote 0
Rick, I have just discovered that your formula works perfectly well, as I simply click on the 'DATA' cell (which itself doesn't need sorting) and the formula selects the remaining rows until the next heading - excellent.
My problem is now, as I have just discovered, that the SORT VBA details uses row references (even if I click on relative reference), so is there a way to just say 'with selection' and not mention the first and last rows?
Thanks,
Raymond
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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