简体   繁体   English

工作和激活多个工作簿的问题 excel VBA

[英]Issues with Working and Activating Multiple Workbooks excel VBA

I'm having some issues when working with calling and working with multiple workbooks.我在调用和处理多个工作簿时遇到了一些问题。 I have a macro that updates some excel sheets using some data.我有一个宏,可以使用一些数据更新一些 Excel 工作表。 All 5 of those spreadsheets are assigned a variable with the filename and the file path.所有这 5 个电子表格都分配了一个带有文件名和文件路径的变量。 When I run a second SUB / Macri to Save and Close all 5 of those workbooks after they are done updating - It doesn't select the right workbooks even though they've been assigned the same variable names as the previous macro.当我在完成更新后运行第二个 SUB / Macri 以保存并关闭所有 5 个工作簿时 - 即使它们已被分配与前一个宏相同的变量名称,它也没有选择正确的工作簿。 So I believe my issue is - If the workbooks are already open I can't associate a variable too them.所以我相信我的问题是 - 如果工作簿已经打开,我也无法将变量关联起来。 - I assume that if the file name and file path are right, the workbooks open can be set as variables and then be able to close them - Thoughts? - 我假设如果文件名和文件路径正确,打开的工作簿可以设置为变量,然后可以关闭它们 - 想法?

    Sub CloseWorkbooks()

    Dim MB, WB1, WB2, WB3, WB4, WB5 As Workbook
    Dim FP1, FN1, FN2, FN3, FN4, FN5 As String


    FP1 = "G:\DATA......"

        FN5 = "Book1.xlsx"
        FN2 = "Book2.xlsx"
        FN3 = "Book3.xlsx"
        FN4 = "Book4.xlsx"
        FN1 = "Book5.xlsx"

        Application.DisplayAlerts = False

            Set WB1 = Workbooks.Open(Filename:=FP1 & FN1)
            WB1.Activate
            WB1.Save

            'Application.Wait (Now + TimeValue("00:00:05"))
            WB1.Close


                Set WB2 = Workbooks.Open(Filename:=FP1 & FN2)
                WB2.Activate
                WB2.Save
                'Application.Wait (Now + TimeValue("00:00:05"))
                WB2.Close


                    Set WB3 = Workbooks.Open(Filename:=FP1 & FN3)
                    WB3.Activate
                    WB3.Save
                    Application.Wait (Now + TimeValue("00:00:02"))
                    WB3.Close


                        Set WB4 = Workbooks.Open(Filename:=FP1 & FN4)
                        WB4.Activate
                        WB4.Save
                        Application.Wait (Now + TimeValue("00:00:02"))
                        WB4.Close


                            Set WB5 = Workbooks.Open(Filename:=FP1 & FN5)
                            WB5.Activate
                            WB5.Save
                            Application.Wait (Now + TimeValue("00:00:02"))
                            WB5.Close



End Sub

This works if the worksheets are NOT Open, but it doesn't work if the worksheets are open - which is what I want it to accomplish.如果工作表未打开,则此方法有效,但如果工作表已打开,则此方法无效 - 这就是我希望它完成的任务。 The previous macro opens all the worksheets and updates them.上一个宏打开所有工作表并更新它们。 I want this macro (2nd one shown above) to save and close all the workbooks.我希望这个宏(上面显示的第二个)保存并关闭所有工作簿。

-Thank you. -谢谢。

If you replace your Workbooks.Open(...) with a call to this it should return the workbook if its open, and open and return it if it is not.如果您将Workbooks.Open(...)替换为对 this 的调用,它应该在打开时返回工作簿,如果未打开则打开并返回它。

Public Function getWorkbookByFileName(ByVal FileName As String) As Workbook
Dim Book As Workbook: Set Book = Nothing
Dim Count As Integer: Count = Application.Workbooks.Count
Dim Index As Integer: Index = 1
Do
    If Application.Workbooks(Index).FullName = FileName Then
        Set Book = Application.Workbooks(Index)
        Exit Do
    End If
    If Index < Count Then
        Index = Index + 1
    Else
        Exit Do
    End If
Loop
If Book Is Nothing Then
    Set Book = Application.Workbooks.Open(FileName:=FileName)
End If
Set getWorkbookByFileName = Book
End Function

This is fairly long winded, looking at some of the answers to Detect whether Excel workbook is already open I see that it can be achieved without looping through the workbooks.这是相当冗长的,查看检测 Excel 工作簿是否已经打开的一些答案我看到它可以在不循环工作簿的情况下实现。

This will close the correct workbook.这将关闭正确的工作簿。

Sub CloseWorkbooks()
    Application.DisplayAlerts = False

    Dim wb As Workbook
    Dim i As Long

    For i = 1 To 6
        Set wb = Workbooks.Open(Filename:="G:\DATA......Book" & i & ".xlsx")
        Application.Wait (Now + TimeValue("00:00:02"))
        wb.Close SaveChanges:=True
    Next i
Exit Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM