[英]Excel VBA : Looping a simple copy of a worksheet over multiple workbooks in a folder
I'm attempting to apply a macro that would copy and paste one specific worksheet (call the title of that worksheet "x") from one workBOOK ("x1") , onto a master workBOOK (call that workBOOK "xmaster"), after it copy and pastes the worksheet from workbook x1 it should also rename the title of the worksheet "x" to cell B3. 我正在尝试应用一个宏,该宏将一个工作簿(“ x1”)中的一个特定工作表(称为该工作表的标题“ x”)复制并粘贴到主工作簿(称为该工作簿“ xmaster”)上,之后它从工作簿x1复制并粘贴工作表,还应该将工作表的标题“ x”重命名为单元格B3。 This should be done before it moves to the next workbook.
这应该在移至下一个工作簿之前完成。
It would need to do this for workBOOK x1 through, say, x100. 对于workBOOK x1至x100,将需要执行此操作。 I cannot refer to the workbook by name though, because they are each named a string of text that is in no real sortable method.
但是,我无法按名称引用工作簿,因为它们每个都被命名为一个字符串,但没有真正的可排序方法。
This code I know works, copying "x" from "x1" to "xmaster", along with renaming the sheet, and breaking the links, is the following: 我知道的这段代码有效,将“ x”从“ x1”复制到“ xmaster”,以及重命名工作表并断开链接,如下所示:
Sub CombineCapExFiles()
Sheets("Capital-Projects over 3K").Move After:=Workbooks("CapEx Master File.xlsm").Sheets _
(3)
ActiveSheet.Name = Range("B3").Value
Application.DisplayAlerts = False
For Each wb In Application.Workbooks
Select Case wb.Name
Case ThisWorkbook.Name, "CapEx Master File.xlsm"
' do nothing
Case Else
wb.Close
End Select
Next wb
Application.DisplayAlerts = True
End Sub
The Activate Previous window isn't working, also not sure how to fix that portion of it. “激活上一个”窗口不起作用,也不确定如何修复该部分。
I'm not sure how to build this to loop through all workBOOKs in the directory, however. 我不确定如何构建它来遍历目录中的所有workBOOK。
Should I use this:? 我应该使用这个吗?
MyPath = "C:\directory here"
strFilename = Dir(MyPath & "\*.xlsx", vbNormal) 'change to xlsm if needed ?
If Len(strFilename) = 0 Then Exit Sub ' exit if no files in folder
Do Until strFilename = ""
'Your code here
strFilename = Dir()
Loop
An additional constraint is that it needs to not run the macro on xmaster (it will have an error because it will not have the sheet "x" which will be renamed from the previous workbooks.) 另一个限制是它不需要在xmaster上运行宏(这将发生错误,因为它将没有工作表“ x”,该工作表将从以前的工作簿中重命名。)
Thanks! 谢谢! Matthew
马修
like this? 像这样? (not tested)
(未测试)
Option Explicit
Sub LoopFiles()
Dim strDir As String, strFileName As String
Dim wbCopyBook As Workbook
Dim wbNewBook As Workbook
Dim wbname as String
strDir = "C:\"
strFileName = Dir(strDir & "*.xlsx")
Set wbNewBook = Workbooks.Add 'instead of adding a workbook, set = to the name of your master workbook
wbname = ThisWorkbook.FullName
Do While strFileName <> ""
Set wbCopyBook = Workbooks.Open(strDir & strFileName)
If wbCopyBook.FullName <> wbname Then
wbCopyBook.Sheets(1).Copy Before:=wbNewBook.Sheets(1)
wbCopyBook.Close False
strFileName = Dir()
Else
strFileName = Dir()
End If
Loop
End Sub
This bit will work to avoid running the macro on xmaster. 该位可以避免在xmaster上运行宏。
xmaster = "filename for xmaster"
MyPath = "C:\directory here"
strFilename = Dir(MyPath & "\*.xls*", vbNormal) 'this will get .xls, .xlsx, .xlsm and .xlsb files
If Len(strFilename) = 0 Then Exit Sub ' exit if no files in folder
Do Until strFilename = ""
If strFileName = xmaster Then ' skip the xmaster file
strFilename = Dir()
End If
'Your code here
strFilename = Dir()
Loop
I can't help on the other part though. 但是我在另一方面还是帮不上忙。 I don't see any Activate Previous window part in your code.
在您的代码中没有看到“激活上一个”窗口部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.