BA sort - How to fix this problem?

Lasalle1370

New Member
Joined
Aug 7, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
There are 2 worksheets in the workbook: sheet1 & sheet2. I have code in sheet2 that will sort sheet1. I want to sort the entire worksheet Sheet1 based on column "A". See the attached image for what Sheet1 looks like.

The code in sheet2 is:

Sub sortWorksheet()
Dim sortrange As Range

Worksheets("Sheet1").Activate
Set sortrange = Range("A:I")
sortrange.Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
End Sub

Nothing happens when I run this code.
What am I missing?

Worksheet image.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to MrExcel.
Is the code in the sheet2 code module, or in a normal module?
 
Upvote 0
I am not exzactly sure of the meaning of your question.
Does the attached image answer your question?

Response 1.png
 
Upvote 0
Try:
Code:
Sub sortWorksheet()
Dim sortrange As Range

With Worksheets("Sheet1")
    Set sortrange = .Range("A:I")
    sortrange.Sort key1:=.Range("A1"), order1:=xlAscending, Header:=xlYes
End With
End Sub
 
Upvote 0
@Lasalle1370 you will get that issue with your code in post number one if your code was in the place you have marked as "Code can be either here".

The code that @Phuoc has posted will work in either place but should still be in the place that you have marked as "Or here - either place"
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,862
Members
449,052
Latest member
Fuddy_Duddy

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