Search code examples
vbaexcelcomboboxuserform

VBA screenupdating in if statement


I have a combobox in a userform filled with sheets. If you choose a sheet it activates the sheet so you have the option to unprotect it.

My problem here is if I choose a sheet with the combobox it jumps to that sheet.

I tried to set Application.Calculation = xlCalculationManual and Application.ScreenUpdating = False in the end and tried it with a If-Statement. It doesn't work.

Public comBox2 As Boolean
Private Sub ComboBox2_Change()
    Dim sName As String 'name of sheet to show

    comBox2 = True

    sheetName = ComboBox2.Value

    With ActiveWorkbook.Sheets(sheetName)
        .Activate
    End With
End Sub
Sub Userform_initialize()
    If comBox2 Then
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    ElseIf comBox2 = False Then
    comBox2 = False
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End If
End Sub

Solution

  • Public comBox2 As Boolean
    Private Sub ComboBox2_Change()
        Dim sName As String 'name of sheet to show
    
        comBox2 = True
    
        sheetName = ComboBox2.Value
    
    End Sub
    Sub Userform_initialize()
        If comBox2 Then
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        ElseIf comBox2 = False Then
        comBox2 = False
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        End If
    End Sub
    

    So I deleted that activation of the sheet. Now what I would do is put a button next ti combobox with caption "unprotect" and put code for unprotection in it like:

     Activeworkbook.sheets(ComboBox2.value).Unprotect "password" ' in case there is fixed password
    

    or if there are different passwords then

    Dim pass As Variant
    pass = InputBox("Password?")
    Activeworkbook.sheets(ComboBox2.value).Unprotect pass