Macro to open a second workbook and run macro from the first workbook

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Dear Excel Experts,

I have a set of macros (below is my code) that should do the job on another workbook. I found a macro that can open another workbook but it only does the job to open it but the rests of actions it tries to do it on the original workbook. :( I don't know how to "direct" the macros to open workbook AND do actions on THAT workbook then prompt "Save As.."

Appreciate anyone's help on my problem.

Code:
Sub DelegateWork()'On Error Resume Next
Dim lngLastRow As Long
Dim lngNumberOfRows As Long
Dim lngI As Long
Dim strMainSheetName As String
Dim currSheet As Worksheet
Dim prevSheet As Worksheet
Dim Staff As Integer
Dim WS As Worksheet
Dim i As Long
Dim excelfile As String


excelfile = "CTT_" & Text(TODAY(), "ddmmyyyy") & ".xlsb"
Workbooks.Open "C:\Users\Dahlia\Downloads\Test\" & excelfile
'Current worksheet in workbook
Set prevSheet = ActiveWorkbook.ActiveSheet
'First worksheet name
strMainSheetName = prevSheet.Name
'Number of rows in worksheet
lngLastRow = prevSheet.Cells(Rows.Count, 1).End(xlUp).Row
Staff = Application.InputBox("How many to delegate?")
'Number of rows to split among worksheets
lngNumberOfRows = (lngLastRow / Staff) + 1
'Worksheet counter for added worksheets
lngI = 1
While lngLastRow > lngNumberOfRows
Set currSheet = ThisWorkbook.Worksheets.Add


With currSheet
.Move After:=Worksheets(Worksheets.Count)
.Name = strMainSheetName + "(" + CStr(lngI) + ")"
End With
With prevSheet.Rows(lngNumberOfRows + 1 & ":" & lngLastRow).EntireRow
.Cut currSheet.Range("A2")
End With
lngLastRow = currSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set prevSheet = currSheet
lngI = lngI + 1
Wend

For i = 2 To Sheets.Count
        Sheets(1).Rows(1).Copy Destination:=Sheets(i).Rows(1)
    Next

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,216,066
Messages
6,128,577
Members
449,459
Latest member
20rayallen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top