簡體   English   中英

Excel VBA,使用FileDialog打開多個工作簿並引用它們

[英]Excel VBA, using FileDialog to open multiple workbooks and reference them

我目前正在使用以下代碼來提示用戶輸入工作簿,將其打開,從中獲取一些信息,然后關閉它。 目前,我通過使用帶有和索引(“ woorkbooks(2)”)的工作簿集合來處理打開的工作簿。 現在,我需要打開兩個工作簿,而我的問題是,我不知道哪個工作簿將被索引為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

現在,我已經看到了解決此問題的一些解決方案,其中涉及獲取每個工作簿的完整路徑,但我寧願避免使用完整路徑,因為它包含不同語言的單詞(並且工作簿的名稱帶有問號)。 而且,我更喜歡這樣一種解決方案:對於2個文件,用戶僅被提示一次,而不會被提示兩次。

此版本為用戶提供了一個對話框。 請享用。 無論誰拒絕我的其他回答,請在該注釋中添加一條評論,以解釋您對此不滿意的地方,以至於需要拒絕表決。

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

使用現有的openfile函數,將返回值從Boolean更改為Excel.Workbook。 如果他們沒有打開工作簿,則將其設置為Nothing而不是false,否則將其設置為剛剛打開的文件的工作簿引用(您需要修改openfile以獲得該引用)。 然后,您只需調用兩次,並為每個非Nothing的調用設置一個工作簿引用。

下面的示例代碼是自由格式編寫的,未經測試-實際上只是美化的偽代碼-但應為您指明正確的大致方向。

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

編輯添加:

這是修改后的openfile函數的非常基本的形狀。 再次,未經測試,但我已經從我自己的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM