Sort worksheet without selection

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi,

Want to sort worksheet without selection,
code below dosent work,
get error "Object do not support property or method"

VBA Code:
With Worksheets("Sheet1")
  .SortFields.Add Key:=Range("B1"), Order:=xlAscending
  .SortFields.Add Key:=Range("A1"), Order:=xlAscending
  .SetRange Range("A1:AO" & lastrow)
  .Header = xlYes
  .Apply
End With
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Does this do what you want?

VBA Code:
With Worksheets("Sheet1").Sort
  .SortFields.Clear
  .SortFields.Add Key:=.Parent.Range("B1"), Order:=xlAscending
  .SortFields.Add Key:=.Parent.Range("A1"), Order:=xlAscending
  .SetRange .Parent.Range("A1:AO" & lastrow)
  .Header = xlYes
  .Apply
End With
 
Upvote 0
Solution
Does this do what you want?

VBA Code:
With Worksheets("Sheet1").Sort
  .SortFields.Clear
  .SortFields.Add Key:=.Parent.Range("B1"), Order:=xlAscending
  .SortFields.Add Key:=.Parent.Range("A1"), Order:=xlAscending
  .SetRange .Parent.Range("A1:AO" & lastrow)
  .Header = xlYes
  .Apply
End With
get error on linje
VBA Code:
.SetRange .Parent.Range("A1:AO" & lastrow)
"Program or object not difined"
 
Upvote 0
get error on linje
VBA Code:
.SetRange .Parent.Range("A1:AO" & lastrow)
"Program or object not difined"
I am sorry,
I forget
Code:
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row ' sista raden in kolumn "A"

It works perfect,
tanks ??
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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