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.

[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
[/code]

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代码中
[code]
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
MultiSelect:=True, Title:="Files to Merge")

....
Workbooks.Open FileName:=FilesToOpen(x)
[/code]

竟然大量使用了命名参数(如果我没有把VBA语法理解错误的话 -- 我对VBA语法不熟)。
Python支持命名参数。Ruby费了半天劲用 hashtable {} 模拟了命名参数。
想不到,VBA竟然早就支持了命名参数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值