Insert prompt in Excel Vba to determine filename to open

六月ゝ 毕业季﹏ 提交于 2019-12-25 11:48:28

问题


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

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).

Is this possible? :s


回答1:


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



回答2:


You should use an InputBox:

weekNum = InputBox("Input week number:")




回答3:


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.



来源:https://stackoverflow.com/questions/26467885/insert-prompt-in-excel-vba-to-determine-filename-to-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!