Object Variable or With block variable not set when opening excel file

Nobelium

Because I couldn't find a way to increase font size of a dropdown list, I had to find a workaround. One option is to zoom in. Therefore I have this piece of code - which works perfectly. But as soon as I close the file and try to reopen it, there is an error message: "Object Variable or with block variable not set."

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo errorHandler

Dim xZoom As Long
xZoom = 60

If Target.Validation.Type = xlValidateList Then xZoom = 125

errorHandler:
ActiveWindow.Zoom = xZoom  'Debug highlights this row

End Sub

I event tried:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo errorHandler

Dim wb as Workbook

Dim xZoom As Long
xZoom = 60

Set wb = ThisWorkbook

wb.Activate

If Target.Validation.Type = xlValidateList Then xZoom = 125

errorHandler:
ActiveWindow.Zoom = xZoom  'Debug highlights this row

End Sub

But nothing seems to work... I simply don't know what the cause could be... Could anyone please help me?

D_Bester

Check that windows.count > 0 before doing your zoom.

If Application.Windows.Count > 0 Then
    ActiveWindow.Zoom = xZoom
End If

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    On Error GoTo errorHandler

    Dim xZoom As Long
    xZoom = 60

    If Target.Validation.Type = xlValidateList Then
        xZoom = 125
        If Application.Windows.Count > 0 Then
            ActiveWindow.Zoom = xZoom  'Debug highlights this row
        End If
    End If
Exit Sub 'must exit before the error handler
errorHandler:
    Msgbox(Err.Description)
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"Object variable or With block variable not set" in VBA

Object variable or block variable not set when processing HTMLSelectElement

Object variable or With block variable not set when referencing cell using .Find

New to Excel-VBA: Run Time Error "91": Object Variable or With block variable not set

VBA Excel , 91 object variable or with block variable not set

Object Variable or With Block Variable not set getelementsbyname vba

Excel VBA - Error: Object Variable or With Block Variable not set

Object Variable or With Block Not Set (Excel VBA)

Range variable errors in Excel VBA ; "Object variable or With block variable not set"

My Excel "Run-Time error 91: Object Variable or With block variable not set"

dbOpenDynaset - 'Object variable or With block variable not set'

MS Excel 2007 VBA function error (Object variable or with block variable not set)

91 object variable or with block variable not set

Excel vba Object variable or With block variable not set error

Object variable or With block variable not set (Error 91) in excel

VBA error 91: Object variable or With block variable not set (Excel 2013)

Error "object variable or with block variable not set" when searching through code

Object variable or With block variable not set

EXCEL - object variable or with block variable not set

Object variable or With block variable not set error in vba

Object Variable and With block not set error from excel vba

check if getelementsbyclassname exists in excel vba. runtime error 91 object variable or with block variable not set

VBA Excel SQL object variable or with block variable not set

It says Object variable or With block variable is not set

"Object variable or With block variable not set" when closing workbook

VBA: Object Variable or With Block not Set

Object variable or With Block variable not set when looping through files

Excel: object variable or with block not set

Procedure to import excel file to access is erroring out after loop - MS Access Error 91: Object variable or With block variable not set