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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

BSALV

Banned user
Joined
Oct 31, 2010
Messages
1,651
Office Version
  1. 365
  2. 2013
  3. 2007
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

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
673
Office Version
  1. 2021
Platform
  1. MacOS
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,649
Office Version
  1. 365
Platform
  1. Windows
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

KillGorack

New Member
Joined
Jan 23, 2006
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,649
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,186,777
Messages
5,959,724
Members
438,443
Latest member
azedin4

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
Top