Excel 我的当前代码复制选项卡,但不复制数据

Excel 我的当前代码复制选项卡,但不复制数据,excel,vba,Excel,Vba,我需要一个宏,它将从文件夹中的所有文件中复制选项卡,并将它们合并到一个工作簿中。我有一个当前的代码,它将拉标签,但它们返回空白。我需要将原始文件中的所有数据合并到一个文件中。有人能帮我解决这个问题吗?先谢谢你 Sub CreateSheet(worksheetname) With ThisWorkbook .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = worksheetname End With End Sub Sub Works

我需要一个宏,它将从文件夹中的所有文件中复制选项卡,并将它们合并到一个工作簿中。我有一个当前的代码,它将拉标签,但它们返回空白。我需要将原始文件中的所有数据合并到一个文件中。有人能帮我解决这个问题吗?先谢谢你

Sub CreateSheet(worksheetname)
With ThisWorkbook
    .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = worksheetname
End With
End Sub


Sub WorksheetLoop()

     Dim WS_Count As Integer
     Dim I As Integer

     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = ActiveWorkbook.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count

        ' Insert your code here.
        ' The following line shows how to reference a sheet within
        ' the loop by displaying the worksheet name in a dialog box.
        CreateSheet (ActiveWorkbook.Worksheets(I).Name)

     Next I

  End Sub


Sub LoopAllExcelFilesInFolder()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
 myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
  Set wb = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
  DoEvents
Call WorksheetLoop
'Change First Worksheet's Background Fill Blue
'wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)


'Save and Close Workbook
  wb.Close SaveChanges:=True

'Ensure Workbook has closed before moving on to next line of code
  DoEvents

'Get next file name
  myFile = Dir
Loop


ResetSettings:
'Reset Macro Optimization Settings
 Application.EnableEvents = True
 Application.Calculation = xlCalculationAutomatic
 Application.ScreenUpdating = True

End Sub
子创建工作表(工作表名称)
使用此工作簿
.Sheets.Add(后面:=.Sheets(.Sheets.Count)).Name=工作表名称
以
端接头
子工作表loop()
Dim WS_计数为整数
作为整数的Dim I
'将WS_Count设置为活动工作表中的工作表数
'工作簿。
WS\u Count=ActiveWorkbook.Worksheets.Count
'开始循环。
对于I=1到WS\u计数
'在此处插入您的代码。
'下一行显示如何在中引用图纸
'通过在对话框中显示工作表名称来执行循环。
CreateSheet(ActiveWorkbook.Worksheets(I).Name)
接下来我
端接头
子循环AllExcelFileSinFolder()
将wb设置为工作簿
将myPath设置为字符串
将myFile设置为字符串
Dim myExtension作为字符串
Dim FldrPicker As FILE对话框
'优化宏速度
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
'从用户检索目标文件夹路径
Set FldrPicker=Application.FileDialog(msoFileDialogFolderPicker)
用FldrPicker
.Title=“选择目标文件夹”
.AllowMultiSelect=False
如果.Show-1,则转到下一个代码
myPath=.SelectedItems(1)和“\”
以
"如果取消,
下一个代码:
myPath=myPath
如果myPath=”“,则转到重置设置
'目标文件扩展名(必须包含通配符“*”)
myExtension=“*.xls*”
'具有结束扩展名的目标路径
myFile=Dir(myPath&myExtension)
'循环浏览文件夹中的每个Excel文件
当我的文件“”时执行此操作
'将变量设置为等于打开的工作簿
设置wb=Workbooks.Open(文件名:=myPath&myFile)
'在继续下一行代码之前,确保工作簿已打开
多芬特
调用工作表loop
'更改第一个工作表的背景填充为蓝色
'wb.工作表(1).范围(“A1:Z1”).Interior.Color=RGB(51,98,174)
'保存并关闭工作簿
wb.Close SaveChanges:=真
'在继续下一行代码之前,确保工作簿已关闭
多芬特
'获取下一个文件名
myFile=Dir
环
重置设置:
'重置宏优化设置
Application.EnableEvents=True
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
端接头

用下面的代码替换
工作表loop
过程。这将把引用的(
OpenedBook
)工作簿中的每张工作表复制到
thiswoolk

Sub WorksheetLoop(OpenedBook As Workbook)

    Dim wrksht As Worksheet

    With ThisWorkbook
        For Each wrksht In OpenedBook.Worksheets
            wrksht.Copy Before:=.Worksheets(.Worksheets.Count)
        Next wrksht
    End With

End Sub
LoopAllExcelFilesInFolder
过程中更改此代码行:

Call WorksheetLoop  

如果您正在打开的工作簿在打开事件中包含代码,您可能需要添加(我知道有比这更好的方法,我现在想不起来):


我认为您不能使用自定义名称创建新工作表,只能使用默认名称。但您可以立即重命名它

试试这个:

With ThisWorkbook
    set NewSheet = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 
    NewSheet.Name = worksheetname
End With

可以在子程序中设置参数并使用copy命令

