Carryover variable to called sub

dlowell09

New Member
Joined
Jan 17, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am attempting to simplify some VBA code as it has gotten fairly long and is taking an extensive amount of time to run. I have multiple cubes that are updated weekly and I need to copy data from those cubes to a new sheet where I can manipulate and make edits. Rather than having three subs with identical code, I thought I could set up a separate sub that is called by each of the main subs. The problem is that I have variables to refer to the different sheets and need those to carry down from the primary sub to the secondary sub. The problem is that the variable I set in my primary sub is not being recognized by the secondary sub. I just get a runtime error 424 object required.

An example (condensed to show relevant sections):

VBA Code:
Sub Update_Actuals_Expansion()

Dim WSCube As Worksheet
Dim WSHist As Worksheet

Set WSCube = ActiveWorkbook.Worksheets(Sheet3)
Set WSHist = ActiveWorkbook.Worksheets(Sheet6)

Call CopyActuals

End Sub

Sub CopyActuals()

'Clear the old historical data to avoid any contamination
    WSHist.Activate
    WSHist.Cells.ClearContents
    
    'Copy new actuals from the cube and paste into the newly cleared historical data sheet
    WSCube.Activate
    WSCube.Range("A8").Select
    ActiveCell.CurrentRegion.Select
    Selection.Copy
    WSHist.Activate
    WSHist.Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Correction, I copied the old code, variable definition should be this:

VBA Code:
Set WSCube = Sheet4
Set WSHist = Sheet5
 
Upvote 0
Pass them as arguments:

VBA Code:
Sub CopyActuals(wsHist as Worksheet, wsCube As Worksheet)

'Clear the old historical data to avoid any contamination
    WSHist.Cells.ClearContents
    
    'Copy new actuals from the cube and paste into the newly cleared historical data sheet
    WSCube.Range("A8").CurrentRegion.Copy
    WSHist.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                     SkipBlanks:=False, Transpose:=False
        
End Sub

called using:

VBA Code:
Sub Update_Actuals_Expansion()

Dim WSCube As Worksheet
Dim WSHist As Worksheet

Set WSCube = Sheet4
Set WSHist = Sheet5

Call CopyActuals(WSHist, WSCube)

End Sub
 
Upvote 1
Solution
Pass them as arguments:

VBA Code:
Sub CopyActuals(wsHist as Worksheet, wsCube As Worksheet)

'Clear the old historical data to avoid any contamination
    WSHist.Cells.ClearContents
   
    'Copy new actuals from the cube and paste into the newly cleared historical data sheet
    WSCube.Range("A8").CurrentRegion.Copy
    WSHist.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                     SkipBlanks:=False, Transpose:=False
       
End Sub

called using:

VBA Code:
Sub Update_Actuals_Expansion()

Dim WSCube As Worksheet
Dim WSHist As Worksheet

Set WSCube = Sheet4
Set WSHist = Sheet5

Call CopyActuals(WSHist, WSCube)

End Sub
That is exactly what I needed. Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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