defining variable in sub

sspicer

New Member
Joined
Jun 27, 2008
Messages
48
I have a lot of repetitive code and was trying to clean it up by creating a sub with a variable, I'm not sure whether I'm correct in my thinking but would love some help with the below, I've written the following code to navigate between worksheets:

Code:
Sub MoveTo(Target As Worksheet)
    Sheets(Target).Activate
    UnhideAll
    HideOtherSheets
End Sub

where UnhideAll and HideOtherSheets both work in their own right, and indeed if I put an actual sheet name in place of target the sub works.

Then, I have the following code upon opening:

Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    
'ask whether a new session should be started or not
    
    Message = MsgBox("Do you wish to begin a new session?" & Chr(10) & Chr(10) & "Clicking Yes will clear all existing data", vbYesNo, "Warning!")
        
        If Message = vbYes Then
            Sheets("Scheme Input").Range("C5:C21").ClearContents
            Sheets("Member Data").Range("H3:H10000").ClearContents
            Sheets("Member Data").Range("J3:L10000").ClearContents
        Else: End If
    
MoveTo (Control)
'remember calculation mode of excel upon opening
    
    If Application.Calculation = xlManual Then
        Application.Calculation = xlAutomatic
        StartCalcMode = "Manual"
    Else: StartCalcMode = "Automatic"
    End If
    
End Sub

and I keep getting an error that says "object variable or with block variable not set".

Any ideas??
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You are passing a Worksheet object, not the name of a sheet, so this line:
Code:
Sheets(Target).Activate
should be:
Code:
Target.Activate
 
Upvote 0
Thank you! Works a dream (although changed the variable to string instead of other way around, but I guess would work either way).

Code:
Sub MoveTo(Target As String)
    Sheets(Target).Activate
    UnhideAll
    HideOtherSheets
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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