vba Excel VB 此活动工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12997100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:15:23  来源:igfitidea点击:

Excel VB This Active Worksheet

excelexcel-vbaworksheetvba

提问by Dan Twining

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:

我有一个 Excel 宏,其中包含以下内容:

    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?

我想要做的是使它成为一个全局宏,但文件名在宏中硬编码。如何更改代码以使其引用“此”当前打开的文件?

回答by Dan Twining

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

感谢这个SO项目在Excel中创建一个按钮

回答by assylias

ActiveWorkbookdoes reference the current open and active file.

ActiveWorkbook确实引用了当前打开和活动的文件。

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

如果文件名,你指的是事实表名称是硬编码的,你可以用主动的宏观调控工作被替换ActiveWorkbook.Worksheets("Data 21 Oct 12 11-05")ActiveSheet