VBA竟然支持命名参数 -- 合并多个Excel workbooks为一个Excel文件

阅读更多
在网上找到这么一段 Excel 宏代码。
可以合并多个Excel Workbooks (即Excel文件) 合并成一个 Excel文件。


http://exceltips.vitalnews.com/Pages/T002409_Merging_Many_Workbooks.html

Merging Many Workbooks

Summary: Got a whole slew of workbooks that you need to merge together? You can do it manually, but it could take you all day. It’s much better to use a macro to do the merging, and you can be done in a few minutes. This tip explains how you can develop such a macro and how it could save you time. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

Joydip Das ran into a problem merging quite a few workbooks together. The majority of the workbooks--about 200 of them, all in a single folder--each contain a single worksheet, but some contain more. The worksheets form each of these workbooks needs to be added to a single workbook.
The easiest way to do merges of this magnitude--particularly if you have to do it often--is with a macro. The following macro displays a dialog box asking you to select the files to merge. (You can select multiple workbooks by holding down the Ctrl key as you click each one.) It loops thru the list you select, opening each one and moving all its worksheets to the end of the workbook with the code.

Sub CombineWorkbooks()


    Dim FilesToOpen
    Dim x As Integer

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    FilesToOpen = Application.GetOpenFilename _
      (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
      MultiSelect:=True, Title:="Files to Merge")

    If TypeName(FilesToOpen) = "Boolean" Then
        MsgBox "No Files were selected"
        GoTo ExitHandler
    End If

    x = 1
    While x <= UBound(FilesToOpen)
        Workbooks.Open FileName:=FilesToOpen(x)
        Sheets().Move After:=ThisWorkbook.Sheets _
          (ThisWorkbook.Sheets.Count)
        x = x + 1
    Wend

ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub


In the process of adding the worksheets to the end of the workbook, Excel will automatically append a (2), (3), etc. when duplicates worksheet names are detected. Any formulas in the book referring to other sheets will also be updated to reflect the new names.

------------------------------

上面的VBA代码中
    FilesToOpen = Application.GetOpenFilename _
      (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
      MultiSelect:=True, Title:="Files to Merge")

....
    Workbooks.Open FileName:=FilesToOpen(x)


竟然大量使用了命名参数(如果我没有把VBA语法理解错误的话 -- 我对VBA语法不熟)。
Python支持命名参数。Ruby费了半天劲用 hashtable {} 模拟了命名参数。
想不到,VBA竟然早就支持了命名参数。

你可能感兴趣的:(Excel,VBA,VB,Microsoft,Python)