Sorting data in sheet from workbook module code

Thebatfink

Active Member
Joined
Apr 8, 2007
Messages
410
Hi,

I'm scratching my head :/ I'm simply trying to perform a sort on some cells, but the code is in a workbook module. I have found a solution, but I don't understand why I need to do it..

Code:
Workbooks(twfn).Worksheets("compiled").Range(Cells(2, pastecol), Cells(pastelastrow + 1, _
pastecol + 1)).Sort Key1:=Workbooks(twfn).Worksheets("compiled").Cells(2, pastecol)

Thats the code I'm using, but why do I need to activate sheet "compiled" with

Code:
Workbooks(twfn).Worksheets("compiled").Activate

before it will work?? I've specified the Workbook name AND the Worksheet name?

Thanks!
Batfink
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It is because of the two Cells references in red, they are not fully qualified

Code:
Workbooks(twfn).Worksheets("compiled").Range([COLOR=red][B]Cells[/B][/COLOR](2, pastecol), [COLOR=red][B]Cells[/B][/COLOR](pastelastrow + 1, _
pastecol + 1)).Sort Key1:=Workbooks(twfn).Worksheets("compiled").Cells(2, pastecol)

Change them both into
Workbooks(twfn).Worksheets("compiled").Cells
and it will work without activating the sheet.

In order to keep the code shorter and more readable, you'd best use With/End With, like this:
Code:
With Workbooks(twfn).Worksheets("compiled")
   .Range(.[COLOR=black]Cells[/COLOR](2, pastecol), .[COLOR=black]Cells[/COLOR](pastelastrow + 1, pastecol + 1)).Sort _
       Key1:=.Cells(2, pastecol)
End With
 
Last edited:
Upvote 0
Hi,

Thanks for the reply. I understand now, didn't realise that with the cells.

Funnily enough I did try and use With, but it doesn't sort the data. It doesn't error, but it doesn't perform the sort either?

I've just substituted my sort line with your With / End With statement and nothing gets sorted? Why would that be?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,522
Members
452,923
Latest member
JackiG

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