如何使用 VBA 在字符串中列出工作簿中的所有工作表?

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

How can I use VBA to list all worksheets in a workbook within a string?

vbaworksheet

提问by seegoon

How can I use VBA to get the names of all the worksheets (or alternatively a maximum of three) within the active workbook to be returned as a string? For context, I will then use the string in naming the workbook when it is saved. I have figured out how to create a savename for the file using input dialogs and so on, so it's only a case of getting VBA to return something like "[Worksheet 1 name] and [Worksheet 2 name]".

如何使用 VBA 获取活动工作簿中所有工作表的名称(或者最多三个)以字符串形式返回?对于上下文,我将在保存工作簿时使用该字符串命名工作簿。我已经想出了如何使用输入对话框等为文件创建保存名,所以这只是让 VBA 返回“[工作表 1 名称] 和 [工作表 2 名称]”之类的内容的一种情况。

Thanks so much!

非常感谢!

回答by Scott Holtzman

Option Explicit

Sub namesheets()

Dim wks as Worksheet, strName as String

For each wks in Worksheets
     strName = strName & wks.Name
Next

End Sub

You can also google "Excel vba loop through worksheets in a workbook" or something to that effect and find the answer very easily.

您还可以谷歌“Excel vba 循环工作簿中的工作表”或类似的东西,并很容易地找到答案。

Also, this question was asked in some form on this website. See link... Loop through subset of worksheets

此外,这个问题是在这个网站上以某种形式提出的。请参阅链接...循环工作表的子集