Sort column without referencing sheet name

K

KellyinCali

I am using a macro to reformat a txt file which was exported from a
proprietary program and opened with Excel. The sort works fine, but I
recorded the macro to get the code and when it sorts by column A, the
recorded code references the specific worksheet name which was automatically
named for the txt file name. Problem is that subsequent txt files will
always have a different name. I just need the wording to make it for
whatever the current sheet is.

TIA,
Kelly


The recorded code came out like this:
Columns("A:F").Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("QSYSPRT732957").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("QSYSPRT732957").Sort.SortFields.Add
Key:=Range( _
"A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("QSYSPRT732957").Sort
.SetRange Range("A1:F300")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 
J

Jacob Skaria

Replace

Worksheets("QSYSPRT732957")

with

ActiveSheet

If this post helps click Yes
 
K

KellyinCali

Would this work?

Cells.Select
Selection.Sort Key1:=Range("A1"), SortOn:=xlSortOnValues,
Order:=xlAscending, Header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
 
K

KellyinCali

Worked... of course! I knew it would be incredibly simple I just didn't know
the syntax. Thanks for indulging my laziness!!

Thanks Jacob!
 
J

Jacob Skaria

Try..

Cells.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Header:=xlNo, Orientation:=xlTopToBottom

The Sort method expects the below (all of them are optional)
expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header,
OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2,
DataOption3)


If this post helps click Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Applying Variables to SORT 4
Undo Macro Action 3
Sort by one column then another. 2
vba dynamic 1
Clear Check Box 2
VBA 2 Codes 2
writing a sort macro 2
Sort Macro Compatibility 3

Top