[英]Pick a specific tab for a copy range
I am trying to open several files in one folder, go to a specific sheet in each spreadsheet entitled "OTC records"
and copy all that data onto one tab called "OTC records"
. 我试图在一个文件夹中打开多个文件,转到每个电子表格中名为"OTC records"
的特定工作表,然后将所有数据复制到一个称为"OTC records"
选项卡上。
The macro I have below seems to open the files ok and stack the data but only for the first sheet in the files. 我在下面的宏似乎可以正常打开文件并堆叠数据,但仅适用于文件中的第一张纸。
I think I need to change the copy range line [Set CopyRng = Wkb.Sheets(1)]
to point to a sheet name but I don't know how to do that. 我想我需要更改复制范围行[Set CopyRng = Wkb.Sheets(1)]
指向工作表名称,但我不知道该怎么做。 I tried to change this to point to the sheet [by changing the line to - Set CopyRng = Wkb.Sheets("OTC records")
] but it is not loving it at all. 我试图将其更改为指向工作表[通过将行更改为Set CopyRng = Wkb.Sheets("OTC records")
],但它根本不喜欢它。
Can anyone please help? 谁能帮忙吗?
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
I've change the code to the below but am not able to get the looping to work. 我将代码更改为以下内容,但无法使循环正常工作。 Would you be able to help? 你能帮忙吗?
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 Sub MergeFiles1()Dim路径作为字符串,ThisWB作为字符串,lngFilecounter作为长尺寸wbDest作为工作簿,shtDest作为工作表,ws作为工作表Dim文件名作为字符串,Wkb作为工作簿Dim CopyRng作为范围,目标作为范围Dim RowofCopySheet作为整数
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!"
End Sub 结束子
Try to loop through sheets in another workbook to find specific one: 尝试遍历另一本工作簿中的工作表以查找特定的工作簿:
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
Then you can access that worksheet by 然后,您可以通过以下方式访问该工作表
Wkb.Sheets(idx)
Information taken from: https://support.microsoft.com/en-us/kb/142126 信息来自: https : //support.microsoft.com/en-us/kb/142126
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.