I am writing a macro in one of the excel files. I want to run that from another excel sheet.
My code:
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
When I tried to debug the code line by line, I got to know that whenever I execute the line Sheet1.Activate
it going to the original excel file where my macro is present.
How will I refer to the first worksheet of another workbook?