我是靠谱客的博主 含糊水壶,最近开发中收集的这篇文章主要介绍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竟然早就支持了命名参数。

最后

以上就是含糊水壶为你收集整理的VBA竟然支持命名参数 -- 合并多个Excel workbooks为一个Excel文件的全部内容,希望文章能够帮你解决VBA竟然支持命名参数 -- 合并多个Excel workbooks为一个Excel文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部