[英]Excel VBA, using FileDialog to open multiple workbooks and reference them
I am currently using to following code to prompt the user for a workbook, open it, get some information from it and then close it. 我目前正在使用以下代码来提示用户输入工作簿,将其打开,从中获取一些信息,然后关闭它。 at the moment, I address the opened workbook by using the workbooks collection with and index ("woorkbooks(2)").
目前,我通过使用带有和索引(“ woorkbooks(2)”)的工作簿集合来处理打开的工作簿。 Now I need to open two workbooks, and my problem is that I wouldn't know which of the workbooks will be indexed as 2 and which will be indexed as 3. So, I figured there must be a way to get a reference to each workbook.
现在,我需要打开两个工作簿,而我的问题是,我不知道哪个工作簿将被索引为2,哪些将被索引为3。因此,我认为必须有一种获取每个工作簿的引用的方法。工作簿。
Function openfile() As Boolean
Dim fd As FileDialog
Dim file_was_chosen As Boolean
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Filters.Clear
.Filters.Add "Excel File", "*.xl*"
End With
file_was_chosen = fd.Show
If Not file_was_chosen Then
MsgBox "You didn't select a file"
openfile = False
Exit Function
End If
fd.Execute
openfile = True
End Function
Now I've seen some solutions to this problem involving getting the full path of each workbook, but I'd prefer avoid using the full path since it contains words in different language (and the name of the workbook appears with question marks). 现在,我已经看到了解决此问题的一些解决方案,其中涉及获取每个工作簿的完整路径,但我宁愿避免使用完整路径,因为它包含不同语言的单词(并且工作簿的名称带有问号)。 Moreover, I'd prefer a solution in which the user is promped only once for 2 files and not twice.
而且,我更喜欢这样一种解决方案:对于2个文件,用户仅被提示一次,而不会被提示两次。
This version gives the user a single dialog. 此版本为用户提供了一个对话框。 Enjoy.
请享用。 And whoever downvoted my other answer, please add a comment to that explaining what you so disliked about it that it required a downvote.
无论谁拒绝我的其他回答,请在该注释中添加一条评论,以解释您对此不满意的地方,以至于需要拒绝表决。
Function openfile() As Variant
Dim aOpen(2) As String, itm As Variant, cnt As Long, lAsk As Long
Dim fd As FileDialog
Dim file_was_chosen As Boolean
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Filters.Clear
.Filters.Add "Excel File", "*.xl*"
End With
Do
file_was_chosen = fd.Show
If Not file_was_chosen Or fd.SelectedItems.Count > 2 Then
lAsk = MsgBox("You didn't select one or two files, try again?", vbQuestion + vbYesNo, "File count mismatch")
If lAsk = vbNo Then
openfile = aOpen
Exit Function
End If
End If
Loop While fd.SelectedItems.Count < 1 Or fd.SelectedItems.Count > 2
cnt = 0
For Each itm In fd.SelectedItems
aOpen(cnt) = itm
cnt = cnt + 1
Next
openfile = aOpen
fd.Execute
End Function
Sub test()
Dim vRslt As Variant
Dim wkb As Excel.Workbook, wkb1 As Excel.Workbook, wkb2 As Excel.Workbook
vRslt = openfile
For Each wkb In Application.Workbooks
If wkb.Path & "\" & wkb.Name = vRslt(0) Then Set wkb1 = wkb
If wkb.Path & "\" & wkb.Name = vRslt(1) Then Set wkb2 = wkb
Next
If vRslt(0) = "" Then ' no files
MsgBox "No files opened so nothing happens..."
ElseIf vRslt(1) = "" Then ' one file was opened
MsgBox "One file so do whatever you want for one file"
Else ' two files were opened
MsgBox "Two files so do whatever you want for two files"
End If
End Sub
Working with your existing openfile function, change the return from Boolean to Excel.Workbook. 使用现有的openfile函数,将返回值从Boolean更改为Excel.Workbook。 If they don't open a workbook you set it to Nothing instead of false, otherwise you set it to the workbook reference of the file you just opened (You'll need to modify openfile to get that reference).
如果他们没有打开工作簿,则将其设置为Nothing而不是false,否则将其设置为刚刚打开的文件的工作簿引用(您需要修改openfile以获得该引用)。 You then just call it twice and set a workbook reference for each call that is not Nothing.
然后,您只需调用两次,并为每个非Nothing的调用设置一个工作簿引用。
Example code below is written freeform and is untested - it's really just glorified pseudocode - but should point you the right general direction. 下面的示例代码是自由格式编写的,未经测试-实际上只是美化的伪代码-但应为您指明正确的大致方向。
sub test
dim lAsk as long
dim wkb1 as excel.workbook
dim wkb2 as excel.workbook
do
if wkb1 is Nothing then
set wkb1 = openfile
if wkb1 is Nothing then
lAsk = msgbox("you didn't select a first file, try again?",vbyesno,"No file selected")
if lAsk = vbNo then exit do
end if
elseif wkb2 is Nothing then
set wkb2 = openfile
if wkb2 is Nothing then
lAsk = msgbox("you didn't select a second file, try again?",vbyesno,"No file selected")
if lAsk = vbNo then exit do
end if
end if
loop while wkb1 is Nothing or wkb2 is Nothing
' do whatever with wkb1 and wkb2 here
end sub
Edited to add: 编辑添加:
Here's a very basic shape for your revised openfile function. 这是修改后的openfile函数的非常基本的形状。 Again, untested but I've modified it from one of my own procs so it should work
再次,未经测试,但我已经从我自己的proc中修改了它,因此它应该可以工作
Function openfile() As Excel.Workbook
Dim sFilter As String
Dim sTitle As String
Dim vFileName As Variant
sFilter = "Excel Files (*.xl*), *.xl*, CSV Files (*.csv), *.csv, All Files (*.*), *.*"
sTitle = "Select file to process"
vFileName = Application.GetOpenFilename(filefilter:=sFilter, Title:=sTitle)
If vFileName = False Then
Set openfile = Nothing
Else
Set openfile = Workbooks.Open(Filename:=vFileName)
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.