簡體   English   中英

用循環打開文件路徑中的所有excel文件后,是否可以通過vba創建工作簿變量來引用這些文件?

[英]After opening all excel files in a file path with a loop, is there any way to reference these files by creating workbook variables through vba?

Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:--"
(leaving out the file path)
MyFile = Dir(MyFolder & "\*.xlsx")

Do While MyFile <> ""
    Workbooks.Open fileName:=MyFolder & "\" & MyFile
    MyFile = Dir
Loop

(see paragraph below)

Workbook.Open
Dim wbk1 as workbook
Set wbk1 = ActiveWorkbook
(can reference workbook like this)
wbk1.Activate

我檢查了其他幾個論壇,發現可以先打開其他工作簿,然后創建一個變量並將其設置為上面第二個代碼段中列出的打開的工作簿,以引用其他工作簿。

當我試圖為該特定文件路徑中的所有文件創建參考變量的方法時,我意識到我無法在運行時動態命名不同的變量來將每個工作簿設置為不同的工作簿變量。

因此,除了完成我要完成的任務之外,還有其他選擇嗎?或者是否可以動態創建變量?

這是您的操作方法:(這將以新名稱將其作為模板打開)

Dim wb1 as Workbook
Set wb1 = Workbooks.Add(MyFile)

或者:(如果工作簿已經打開,則將失敗)(如果以后需要保存,請使用此)

Dim wb1 as Workbook
Set wb1 = Workbooks.Open(MyFile)

然后,您可以創建一個工作表對象,如下所示:

Dim ws1 as Worksheet
Set ws1 = wb1.Worksheets(1)

然后,只要您想引用該工作表上的某些內容(例如RangeCell確保使用“ Worksheet引用來限定它,如下所示:

Dim rng as Range
Set rng = ws1.Range("A1:B1")

您可以使用數組,集合或字典來保存對多個工作簿的引用。
最好打開一個工作簿,執行所需的操作,將其關閉,然后使用相同的變量打開下一個工作簿。

注意:要在變量中正確存儲工作簿,請使用@brax提供的代碼。

但是...這是您要求的:

此代碼將打開一個文件夾中的每個工作簿,然后返回有關每個工作簿的信息。

Option Explicit 'You wouldn't believe how important this is at the top of your module!

Public Sub Test()

    Dim MyFolder As String
    Dim MyFiles As Collection
    Dim wrkBk As Workbook
    Dim sFile As String
    Dim secAutomation As MsoAutomationSecurity

    MyFolder = "C:\"
    Set MyFiles = New Collection

    'We don't want any WorkBook_Open macros to fire when we open the file,
    'so remember the current setting and then disable it.
    secAutomation = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityForceDisable

    sFile = Dir$(MyFolder & "*.xls*")
    Do While Len(sFile) > 0
        'We don't want to open the file if it's got the same name as this one.
        If sFile <> ThisWorkbook.Name Then
            'Open the workbook and add it to the collection, give it a key of the file name.
            Set wrkBk = Workbooks.Open(MyFolder & sFile)
            MyFiles.Add wrkBk, wrkBk.Name
            Set wrkBk = Nothing
        End If
        sFile = Dir$
    Loop

    'Reset macro security settings.
    Application.AutomationSecurity = secAutomation

    '----------------
    'All files are open and ready to be referenced.
    '----------------
    Dim SingleFile As Variant

    'List all details from each file in the immediate Window.
    For Each SingleFile In MyFiles
        With SingleFile
            Debug.Print "Name:  " & .Name & " | Sheet Count: " & .Sheets.Count & _
                " | Last Row on '" & .Worksheets(1).Name & "' Column A: " & .Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
        End With
    Next SingleFile

    'Get the value from a specific file using the key value (Book7.xlsm)
    Debug.Print MyFiles("Book7.xlsm").Worksheets("Form").Range("A6")

    'Now close all the files.
    For Each SingleFile In MyFiles
        Debug.Print "Closing " & SingleFile.Name
        SingleFile.Close SaveChanges:=False
    Next SingleFile

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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