简体   繁体   English

如何使用 VBA 引用另一个工作簿中路径和名称的单元格打开多个 Excel 工作簿

[英]How to Open Multiple Excel workbooks using VBA referencing cells in another workbook for the path and name

How can I open multiple Excel Workbooks one at a time using VBA?如何使用 VBA 一次打开多个 Excel 工作簿? I created 3200 Similar Workbooks and now I need to change some formatting.我创建了 3200 个类似的工作簿,现在我需要更改一些格式。 I would like to create a Loop function to open the first workbook based on a master list starting on Line 5. The general Path is the same for all but each workbook is in it's own folder.我想创建一个循环函数来基于从第 5 行开始的主列表打开第一个工作簿。所有工作簿的一般路径都相同,但每个工作簿都在它自己的文件夹中。 The folder was created based on a column and the Workbook Document was created using another Column.该文件夹是基于一列创建的,而工作簿文档是使用另一列创建的。

Sub Macro2()

Application.ScreenUpdating = False

Dim sPath As String
Dim sFile As String
Dim wb As Workbook


FileName1 = Range("A5")
FileName2 = Range("K5")

sPath = "E:\PARENTFOLDER\theFILES\"
sFile = sPath & FileName1 & "\" & FileName2 & ".xlsm"

Set wb = Workbooks.Open(sFile)

''FORMAT TXT
Range("J10").Select 'clamp design
Selection.Font.Italic = True

Range("I3").Select 'utility parent 
Selection.Font.Bold = True

''RENAME COLUMN HEADERS
Range("G19").Select
ActiveCell.FormulaR1C1 = "TXTa?"
    Selection.Font.Bold = True
Columns("G:G").ColumnWidth = 18.57

Range("H19").Select
ActiveCell.FormulaR1C1 = "TXTb"
    Selection.Font.Bold = True
Columns("H:H").ColumnWidth = 23.29

''COPY
Range("I4").Select
Application.WindowState = xlNormal
Windows("theFILE 1.1.xlsm").Activate
Range("D5").Select
Selection.Copy
Windows(FileName2).Activate
ActiveSheet.Paste
Application.CutCopyMode = False
Range("I4").Select

End Sub

Then I need to open the next workbook 1 line down and so on until empty cells.然后我需要打开下一个工作簿 1 行,依此类推,直到空单元格。 A5 becomes A6 While K5 becomes K6 and so on and so forth. A5 变成 A6 而 K5 变成 K6 等等。

I am aware I cheated and used Excel's Record Macro Tool.我知道我作弊并使用了 Excel 的记录宏工具。

Any and all Help is greatly appreciated.非常感谢任何和所有帮助。

Thanks Everyone for all your Help.感谢大家的帮助。 I seem to have figured it out.我好像想通了。 Here's what I got if anyone else is wondering.如果其他人想知道,这就是我得到的。

Sub Macro2()

Application.ScreenUpdating = False

Dim sFile As String
Dim wb As Workbook
Dim FileName1 As String
Dim FileName2 As String
Dim wksSource As Worksheet
Const scWkbSourceName As String = "theFILE 1.1.xlsm"

Set wkbSource = Workbooks(scWkbSourceName)
Set wksSource = wkbSource.Sheets("Sheet1") ' Replace Sheet1 with the sheet name

Const wsOriginalBook As String = "theFILE 1.1.xlsm"
Const sPath As String = "E:\ParentFolder\theFILES\"

SourceRow = 5

Do While Cells(SourceRow, "D").Value <> ""

FileName1 = wksSource.Range("A" & SourceRow).Value
FileName2 = wksSource.Range("K" & SourceRow).Value

sFile = sPath & FileName1 & "\" & FileName2 & ".xlsm"

Set wb = Workbooks.Open(sFile)

''FORMAT TXT
Range("J10").Select
Selection.Font.Italic = True

Range("I3").Select 
Selection.Font.Bold = True

''RENAME COLUMN HEADERS
Range("G19").Select
ActiveCell.FormulaR1C1 = "WHO MADE THIS?"
    Selection.Font.Bold = True
Columns("G:G").ColumnWidth = 18.57

Range("H19").Select
ActiveCell.FormulaR1C1 = "MANU"
    Selection.Font.Bold = True
Columns("H:H").ColumnWidth = 23.29

''ADD COMPANY
Range("I4").Select
Application.WindowState = xlNormal
Windows("theFILE 1.1.xlsm").Activate
Range("D5").Select
Selection.Copy
Windows(FileName2).Activate
ActiveSheet.Paste
Application.CutCopyMode = False
Range("I4").Select


'''CLOSE WORKBOOK W/O BEFORE SAVE FUNCTION
Application.EnableEvents = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.EnableEvents = True

SourceRow = SourceRow + 1 ' Move down 1 row for source sheet

Loop

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 vba 打开名称更改的 excel 工作簿 - How to open an excel workbook which has a changing name using vba Excel VBA:将多个工作簿合并为一个工作簿 - Excel VBA: Combine multiple workbooks into one workbook 如何使用vba for excel获取另一个工作簿中定义名称的值 - how to get the value of a defined name in another workbook, using vba for excel 使用VBA在Excel中引用变量工作簿 - Referencing variable workbooks in Excel using VBA 如何在Excel中使用其目录路径而不是使用vba进行代码编码来关闭打开的工作簿? - How can I code to close an open workbook using its directory path instead of its name using vba in excel? VBA - 如何在excel工作簿(工作簿名称更改)之间复制单元格? - VBA - How to copy cells between excel workbooks (where workbook names change)? Excel VBA,使用FileDialog打开多个工作簿并引用它们 - Excel VBA, using FileDialog to open multiple workbooks and reference them 如何将单元格用于超链接目标/路径和工作簿名称 VBA? - how to use cells for a Hyperlink destination/path and workbook name VBA? 将单元格从一个工作簿复制到多个工作簿excel - Copy cells from one workbook into multiple workbooks excel Excel VBA将多个工作簿中的多个工作表合并到一个工作簿中 - excel vba merge multiple sheets from multiple workbooks into one workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM