简体   繁体   中英

Excel VB This Active Worksheet

I'm sure this is really easy but I can't find any info on it.

I've got a Excel macro which has things like:

    ActiveWorkbook.Worksheets("Data 21 Oct 12 11-05").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data21 Oct 12 11-05").Sort.SortFields.Add Key _
    :=Range("W1:W23"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data 21 Oct 12 11-05").Sort
    .SetRange Range("A1:BZ23")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

What I want to do is make this a global macro but the file name is hard coded in the macro. How can I change the code so it references "this" currently open file?

Ok, I worked out you need to change it from:

ActiveWorkbook.Worksheets("Data 21 Oct 12 11-05").Sort.

to:

ActiveWorkbook.ActiveSheet.Sort

Credit to this SO item on creating a button in Excel

ActiveWorkbook does reference the current open and active file.

If by file name you refer to the fact that the sheet name is hard coded, you can make the macro work with the active sheet by replacing ActiveWorkbook.Worksheets("Data 21 Oct 12 11-05") with ActiveSheet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM