Vba 如何基于工作簿名称将多个工作簿合并为一个工作簿
我正在为经理准备报告。我有多个excel文件(始终带有一张工作表),我需要根据原始文件的名称将工作表合并到一个带有多张工作表的工作簿(与原始工作簿的名称相同) 我需要它检查文件名,并根据前四个字符合并那些具有相同字符的文件。然后我想用这四个字符的名称保存新工作簿 例如,我在一个文件夹中有这些文件->Vba 如何基于工作簿名称将多个工作簿合并为一个工作簿,vba,excel,Vba,Excel,我正在为经理准备报告。我有多个excel文件(始终带有一张工作表),我需要根据原始文件的名称将工作表合并到一个带有多张工作表的工作簿(与原始工作簿的名称相同) 我需要它检查文件名,并根据前四个字符合并那些具有相同字符的文件。然后我想用这四个字符的名称保存新工作簿 例如,我在一个文件夹中有这些文件-> 1111_AB_ABC 1111_BC_AAA 1222_CD_BBB 1222_KL_XXX 1222_HJ_OPD 1666_HA_BNN 等等(像这样的文件大约有300个,大多数有
1111_AB_ABC
1111_BC_AAA
1222_CD_BBB
1222_KL_XXX
1222_HJ_OPD
1666_HA_BNN
等等(像这样的文件大约有300个,大多数有3个文件在开始时有相同的数字,但有少数几个数字我有四个或五个文件)。
有没有可能做到这一点
我找到了一些关于将工作簿合并到一个主文件的帖子,但是没有关于根据文件名合并文件的帖子。我将给你一些高级的想法 为了实现你想要的,你必须:
- 解析整个目录并检索它包含的所有文件
- 从文件名中提取子字符串
- 使用给定名称创建新工作簿
- 保存工作簿
'要创建新工作簿,您可以使用:Dim w as Workbook ' workbook that will contain the sheets Dim tempWork as Workbook Dim rootFolder ' the folder containing your files Dim fs ' represent FileSystem object Dim folder ' represent folder object Dim files ' represent all files in a folder Dim file ' represent a file object rootFolder = "C:\path\To\my\folder" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(rootFolder) Set files = folder.Files ' retrieve only files in rootFolder For Each file In files ' here "file" represent a file in rootFolder fileName = file.Name firstFourChar = Mid(fileName,1,4) ' with Mid buil-in function you extract sub string ' your business logic goes here next
'用于保存工作簿:Dim w as Workbook Set w = Workbooks.Add
w.save ("path where save")
'用于打开工作簿:Set w = Workbooks.Open(rootFolder & "\" & file.Name)
w.save ("path where save")
Set w = Workbooks.Open(rootFolder & "\" & file.Name)
我会给你一些高层次的想法 为了实现你想要的,你必须:
- 解析整个目录并检索它包含的所有文件
- 从文件名中提取子字符串
- 使用给定名称创建新工作簿
- 保存工作簿
'要创建新工作簿,您可以使用:Dim w as Workbook ' workbook that will contain the sheets Dim tempWork as Workbook Dim rootFolder ' the folder containing your files Dim fs ' represent FileSystem object Dim folder ' represent folder object Dim files ' represent all files in a folder Dim file ' represent a file object rootFolder = "C:\path\To\my\folder" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(rootFolder) Set files = folder.Files ' retrieve only files in rootFolder For Each file In files ' here "file" represent a file in rootFolder fileName = file.Name firstFourChar = Mid(fileName,1,4) ' with Mid buil-in function you extract sub string ' your business logic goes here next
'用于保存工作簿:Dim w as Workbook Set w = Workbooks.Add
w.save ("path where save")
'用于打开工作簿:Set w = Workbooks.Open(rootFolder & "\" & file.Name)
w.save ("path where save")
Set w = Workbooks.Open(rootFolder & "\" & file.Name)
下面是执行此操作的代码 作为一个参数,您需要传递到源文件夹和目标文件夹的路径,结果文件应保存在该文件夹中 请注意,文件夹路径的末尾必须包含斜杠。您可以稍后修改此函数以检查文件夹路径的末尾是否包含斜杠,如果不包含斜杠,则自动添加斜杠
Sub test(sourceFolder As String, destinationFolder As String)
Const TO_DELETE_SHEET_NAME As String = "toBeDeleted"
'------------------------------------------------------------------
Dim settingSheetsNumber As Integer
Dim settingDisplayAlerts As Boolean
Dim dict As Object
Dim wkbSource As Excel.Workbook
Dim wks As Excel.Worksheet
Dim filepath As String
Dim code As String * 4
Dim wkbDestination As Excel.Workbook
Dim varKey As Variant
'------------------------------------------------------------------
'Change [SheetsInNewWorkbook] setting of Excel.Application object to
'create new workbooks with a single sheet only.
With Excel.Application
settingDisplayAlerts = .DisplayAlerts
settingSheetsNumber = .SheetsInNewWorkbook
.SheetsInNewWorkbook = 1
.DisplayAlerts = False
End With
Set dict = VBA.CreateObject("Scripting.Dictionary")
filepath = Dir(sourceFolder)
'Loop through each Excel file in folder
Do While filepath <> ""
If VBA.Right$(filepath, 5) = ".xlsx" Then
Set wkbSource = Excel.Workbooks.Open(sourceFolder & filepath)
Set wks = wkbSource.Worksheets(1)
code = VBA.Left$(wkbSource.Name, 4)
'If this code doesn't exist in the dictionary yet, add it.
If Not dict.exists(code) Then
Set wkbDestination = Excel.Workbooks.Add
wkbDestination.Worksheets(1).Name = TO_DELETE_SHEET_NAME
Call dict.Add(code, wkbDestination)
Else
Set wkbDestination = dict.Item(code)
End If
Call wks.Copy(Before:=wkbDestination.Worksheets(1))
wkbDestination.Worksheets(1).Name = VBA.Mid$(filepath, 6)
Call wkbSource.Close(False)
End If
filepath = Dir
Loop
'Save newly created files.
For Each varKey In dict.keys
Set wkbDestination = dict.Item(varKey)
'Remove empty sheet.
Set wks = Nothing
On Error Resume Next
Set wks = wkbDestination.Worksheets(TO_DELETE_SHEET_NAME)
On Error GoTo 0
If Not wks Is Nothing Then wks.Delete
Call wkbDestination.SaveAs(Filename:=destinationFolder & varKey & ".xlsx")
Next varKey
'Restore Excel.Application settings.
With Excel.Application
.DisplayAlerts = settingDisplayAlerts
.SheetsInNewWorkbook = settingSheetsNumber
End With
End Sub
子测试(sourceFolder作为字符串,destinationFolder作为字符串)
要删除的常数\u工作表\u名称为String=“toBeDeleted”
'------------------------------------------------------------------
尺寸设置表编号为整数
Dim设置将警报显示为布尔值
作为对象的Dim dict
将wkbSource设置为Excel.工作簿
Dim以Excel格式工作。工作表
将文件路径设置为字符串
Dim代码为字符串*4
将wkbDestination设置为Excel.工作簿
Dim varKey作为变体
'------------------------------------------------------------------
'将Excel.Application对象的[SheetwWorkbook]设置更改为
'仅使用单个工作表创建新工作簿。
使用Excel.Application
设置DisplayAlerts=.DisplayAlerts
设置SheetsNumber=.SheetsWorkbook
.wWorkbook=1
.DisplayAlerts=False
以
Set dict=VBA.CreateObject(“Scripting.Dictionary”)
filepath=Dir(sourceFolder)
'循环浏览文件夹中的每个Excel文件
在文件路径“”时执行此操作
如果VBA.Right$(文件路径,5)=“.xlsx”,则
设置wkbSource=Excel.Workbooks.Open(sourceFolder&filepath)
设置wks=wkbSource.Worksheets(1)
代码=VBA.Left$(wkbSource.Name,4)
'如果字典中还不存在此代码,请添加它。
如果不存在dict.exists(代码),则
设置wkbDestination=Excel.Workbooks.Add
wkbDestination.Worksheets(1).Name=删除工作表
调用dict.Add(代码,wkbDestination)
其他的
Set wkbDestination=dict.Item(代码)
如果结束
调用wks.Copy(之前:=wkbDestination.Worksheets(1))
wkbDestination.Worksheets(1).Name=VBA.Mid$(文件路径,6)
调用wkbSource.Close(False)
如果结束
filepath=Dir
环
'保存新创建的文件。
对于dict.keys中的每个varKey
Set wkbDestination=dict.Item(varKey)
'删除空表。
设为零
出错时继续下一步
设置wks=wkbDestination.Worksheets(删除工作表名)
错误转到0
如果不是wks,则为wks。删除
调用wkbDestination.SaveAs(文件名:=destinationFolder&varKey&“.xlsx”)
下一个瓦基
'还原Excel。应用程序设置。
使用Excel.Application
.DisplayAlerts=设置DisplayAlerts
.sheetsineworkbook=设置sheetsnumber
以
端接头
下面是执行此操作的代码
作为一个参数,您需要传递到源文件夹和目标文件夹的路径,结果文件应保存在该文件夹中
请注意,文件夹路径的末尾必须包含斜杠。您可以稍后修改此函数以检查文件夹路径的末尾是否包含斜杠,如果不包含斜杠,则自动添加斜杠
Sub test(sourceFolder As String, destinationFolder As String)
Const TO_DELETE_SHEET_NAME As String = "toBeDeleted"
'------------------------------------------------------------------
Dim settingSheetsNumber As Integer
Dim settingDisplayAlerts As Boolean
Dim dict As Object
Dim wkbSource As Excel.Workbook
Dim wks As Excel.Worksheet
Dim filepath As String
Dim code As String * 4
Dim wkbDestination As Excel.Workbook
Dim varKey As Variant
'------------------------------------------------------------------
'Change [SheetsInNewWorkbook] setting of Excel.Application object to
'create new workbooks with a single sheet only.
With Excel.Application
settingDisplayAlerts = .DisplayAlerts
settingSheetsNumber = .SheetsInNewWorkbook
.SheetsInNewWorkbook = 1
.DisplayAlerts = False
End With
Set dict = VBA.CreateObject("Scripting.Dictionary")
filepath = Dir(sourceFolder)
'Loop through each Excel file in folder
Do While filepath <> ""
If VBA.Right$(filepath, 5) = ".xlsx" Then
Set wkbSource = Excel.Workbooks.Open(sourceFolder & filepath)
Set wks = wkbSource.Worksheets(1)
code = VBA.Left$(wkbSource.Name, 4)
'If this code doesn't exist in the dictionary yet, add it.
If Not dict.exists(code) Then
Set wkbDestination = Excel.Workbooks.Add
wkbDestination.Worksheets(1).Name = TO_DELETE_SHEET_NAME
Call dict.Add(code, wkbDestination)
Else
Set wkbDestination = dict.Item(code)
End If
Call wks.Copy(Before:=wkbDestination.Worksheets(1))
wkbDestination.Worksheets(1).Name = VBA.Mid$(filepath, 6)
Call wkbSource.Close(False)
End If
filepath = Dir
Loop
'Save newly created files.
For Each varKey In dict.keys
Set wkbDestination = dict.Item(varKey)
'Remove empty sheet.
Set wks = Nothing
On Error Resume Next
Set wks = wkbDestination.Worksheets(TO_DELETE_SHEET_NAME)
On Error GoTo 0
If Not wks Is Nothing Then wks.Delete
Call wkbDestination.SaveAs(Filename:=destinationFolder & varKey & ".xlsx")
Next varKey
'Restore Excel.Application settings.
With Excel.Application
.DisplayAlerts = settingDisplayAlerts
.SheetsInNewWorkbook = settingSheetsNumber
End With
End Sub
子测试(sourceFolder作为字符串,destinationFolder作为字符串)
要删除的常数\u工作表\u名称为String=“toBeDeleted”
'------------------------------------------------------------------
尺寸设置表编号为整数
Dim设置将警报显示为布尔值
作为对象的Dim dict
将wkbSource设置为Excel.工作簿
Dim以Excel格式工作。工作表
将文件路径设置为字符串
Dim代码为字符串*4
将wkbDestination设置为Excel.工作簿
Dim varKey作为变体
'------------------------------------------------------------------
'将Excel.Application对象的[SheetwWorkbook]设置更改为
'仅使用单个工作表创建新工作簿。
使用Excel.Application
设置DisplayAlerts=.DisplayAlerts
设置SheetsNumber=.SheetsWorkbook
.wWorkbook=1
.DisplayAlerts=False
以
口述