Search code examples
vbaexcelcheckboxuserform

vba excel userform.show not working


I'm a bit new to VBA, and I think I have a basic understanding of how to use it. My User Form is running fine, but under a very specific condition that I don't like. It can only draw the checkboxes if and only if I run the main code first, and even then it only works the first time and won't update with respect to changes in the workbook. I assumed it was due to needing the public variable to be run, but after testing that theory, the problem persist. Can anyone help me solve this problem? What is causing this?

Here is my main Code:

Public Allplans As Integer

Sub Compare()

    Allplans = Sheets.Count - 2
    UF1.Show

''''Clear old series
For Each Series In Sheets(2).SeriesCollection
        Sheets(2).SeriesCollection(1).Delete
Next

''''Add new series
For i = 1 To Allplans
    Sheets(2).SeriesCollection.NewSeries
    Sheets(2).SeriesCollection(i).Name = Sheets(i + 2).Range("$A$1")
    Sheets(2).SeriesCollection(i).XValues = Sheets(i + 2).Range("$A$10:$A$23")
    Sheets(2).SeriesCollection(i).Values = Sheets(i + 2).Range("$B$10:$B$23")
Next

End Sub

And this is my User Form:

Private Sub UF1_Initialize()

Dim i           As Integer
Dim chkBox      As MSForms.CheckBox

For i = 1 To Allplans
    Set chkBox = UF1.Frame1.Controls.Add("Forms.CheckBox.1", "CheckBox" & i)
    chkBox.Caption = Sheets(i + 2).Name
    chkBox.Left = 10
    chkBox.Top = 5 + ((i - 1) * 20)
    chkBox.Value = True
Next

End Sub

Solution

  • As you do not give that much information please try the following changes to your code. Rename UF1_Initialize to UserForm_Initializand use the Mekey word in it like that

    Private Sub UserForm_Initialize()
    
    Dim i           As Integer
    Dim chkBox      As MSForms.CheckBox
    
    For i = 1 To Allplans
        Set chkBox = Me.Frame1.Controls.Add("Forms.CheckBox.1", "CheckBox" & i)
        chkBox.Caption = Sheets(i + 2).Name
        chkBox.Left = 10
        chkBox.Top = 5 + ((i - 1) * 20)
        chkBox.Value = True
    Next
    
    End Sub
    

    Also do not use the default instance of the userform

    Option Explicit
    
    Public Allplans As Integer
    
    Sub Compare()
    
    Dim series
    Dim i As Long
    
        Allplans = Sheets.Count - 2
    
        Dim myUF1 As New UF1
        myUF1.Show
    
    ''''Clear old series
    For Each series In Sheets(2).SeriesCollection
            Sheets(2).SeriesCollection(1).Delete
    Next
    
    ''''Add new series
    For i = 1 To Allplans
        Sheets(2).SeriesCollection.NewSeries
        Sheets(2).SeriesCollection(i).Name = Sheets(i + 2).Range("$A$1")
        Sheets(2).SeriesCollection(i).XValues = Sheets(i + 2).Range("$A$10:$A$23")
        Sheets(2).SeriesCollection(i).Values = Sheets(i + 2).Range("$B$10:$B$23")
    Next
    
    End Sub
    

    It is also good practise to use Option Explicit, just please also have a look here. You might also have a look at this tutorial, especially the part to add code for cancelling the form