Excel VB This Active Worksheet

I am sure that it is really easy, but I can not find information about it.

I have an Excel macro that 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 

I want to make this a global macro, but the file name is hardcoded in the macro. How can I change the code to refer to the "this" currently open file?

+4
source share
2 answers

Ok, I decided that you need to change it:

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

in

 ActiveWorkbook.ActiveSheet.Sort 

Credit this SO element when creating a button in Excel

+7
source

ActiveWorkbook refers to the currently open and active file.

If you refer by the file name 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 .

+3
source

Source: https://habr.com/ru/post/1441085/


All Articles