从另外一个excel文件运行宏目前excel文件

问题描述 投票:0回答:2

我在Excel文件中的一个编写宏。我想运行从另一个Excel工作表。

我的代码:

Sub Full_Automation()
    Dim All_Submitted_Dates As Variant
    Dim All_WorkWeek As Variant
    Dim dctUnique_WorkWeek As Dictionary
    Dim DateCounter As Long
    Dim WorkWeekCounter As Long

    Sheet1.Activate
    Set dctUnique_WorkWeek = New Dictionary

With Sheet1
    All_Submitted_Dates = Application.Transpose(.Range(.Range("K2"), .Cells(.Rows.Count, "K").End(xlUp)))
End With

    WorkWeekCounter = 1

For DateCounter = 1 To UBound(All_Submitted_Dates)
    If Not dctUnique_WorkWeek.Exists("WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter))) Then
        dctUnique_WorkWeek.Add Key:="WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter)), Item:=1
    Else
        dctUnique_WorkWeek("WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter))) = dctUnique_WorkWeek("WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter))) + 1
    End If
Next DateCounter

    Worksheets.Add after:=Sheets(Sheets.Count)
    Worksheets(3).Activate


    Dim rowCounter As Long
    Dim varKey As Variant

    rowCounter = 2


    For Each varKey In dctUnique_WorkWeek.Keys()
        Range("A" & rowCounter).Value = varKey
        Range("D" & rowCounter).Value = dctUnique_WorkWeek(varKey)

        If rowCounter = 2 Then
            Range("C" & rowCounter).Formula = "=B" & rowCounter
            Range("E" & rowCounter).Formula = "=D" & rowCounter
        Else
            Range("C" & rowCounter).Formula = "=C" & (rowCounter - 1) & "+B" & rowCounter
            Range("E" & rowCounter).Formula = "=E" & (rowCounter - 1) & "+D" & rowCounter
        End If
        rowCounter = rowCounter + 1
    Next

End Sub

当我试图调试一行代码行,我知道了,每当我执行行Sheet1.Activate它要到原始Excel文件在我的宏是存在。我将如何引用另一个工作簿的第一个工作表?

excel vba excel-formula excel-vba-mac
2个回答
1
投票

Sheets集合是Workbook对象的属性(观察到Sheets收集比worksheets收集更具包容性的,因为不是所有的SheetsWorksheets)。默认工作簿是ActiveWorkbook,如果你不指定任何东西,这将得到解决。

您可以将工作簿分配给声明为Workbook的变量。

Dim Wb As Workbook
Set Wb = ThisWorkbook
or 
Set Wb = ActiveWorkbook
or
Set Wb = Workbooks.Open ([File name])
or
Set Wb = Workbooks.Add ([Template])

然后,您可以解决在指定的工作簿中的任何表。

Debug.Print Wb.Worksheets("Sheet1").Cells(1, 1).Value

1
投票

早期绑定可以在最初声明中加载字典对象。

Set dctUnique_WorkWeek = New Dictionary

这将创建一个1-d阵列,但在启动增量在For ...接下来的1,不为零。可能更好地简单地使用一个2-d阵列。事实上,我已经是权宜之计始终使用LBOUND到UBound函数的使用For ... Next涉及的数组。

With Sheet1
    All_Submitted_Dates = Application.Transpose(.Range(.Range("K2"), .Cells(.Rows.Count, "K").End(xlUp)))
End With

该工作表Sheet1代号将会包含VBA项目的工作簿内引用Sheet1工作。使用工作表的名称,如果外部提供一个明确的父工作簿。

Sheet1.Activate

其实,没有必要.Activate工作表,只要提供一个明确的引用父工作簿中引用它。

dctUnique_WorkWeek("WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter))) = dctUnique_WorkWeek("WW" & WorksheetFunction.WeekNum(All_Submitted_Dates(DateCounter))) + 1

一)VBA的格式使用WW格式掩码来获取相同数目的WorksheetFunction.WeekNum。 b)中有绕过字典的Exists方法的速记“在字典COUNTIF”。

WorkWeekCounter不会出现超出被声明并指派值1使用。

WorkWeekCounter = 1

您可以一次写的所有键和项目。该公式将要求因不同的公式2个步骤。

For Each varKey In dctUnique_WorkWeek.Keys()

您的公式似乎引用B列还没有值在新的工作表放入B列。

Option Explicit

Sub Full_Automation()

    Dim All_Submitted_Dates As Variant, dctUnique_WorkWeek As New Dictionary
    Dim dc As Long

    With ActiveWorkbook  'better as With Workbooks("Book1.xlsx")

        With .Worksheets("Sheet1")
            All_Submitted_Dates = .Range(.Cells(2, "K"), .Cells(.Rows.Count, "K").End(xlUp)).Value2
        End With

        For dc = LBound(All_Submitted_Dates, 1) To UBound(All_Submitted_Dates, 1)
            dctUnique_WorkWeek.Item("WW" & Right(Format(All_Submitted_Dates(dc, 1), "\0ww"), 2)) = _
                dctUnique_WorkWeek.Item("WW" & Right(Format(All_Submitted_Dates(dc, 1), "\0ww"), 2)) + 1
        Next dc

        Worksheets.Add After:=.Sheets(.Sheets.Count)

        With .Sheets(.Sheets.Count)

            'name = "give the new worksheet a name"

            .Cells(2, "A").Resize(dctUnique_WorkWeek.Count, 1) = Application.Transpose(dctUnique_WorkWeek.keys)
            .Cells(2, "D").Resize(dctUnique_WorkWeek.Count, 1) = Application.Transpose(dctUnique_WorkWeek.items)

            'optionally sort the weeks
            With .Cells(2, "A").Resize(dctUnique_WorkWeek.Count, 4)
                .Sort key1:=.Cells(1), order1:=xlAscending, Header:=xlNo
            End With

            .Cells(2, "C").Formula = "=B2"
            .Cells(2, "E").Formula = "=D2"

            .Range(.Cells(3, "C"), .Cells(.Rows.Count, "A").End(xlUp).Offset(0, 2)).Formula = "=C2+B3"
            .Range(.Cells(3, "E"), .Cells(.Rows.Count, "A").End(xlUp).Offset(0, 4)).Formula = "=E2+D3"

        End With

    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.