Share via

"Object variable or With block variable not set" accessing ActiveSheet

Anonymous
2011-04-29T03:13:41+00:00

I am trying to set all worksheets to a user-specified zoom:

 1 Option Explicit

 2

 3 Sub AllSheetZoom()

 4

 5 Dim wSht As Worksheet

 6 Dim SvActiveSht As Worksheet

 7 Dim strZoom As String

 8

 9 SvActiveSht = ActiveWorkbook.ActiveSheet

10

11 strZoom = InputBox("Zoom percentage?")

12

13 For Each wSht In ActiveWorkbook.Worksheets

14     wSht.Activate

15     ActiveWindow.Zoom = strZoom

16 Next wSht

17

18 ActiveSheet = SvActiveSht

19

20 End Sub

This generates the following error:

   Run-time error '91':

   Object variable or With block variable not set

Clicking on Debug highlights line 9.  I think the error means that I am missing an object prefix, so I replaced line 9 with:

   SvActiveSht = Application.ActiveWorkbook.ActiveSheet

I get the same error.  What is wrong?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

OssieMac 48,001 Reputation points Volunteer Moderator
2011-04-29T04:57:55+00:00

You left out "Set" that is required to assign an object to a variable'

See comment re other syntax error line 18

1 Option Explicit

 2

 3 Sub AllSheetZoom()

 4

 5 Dim wSht As Worksheet

 6 Dim SvActiveSht As Worksheet

 7 Dim strZoom As String

 8

 9  Set SvActiveSht = ActiveWorkbook.ActiveSheet

10

11 strZoom = InputBox("Zoom percentage?")

12

13 For Each wSht In ActiveWorkbook.Worksheets

14     wSht.Activate

15     ActiveWindow.Zoom = strZoom

16 Next wSht

17

'18 ActiveSheet = SvActiveSht   'Incorrect syntax

18 SvActiveSht.Activate

19

20 End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-04-29T05:29:42+00:00

    Thanks, OssieMac.  I'm trying to lose my C++ ways.  This will take some time, but I feel the transformation taking place.

    Was this answer helpful?

    0 comments No comments