ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Referring to a Range Based on Selected Rows (https://www.excelbanter.com/excel-programming/348650-referring-range-based-selected-rows.html)

Sprinks

Referring to a Range Based on Selected Rows
 
To sort a list such as the following alphanumerically, I've created
subroutines to insert a leading zero on the single-digit entries, sort, and
then strip off the leading zero. These work fine, assuming the user has
selected the entire range to be sorted, and not just entries in this column.

I'd like to bulletproof the macro by:
- determining the beginning and ending row of the selection (x, y)
- sorting the range Bx:Fy

Thanks for your input.

Sprinks

Pre-sort condition
--------------------
AC20
AC1
AC2
AC9
AC10

Normal Excel Sort Result
-----------------------------
AC1
AC10
AC2
AC20
AC9

Macro Sort Result
---------------------
AC1
AC2
AC9
AC10
AC20

Tom Ogilvy

Referring to a Range Based on Selected Rows
 
x = selection(1).row
y = selection(selection.count).row
set rng = Range(cells(x,"B"),cells(y,"F"))


--
Regards,
Tom Ogilvy


"Sprinks" wrote in message
...
To sort a list such as the following alphanumerically, I've created
subroutines to insert a leading zero on the single-digit entries, sort,

and
then strip off the leading zero. These work fine, assuming the user has
selected the entire range to be sorted, and not just entries in this

column.

I'd like to bulletproof the macro by:
- determining the beginning and ending row of the selection (x, y)
- sorting the range Bx:Fy

Thanks for your input.

Sprinks

Pre-sort condition
--------------------
AC20
AC1
AC2
AC9
AC10

Normal Excel Sort Result
-----------------------------
AC1
AC10
AC2
AC20
AC9

Macro Sort Result
---------------------
AC1
AC2
AC9
AC10
AC20




Sprinks

Referring to a Range Based on Selected Rows
 
Thanks, Tom; this works.

I'm having an additional problem. The Selection.Sort line in the following
code generates the error €œSort method of Range class failed.€

Can you tell me why?

Sub AlphaSort()
Dim rng As Range
Dim intFirst As Integer
Dim intLast As Integer

' Sorts Alphanumeric list by adding then removing leading zeros
Call AddLeadingZero

intFirst = Selection(1).Row
intLast = Selection(Selection.Count).Row
Set rng = Range(Cells(intFirst, "B"), Cells(intLast, "F"))
rng.Select

Selection.Sort

Call RemoveLeadingZero

End Sub

Thank you.

"Sprinks" wrote:

To sort a list such as the following alphanumerically, I've created
subroutines to insert a leading zero on the single-digit entries, sort, and
then strip off the leading zero. These work fine, assuming the user has
selected the entire range to be sorted, and not just entries in this column.

I'd like to bulletproof the macro by:
- determining the beginning and ending row of the selection (x, y)
- sorting the range Bx:Fy

Thanks for your input.

Sprinks

Pre-sort condition
--------------------
AC20
AC1
AC2
AC9
AC10

Normal Excel Sort Result
-----------------------------
AC1
AC10
AC2
AC20
AC9

Macro Sort Result
---------------------
AC1
AC2
AC9
AC10
AC20



All times are GMT +1. The time now is 02:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com