2017-08-07 71 views
1

我有以下宏循環瀏覽目錄並將數據放入我的主文件中。主文件夾包含有關員工在特定項目上的花費時間的所有信息。但是,員工小時文件(非主文件)的工作表名稱可能有所不同。我設法改變這種爲activesheet(原材),但我不知道如何調整這個對於非主動(非主)張(公式中的這個特定的句子:Set CurrentWBSht = CurrentWB.Sheets("Sheet1")Excel循環瀏覽目錄時調整爲非活動工作表

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 

回答

1

有幾種方法引用到工作表,而不需要事先知道他們的名字:

'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) 
+1

感謝您的回覆。這很簡單,我想我覺得太複雜了,謝謝! – Smits

0

如果工作簿不僅具有1片,那麼你可以簡單地這樣做:

Set CurrentWBSht = CurrentWB.Sheets(1) 

如果有更多的超過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 

你可以在迴路中增加一個標誌上方,以確認,如果你發現了一個片

此外,從我所看到的,你的宏在你的主表中?如果是這種情況,如果「主工作簿」已打開,則不需要執行檢查。您可以使用ThisWorkbook.Worksheets(1).NameThisWorkbook是您的宏運行的工作簿的對象)