select一个复制范围的特定标签

我试图在一个文件夹中打开几个文件,转到每个电子表格中名为"OTC records"的特定表格,并将所有数据复制到一个名为"OTC records"选项卡上。

我下面的macros似乎打开文件确定和堆叠的数据,但只为文件中的第一张。

我想我需要改变复制范围行[Set CopyRng = Wkb.Sheets(1)]指向一个表名称,但我不知道该怎么做。 我试图改变这个指向表[通过改变行 – Set CopyRng = Wkb.Sheets("OTC records") ],但它根本不爱它。

任何人都可以请帮忙?

 Sub MergeFiles1() Dim path As String, ThisWB As String, lngFilecounter As Long Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet Dim Filename As String, Wkb As Workbook Dim CopyRng As Range, Dest As Range Dim RowofCopySheet As Integer RowofCopySheet = 2 ThisWB = ActiveWorkbook.Name path = ("F:\WIN7PROFILE\Desktop\Recs") Application.EnableEvents = False Application.ScreenUpdating = False Set shtDest = ActiveWorkbook.Sheets("OTC records") Filename = Dir(path & "\*.xls", vbNormal) If Len(Filename) = 0 Then Exit Sub Do Until Filename = vbNullString If Not Filename = ThisWB Then Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename) Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count)) Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1) CopyRng.Copy Dest Wkb.Close False End If Filename = Dir() Loop Range("A1").Select Application.EnableEvents = True Application.ScreenUpdating = True MsgBox "Done!" End Sub 

我已经将代码更改为下面,但无法使循环工作。 你能帮忙吗?

Sub MergeFiles1()Dim path As String,ThisWB As String,lngFilecounter As Long Dim wbDest As Workbook,shtDest As Worksheet,ws As Worksheet Dim Filename As String,Wkb As Workbook Dim CopyRng As Range,Dest As Range Dim RowofCopySheet As Integer

 RowofCopySheet = 2 ThisWB = ActiveWorkbook.Name path = ("F:\WIN7PROFILE\Desktop\Recs") Application.EnableEvents = False Application.ScreenUpdating = False Set shtDest = ActiveWorkbook.Sheets("OTC records") Filename = Dir(path & "\*.xls", vbNormal) If Len(Filename) = 0 Then Exit Sub Do Until Filename = vbNullString If Not Filename = ThisWB Then Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename) WS_Count = ActiveWorkbook.Worksheets.Count For I = 1 To WS_Count if Wkb.Worksheets(I).Name = "OTC Records" idx = I End If Next I Set CopyRng = Wkb.Sheets(idx).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count)) Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1) CopyRng.Copy Dest Wkb.Close False End If Filename = Dir() Loop Range("A1").Select Application.EnableEvents = True Application.ScreenUpdating = True MsgBox "Done!" 

结束小组

尝试在另一个工作簿中循环查找特定的工作簿:

 WS_Count = ActiveWorkbook.Worksheets.Count For I = 1 To WS_Count if Wkb.Worksheets(I).Name = "OTC Records" idx = I ' idx would hold index of the found sheet end if Next I 

然后你可以通过访问该工作表

 Wkb.Sheets(idx) 

信息来自: https : //support.microsoft.com/en-us/kb/142126