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

时间:2017-08-07 08:41:13

标签: excel vba excel-vba

我有以下宏来遍历目录并将数据放入我的主文件中。 master文件夹包含有关特定项目的员工营业时间的所有信息。但是,员工小时文件(非主文件)的工作表名称可能不同。我设法为活动表(主表)更改了这个,但我不确定如何为非活动(非主)表单调整此值(在公式中这个特定的句子: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

2 个答案:

答案 0 :(得分: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 :(得分:0)

如果工作簿只有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

您可以在上面的循环中添加一个标记,以确认您是否找到了纸张

另外,从我所看到的,你的宏在你的主表中?如果是这样的话,那么您是否需要检查“Master”工作簿'开了。您可以使用 ThisWorkbook.Worksheets(1).Name ThisWorkbook是运行宏的工作簿的对象)