激活可变工作簿名称的窗口

时间:2018-01-29 08:53:46

标签: excel vba excel-vba

我想知道如何在不知道日期并激活其窗口的情况下打开名称中包含日期的工作簿。

以下是一个例子:

Dim src As Workbook
Set src = Workbooks.Open(ActiveWorkbook.Path & "\Seats 2018-1-02.xlsx", True, True)

Windows("Seats 2018-1-02.xlsx").Activate

exportDate = Mid(ActiveWorkbook.Name, 22, 9) //extract export date
Range("M15").Select

我该怎么办? 谢谢

1 个答案:

答案 0 :(得分:1)

您无需激活工作表。由于您要为工作表声明一个对象,因此当您调用以" wb。"开头的方法时你正在选择那个对象。例如,您可以在工作簿之间跳转而不激活此处的任何内容:

Sub Test()

Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test1.xls")
Dim wb2 As Excel.Workbook
Set wb2 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test2.xls")

wb1.Sheets("Sheet1").Cells(1, 1).Value = 24
wb2.Sheets("Sheet1").Cells(1, 1).Value = 24
wb1.Sheets("Sheet1").Cells(2, 1).Value = 54

End Sub

如果您不知道确切的名称,也可以找到您的文件:

Sub openAllFiles()

yourPath = "C:\Documents and Settings\xxxx\Desktop\"
file = Dir(yourPath & "Seats*.xlsx")
Do While file <> vbNullString
Workbooks.Open (yourPath & file)
file = Dir()
Loop
End Sub