Sort with VBA works in general but not worksheet

KillGorack

New Member
Joined
Jan 23, 2006
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
I'm using the following code to sort a range.

VBA Code:
ThisWorkbook.Sheets("Area").range("A1:D18").Sort Key1:=range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

It works well when I place it in a module, but when I use this code in the worksheet.. it doesn't work at all.

Any idea?
 

Attachments

  • Capture.PNG
    Capture.PNG
    21.8 KB · Views: 6

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
with a with ... end with and a point in front
VBA Code:
    With ThisWorkbook.Sheets("Area").Range("A1:D18")
          .Sort Key1:=.Range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
     End With
 
Upvote 0
I'm using the following code to sort a range.

VBA Code:
ThisWorkbook.Sheets("Area").range("A1:D18").Sort Key1:=range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

It works well when I place it in a module, but when I use this code in the worksheet.. it doesn't work at all.

Any idea?
The code specifies the sheet it will work in... ThisWorkbook.Sheets("Area")

Remember to modify range and sort key in code to work as per your need in Sheet "Overall" range("A1:D18").Sort Key1:=range("B2")

Use this and try

VBA Code:
ThisWorkbook.Sheets("Area").range("A1:D18").Sort Key1:=range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ThisWorkbook.Sheets("Overall").range("A1:D18").Sort Key1:=range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 
Upvote 0
Or maybe
VBA Code:
With ThisWorkbook.Sheets("Area")
    .range("A1:D18").Sort Key1:=.range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
 
Upvote 0
Thanks for the replies.. I had to show this in a meeting so I kinda hacked it together, in the sheet I was to do the sort. I placed the sort in the worksheet activate

It works.. In the area sheet vba.

VBA Code:
Private Sub Worksheet_Activate()
    Dim rRow As Integer
    rRow = ThisWorkbook.Sheets("Area").Cells(Rows.Count, 1).End(xlUp).Row
    ThisWorkbook.Sheets("Area").range("A1:D" & rRow).Sort Key1:=range("B2"), Order1:=xlDescending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

edited the code
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,907
Members
449,478
Latest member
Davenil

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