简体   繁体   English

Excel VBA:在一个文件夹中的多个工作簿上循环工作表的简单副本

[英]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.

相关问题 Excel VBA。 从多个工作簿复制数据并粘贴到一个工作簿的同一工作表中 - Excel VBA. Copy data from multiple workbooks and paste in one workbook same worksheet VBA将工作表从一个工作簿复制到另一个文件夹中的所有工作簿 - VBA to copy worksheet from one workbook to all workbooks in another folder 使用VBA将一个工作表复制到多个相同的工作簿 - Copy one worksheet to multiple identical workbooks using VBA VBA:将特定范围从多个工作簿复制到一个工作表中 - VBA: Copy specific range from multiple workbooks into one worksheet 如何使用 VBA 从工作表创建多个 Excel 工作簿 - How to create multiple Excel workbooks from a worksheet with VBA VBA 将一个文件夹中多个工作簿的范围复制到另一个文件夹 - VBA to copy a range from multiple workbooks in a folder into another folder VBA将多个工作簿中的粘贴复制到同一文件夹中的1个中 - VBA copy Paste from multiple workbooks into 1 on same folder VBA将数据从多个工作簿复制到Mastercopy Excel - VBA to copy data from multiple workbooks to Mastercopy excel Excel VBA-将2个工作簿中的多个列复制到1个工作簿中 - Excel VBA - copy multiple columns from 2 workbooks into 1 workbook VBA:将文件夹中所有工作簿的范围复制到另一个工作簿中的工作表,其中包含每个 wb 的工作簿名称 - VBA: Copy a range from all workbooks in a folder to a worksheet in another workbook with workbook name from each wb included
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM