excel VBA 如何从工作表名称确定工作表索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8767703/
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-11 14:52:12  来源:igfitidea点击:

excel VBA how to determine sheet index from sheet name

excelexcel-vbavba

提问by Jim

In the ongoing project of mine, with which I'm about to be done, I want to make sure that the people taking over my job will be left with something that won't break after I'm gone. This project is an ongoing tracker kept daily throughout the year and each worksheet used (several for varying items of interest) are separated out by year in the name of the worksheet. For instance, the primary sheet is simply called 'YYYY' in which the 'YYYY' is the current year. Other worksheets are called, 'DTDYYYY', 'MTDYYYY', 'YTDYYYY' in which the YYYY is the current year. Come 2013, I'd like it if the little VBA that I use is able to take the current YYYY and find the appropriate worksheets automagically.

在我正在进行的项目中,我即将完成,我想确保接替我工作的人会留下一些在我离开后不会中断的东西。这个项目是一个持续的跟踪器,全年每天都在使用,每个使用的工作表(几个用于不同的兴趣项目)在工作表的名称中按年份分开。例如,主工作表简称为“YYYY”,其中“YYYY”是当前年份。其他工作表称为“DTDYYYY”、“MTDYYYY”、“YTDYYYY”,其中 YYYY 是当前年份。到 2013 年,我希望我使用的小 VBA 能够获取当前的 YYYY 并自动找到合适的工作表。

I know that I can reference sheets using the SheetIDX convention. I know that I can use Sheets(NAME) method. I'm not so sure how to best go about accomplishing my goal, though.

我知道我可以使用 SheetIDX 约定来引用工作表。我知道我可以使用 Sheets(NAME) 方法。不过,我不太确定如何最好地实现我的目标。

回答by Tim

Application.Sheets("Sheet1").Index
Application.Sheets(1).Name

Hope this is what you needed.

希望这是你所需要的。

Sorry, I didn't see the last part about current year. Perhaps this?

抱歉,我没有看到有关当年的最后一部分。也许这个?

Application.Sheets(Year(Date())).Activate

回答by Fionnuala

You need a string for the name.

您需要一个字符串作为名称。

With ThisWorkbook
    .Sheets(CStr(Year(Date))).Select
End With