Excel VBA-创建用于打开文件的循环

时间:2018-07-09 15:10:41

标签: excel vba excel-vba

我有下面的代码用于打开文件。具体来说,它提示您打开对应于一年中每个月的12个不​​同文件。

尽管代码按原样工作,但似乎太长了,我认为可以通过循环来缩短代码。但是,我不确定该怎么写。

感谢您的帮助。

'Intialize workbooks
    Dim myFile As String, monthSumJan As Workbook, monthSumFeb As Workbook, monthSumMar As Workbook, monthSumApr As Workbook, monthSumMay As Workbook, monthSumJun As Workbook, monthSumJul As Workbook, monthSumAug As Workbook, monthSumSep As Workbook, monthSumOct As Workbook, monthSumNov As Workbook, monthSumDec As Workbook
    Dim Jan As Worksheet, Feb As Worksheet, Mar As Worksheet, Apr As Worksheet, May As Worksheet, Jun As Worksheet, Jul As Worksheet, Aug As Worksheet, Sep As Worksheet, Oct As Worksheet, Nov As Worksheet, Dec As Worksheet
    ChDir "C:\Users\stefan.bagnato\Desktop\Monthly Performance Summary"
    'January
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumJan = ThisWorkbook
    ActiveSheet.name = "Jan"
    Call ECHIBasicMonthlySummary
    'February
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumFeb = ThisWorkbook
    ActiveSheet.name = "Feb"
    Call ECHIBasicMonthlySummary
    'March
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumMar = ThisWorkbook
    ActiveSheet.name = "Mar"
    Call ECHIBasicMonthlySummary
    'April
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumApr = ThisWorkbook
    ActiveSheet.name = "Apr"
    Call ECHIBasicMonthlySummary
    'May
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumMay = ThisWorkbook
    ActiveSheet.name = "May"
    Call ECHIBasicMonthlySummary
    'June
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumJun = ThisWorkbook
    ActiveSheet.name = "Jun"
    Call ECHIBasicMonthlySummary
    'July
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumJul = ThisWorkbook
    ActiveSheet.name = "Jul"
    Call ECHIBasicMonthlySummary
    'August
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumAug = ThisWorkbook
    ActiveSheet.name = "Aug"
    Call ECHIBasicMonthlySummary
    'September
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumSep = ThisWorkbook
    ActiveSheet.name = "Sep"
    Call ECHIBasicMonthlySummary
    'October
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumOct = ThisWorkbook
    ActiveSheet.name = "Oct"
    Call ECHIBasicMonthlySummary
    'November
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumNov = ThisWorkbook
    ActiveSheet.name = "Nov"
    Call ECHIBasicMonthlySummary
    'December
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumDec = ThisWorkbook
    ActiveSheet.name = "Dec"
    Call ECHIBasicMonthlySummary

2 个答案:

答案 0 :(得分:1)

速战速决

Dim i As Long
For i = 1 To 12
    myFile = Application.GetOpenFilename
    Workbooks.Open Filename:=myFile
    Set monthSumJan = ThisWorkbook
    Call ECHIBasicMonthlySummary
Next i

对循环的解释here

您知道您不检查用户是否在对话框GetOpenFilename中按“取消”,不是吗?

更新:

由于尚不知道完整的相关代码,所以

Dim actWkb As Workbook
Dim i As Long
For i = 1 To 12
    myFile = Application.GetOpenFilename
    Set actWkb =  Workbooks.Open(myFile)
    Call ECHIBasicMonthlySummary(actWkb)
Next i

并调整

Sub ECHIBasicMonthlySummary(ByVal monthSh As Workbook)

' adjust it your code here
' you need to use monthSh

End Sub

答案 1 :(得分:1)

如果以后需要引用它们,则可以将您的工作簿存储在一个数组中。

' Array of Monthly Summary Workbook objects
Dim MonthSum(1 To 12) As Workbook

'Application.GetOpenFileName can return a path as a String, 
' or `False` if the user hits Cancel, so the variable should be a Variant
Dim File As Variant

' Set Current Directory
ChDir "C:\Users\stefan.bagnato\Desktop\Monthly Performance Summary"

' Loop through each month
Dim Month As Integer
For Month = 1 To 12

    ' Show Open File Dialog
    File = Application.GetOpenFilename

    ' Stop the loop if user hits cancel
    If File = False Then
        Exit For
    End If

    ' Open selected file, store Workbook object in the MonthSum array
    ' Workbooks.Open() returns the Worbook object of the opened file. 
    Set MonthSum(Month) = Workbooks.Open(File)

    ' Set Sheet Name, MonthName() returns the name of the month
    ' given its number. The true parameter indicates the name
    ' should be abbreviated to 3 letters
    ActiveSheet.Name = MonthName(Month,True)

    ' Call your custom macro
    Call ECHIBasicMonthlySummary

Next

' Do some other stuff

' Close the Workbooks
For Month = 1 To 12
    ' If User hit Cancel, MonthSum(Month) might not be a workbook object
    ' so check and make sure it's not Nothing before calling Close on it.
    If Not MonthSum(Month) Is Nothing Then 
        MonthSum(Month).Close SaveChanges:=True
    End If
Next