Hello,
I'm trying to sort some data according to a column selected via a Message Box.
The code I'm using is;
Dim Answer As String
Dim MyNote As String
Dim RowCounter As Integer
Dim MyRange As Range
Range("A1").Select
Selection.End(xlDown).Select
RowCounter = ActiveCell.Row 'this tells me how tall the data column is across all the rows
'ChoiceOne:
MyNote = "The customers will now be sorted according to their TOTAL debt. Answer NO for other options."
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Sort options")
If Answer = vbNo Then
GoTo ChoiceTwo
Else
Set MyRange = ActiveSheet.Range(Cells(2, 11), Cells(RowCounter, 11))
GoTo Sort
End If
[I'm putting some more dialogue boxes in here which I'm omitting for now]
Sort:
Cells.Select
ActiveWorkbook.Worksheets("Sales Ledger Aged Debtors Repor").Sort.SortFields. _ } This is the line where the macro stops.
Add Key:=Range("MyRange"), SortOn:=xlSortOnValues, Order:=xlDescending, _ } I think it's not recognising "MyRange" as a valid parameter
DataOption:=xlSortNormal }
With ActiveWorkbook.Worksheets("Sales Ledger Aged Debtors Repor").Sort
.SetRange Range("MyRange")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:K").Select 'Fix column widths
Selection.Columns.AutoFit
Rows("1:1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
End With
I'm getting a problem at the time of execution of the Sort instruction. At least I think that's what the Error Message "Method 'Range' of object '_Global' failed.
Any ideas please?
Downes51
I'm trying to sort some data according to a column selected via a Message Box.
The code I'm using is;
Dim Answer As String
Dim MyNote As String
Dim RowCounter As Integer
Dim MyRange As Range
Range("A1").Select
Selection.End(xlDown).Select
RowCounter = ActiveCell.Row 'this tells me how tall the data column is across all the rows
'ChoiceOne:
MyNote = "The customers will now be sorted according to their TOTAL debt. Answer NO for other options."
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Sort options")
If Answer = vbNo Then
GoTo ChoiceTwo
Else
Set MyRange = ActiveSheet.Range(Cells(2, 11), Cells(RowCounter, 11))
GoTo Sort
End If
[I'm putting some more dialogue boxes in here which I'm omitting for now]
Sort:
Cells.Select
ActiveWorkbook.Worksheets("Sales Ledger Aged Debtors Repor").Sort.SortFields. _ } This is the line where the macro stops.
Add Key:=Range("MyRange"), SortOn:=xlSortOnValues, Order:=xlDescending, _ } I think it's not recognising "MyRange" as a valid parameter
DataOption:=xlSortNormal }
With ActiveWorkbook.Worksheets("Sales Ledger Aged Debtors Repor").Sort
.SetRange Range("MyRange")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:K").Select 'Fix column widths
Selection.Columns.AutoFit
Rows("1:1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
End With
I'm getting a problem at the time of execution of the Sort instruction. At least I think that's what the Error Message "Method 'Range' of object '_Global' failed.
Any ideas please?
Downes51