论坛首页 Microsoft .Net版

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

浏览 765 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-06-14 关键字: vba
在网上找到这么一段 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竟然早就支持了命名参数。
   
最后更新时间:2008-06-14
这个,vb6 也支持过程不带括号调用, 允许简单的反射调用callbyname, variant和对象迟绑定(即运行时才检查方法属性是否存在)...
   
0 请登录后投票
最后更新时间:2008-06-14
是命名参数,不过最初为写asp学vb的时候,想不出能用在哪里
   
0 请登录后投票
最后更新时间:2008-06-19
vb中的命名参数是为了其无限制的可选参数设立的。

即方法A([a],[b],[c])

我们可以用A(1,2)通过命名来传给abc中的任意的两个
   
0 请登录后投票
最后更新时间:2008-09-03
"vb中的命名参数是为了其无限制的可选参数设立的。"反对!
命名参数是为了提高代码的可读性。
如果一个函数,其单数为 10 个 int, 10 个 string, 你直接调用
f1(1,2,3,4,5,6,7,8,9,10,"s1","s2","s3","s4","s5","s6","s7","s8","s9","s10")
代码可读性差。如果该成
f1(test:=1, UserLevel:=2....); 代码可读性就很好。
如果函数参数数量很少就不要用命名参数。
   
0 请登录后投票
论坛首页 Microsoft .Net版

跳转论坛:
JavaEye推荐