Sub WorksheetLoop(WB As Workbook)

     Dim WS_Count As Integer
     Dim I As Integer
     Dim myWB As Workbook

     Set myWB = ThisWorkbook
     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = WB.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count

        ' Insert your code here.
        ' The following line shows how to reference a sheet within
        ' the loop by displaying the worksheet name in a dialog box.
        'CreateSheet (ActiveWorkbook.Worksheets(I).Name)
        WB.Worksheets(I).Copy after:=myWB.Sheets(myWB.Sheets.Count)

     Next I

  End Sub


Sub LoopAllExcelFilesInFolder()

Dim WB As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
 myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
  Set WB = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
  DoEvents
 WorksheetLoop WB
'Change First Worksheet's Background Fill Blue
'wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)


'Save and Close Workbook
  WB.Close SaveChanges:=True

'Ensure Workbook has closed before moving on to next line of code
  DoEvents

'Get next file name
  myFile = Dir
Loop


ResetSettings:
'Reset Macro Optimization Settings
 Application.EnableEvents = True
 Application.Calculation = xlCalculationAutomatic
 Application.ScreenUpdating = True

End Sub
子工作表loop(WB作为工作簿)
Dim WS_计数为整数
作为整数的Dim I
将myWB设置为工作簿
设置myWB=ThisWorkbook
'将WS_Count设置为活动工作表中的工作表数
'工作簿。
WS_Count=WB.Worksheets.Count
'开始循环。
对于I=1到WS\u计数
'在此处插入您的代码。
'下一行显示如何在中引用图纸
'通过在对话框中显示工作表名称来执行循环。
'CreateSheet(ActiveWorkbook.Worksheets(I).Name)
WB.工作表(I).复制时间:=myWB.工作表(myWB.Sheets.Count)
接下来我
端接头
子循环AllExcelFileSinFolder()
将WB设置为工作簿
将myPath设置为字符串
将myFile设置为字符串
Dim myExtension作为字符串
Dim FldrPicker As FILE对话框
'优化宏速度
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
'从用户检索目标文件夹路径
Set FldrPicker=Application.FileDialog(msoFileDialogFolderPicker)
用FldrPicker
.Title=“选择目标文件夹”
.AllowMultiSelect=False
如果.Show-1,则转到下一个代码
myPath=.SelectedItems(1)和“\”
以
"如果取消,
下一个代码:
myPath=myPath
如果myPath=”“,则转到重置设置
'目标文件扩展名(必须包含通配符“*”)
myExtension=“*.xls*”
'具有结束扩展名的目标路径
myFile=Dir(myPath&myExtension)
'循环浏览文件夹中的每个Excel文件
当我的文件“”时执行此操作
'将变量设置为等于打开的工作簿
设置WB=Workbooks.Open(文件名:=myPath&myFile)
'在继续下一行代码之前,确保工作簿已打开
多芬特
工作表循环WB
'更改第一个工作表的背景填充为蓝色
'wb.工作表(1).范围(“A1:Z1”).Interior.Color=RGB(51,98,174)
'保存并关闭工作簿
WB.Close SaveChanges:=真
'在继续下一行代码之前,确保工作簿已关闭
多芬特
'获取下一个文件名
myFile=Dir
环
重置设置:
'重置宏优化设置
Application.EnableEvents=True
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
端接头

您不会将工作表复制到代码中的任何地方,只复制工作表名称,这就是工作表为空的原因。查看我希望我的选项卡保持相同的名称,并且它们通常会有所不同。此网站仅将工作表称为“工作表1”等。有没有办法避免这种情况?您不是在复制工作表,而是在使用
.sheets.Add(在:=.sheets(.sheets.Count)之后)创建新的工作表。Name=worksheetname
。对于您正在尝试执行的操作-在您尝试从中复制的所有文件中是否会有重复的工作表名称?不,所有工作表名称都是重复的different@ShawnV.Wilson您可以创建一个工作表并将其命名在同一行上,也可以添加一个工作表并设置一个变量reference
With ThisWorkbook
    set NewSheet = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 
    NewSheet.Name = worksheetname
End With
Sub WorksheetLoop(WB As Workbook)

     Dim WS_Count As Integer
     Dim I As Integer
     Dim myWB As Workbook

     Set myWB = ThisWorkbook
     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = WB.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count

        ' Insert your code here.
        ' The following line shows how to reference a sheet within
        ' the loop by displaying the worksheet name in a dialog box.
        'CreateSheet (ActiveWorkbook.Worksheets(I).Name)
        WB.Worksheets(I).Copy after:=myWB.Sheets(myWB.Sheets.Count)

     Next I

  End Sub


Sub LoopAllExcelFilesInFolder()

Dim WB As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
 myExtension = "*.xls*"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
  Set WB = Workbooks.Open(Filename:=myPath & myFile)

'Ensure Workbook has opened before moving on to next line of code
  DoEvents
 WorksheetLoop WB
'Change First Worksheet's Background Fill Blue
'wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)


'Save and Close Workbook
  WB.Close SaveChanges:=True

'Ensure Workbook has closed before moving on to next line of code
  DoEvents

'Get next file name
  myFile = Dir
Loop


ResetSettings:
'Reset Macro Optimization Settings
 Application.EnableEvents = True
 Application.Calculation = xlCalculationAutomatic
 Application.ScreenUpdating = True

End Sub