在 Excel Vba 中插入提示以确定要打开的文件名

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

Insert prompt in Excel Vba to determine filename to open

excelvbaexcel-vbaprompt

提问by Stan

Let's say I have the following files (one for each week in a year):

假设我有以下文件(一年中的每周一个):

  • test 01.xlsm
  • test 02.xlsm
  • test .....xlsm
  • test 52.xlsm
  • 测试 01.xlsm
  • 测试 02.xlsm
  • 测试 .....xlsm
  • 测试 52.xlsm

I want to be able to choose the file I'd like to open with a prompt.

我希望能够在提示下选择我想打开的文件。

So instead of this function ...

所以而不是这个功能......

Workbooks.Open Filename:= _
    "F:\mydocs\test11.xlsm"

... I need something that lets me enter the number myself (so in this case "11" or whatever value between 01 and 52 depending on the week I want to see the results for).

...我需要一些可以让我自己输入数字的东西(所以在这种情况下,“11”或介于 01 和 52 之间的任何值,取决于我想查看结果的那一周)。

Is this possible? :s

这可能吗?:s

回答by Gary's Student

Perhaps:

也许:

Sub duraln()
Dim s As String
s = Application.InputBox(Prompt:="enter two digit suffix", Type:=2)
Workbooks.Open Filename:= _
    "F:\mydocs\test" & s & ".xlsm"
End Sub

回答by Mr. Mascaro

You should use an InputBox:

你应该使用一个InputBox

weekNum = InputBox("Input week number:")

weekNum = InputBox("Input week number:")

回答by David Zemens

Why not use the file picker?

为什么不使用文件选择器?

Dim wb as Workbook

ChDir "F:\mydocs\"

Set wb = Application.GetOpenFilename("Microsoft Excel Files, *.xls*")

This way you can just choose the file.

这样你就可以只选择文件。