在循环目录时,Excel会调整为非活动工作表

我有以下macros循环通过目录,并把数据放在我的主文件。 主文件夹包含有关员工在特定项目上花费的小时数的所有信息。 但是,员工小时文件(非主文件)的工作表名称可能有所不同。 我设法改变这activesheet(主表),但我不知道如何调整这个非活动(非主)工作表(在公式这个特定的语句: Set CurrentWBSht = CurrentWB.Sheets("Sheet1")

  Option Explicit Sub CopyToMasterFile() Dim MasterWB As Workbook Dim MasterSht As Worksheet Dim MasterWBShtLstRw As Long Dim FolderPath As String Dim TempFile Dim CurrentWB As Workbook Dim CurrentWBSht As Worksheet Dim CurrentShtLstRw As Long Dim CurrentShtRowRef As Long Dim CopyRange As Range Dim ProjectNumber As String Dim wbname As String Dim sheetname As String wbname = ActiveWorkbook.Name sheetname = ActiveSheet.Name FolderPath = "C:\test file\" TempFile = Dir(FolderPath) Dim WkBk As Workbook Dim WkBkIsOpen As Boolean 'Check is master is open already For Each WkBk In Workbooks If WkBk.Name = wbname Then WkBkIsOpen = True Next WkBk If WkBkIsOpen Then Set MasterWB = Workbooks(wbname) Set MasterSht = MasterWB.Sheets(sheetname) Else Set MasterWB = Workbooks.Open(FolderPath & wbname) Set MasterSht = MasterWB.Sheets(sheetname) End If ProjectNumber = MasterSht.Cells(1, 1).Value Do While Len(TempFile) > 0 'Checking that the file is not the master and that it is a xlsx If Not TempFile = wbname And InStr(1, TempFile, "xlsx", vbTextCompare) Then Set CopyRange = Nothing 'Note this is the last used Row, next empty row will be this plus 1 With MasterSht MasterWBShtLstRw = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set CurrentWB = Workbooks.Open(FolderPath & TempFile) Set CurrentWBSht = CurrentWB.Sheets("Sheet1") With CurrentWBSht CurrentShtLstRw = .Cells(.Rows.Count, "AE").End(xlUp).Row End With For CurrentShtRowRef = 1 To CurrentShtLstRw If CurrentWBSht.Cells(CurrentShtRowRef, "AE").Value = ProjectNumber Then 'This is set to copy from Column A to Column L as per the question If CopyRange Is Nothing Then 'If there is nothing in Copy range then union wont work 'so first row of the work sheet needs to set the initial copyrange Set CopyRange = CurrentWBSht.Range("AE" & CurrentShtRowRef & _ ":AQ" & CurrentShtRowRef) Else 'Union is quicker to be able to copy from the sheet once Set CopyRange = Union(CopyRange, _ CurrentWBSht.Range("AE" & CurrentShtRowRef & _ ":AQ" & CurrentShtRowRef)) End If ' ending If CopyRange Is Nothing .... End If ' ending If CurrentWBSht.Cells.... Next CurrentShtRowRef CopyRange.Select 'add 1 to the master file last row to be the next open row CopyRange.Copy MasterSht.Cells(MasterWBShtLstRw + 1, 1).PasteSpecial xlPasteValues CurrentWB.Close savechanges:=False End If 'ending If Not TempFile = "master.xlsx" And .... TempFile = Dir Loop ActiveSheet.Range("A1:M200").RemoveDuplicates Columns:=Array(1, 2, 4, 8, 9, 10, 11, 12), Header:=xlYes End Sub 

有几种方法可以参考工作表,而不必事先知道他们的名字:

 'To get a specific worksheet: Set CurrentWBSht = CurrentWB.Worksheets(10) 'To get the last worksheet: Set CurrentWBSht = CurrentWB.Worksheets(Worksheets.Count) 'To get the pre last worksheet: Set CurrentWBSht = CurrentWB.Worksheets(Worksheets.Count-1) 

如果工作簿只有一张纸,那么你可以简单地这样做:

 Set CurrentWBSht = CurrentWB.Sheets(1) 

如果“非主”工作簿中有多个表单,则可以这样做:

 Set CurrentWB = Workbooks.Open(FolderPath & TempFile) Dim oWS As Worksheet ' Loop through all sheets to find the sheet we want For Each oWS In CurrentWB.Worksheets If oWS.Name = sheetname Then Set CurrentWBSht = oWS Exit For End If Next 

您可以在上面的循环中添加一个标记,以确认您是否find工作表

另外,从我所看到的,你的macros在你的主表中? 如果是这样的话,如果“主工作簿”是开放的,则不需要进行检查。 您可以只使用ThisWorkbook.Worksheets(1).NameThisWorkbook是您的macros运行的工作簿的对象)