+ Reply to Thread
Results 1 to 2 of 2

VBA - How to access checkbox values on userform that were created with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2022
    Location
    United States
    MS-Off Ver
    2016
    Posts
    2

    VBA - How to access checkbox values on userform that were created with VBA

    I have a userform that dynamically creates checkboxes for files in a chosen folder. How do I access the values of these checkboxes another sub?

    Userform2.png

    Sub for selecting folder and listing the check boxes is below

    Sub SelectFolder()
        Dim fldr, xlApp
        Dim saveName
        Dim nameLength
        Dim oFSO As Object
        Dim oFolder As Object
        Dim oFile As Object
        Dim i As Integer
        Dim j As Integer
        Dim chkBox As MSForms.CheckBox
        Dim slashLocation
        Dim nameLocation
        Dim partialDisplayNameLoc
    
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Visible = False
        Set file = xlApp.FileDialog(4) 'msoFileDialogFolderPicker
        With file
            .Title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = "C:\"
            If .Show = -1 Then foldername = .SelectedItems(1)
        End With
    
        i = 1
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFolder = oFSO.GetFolder(foldername)
        For Each oFile In oFolder.Files
            strName = CStr(oFile)
            slashLocation = InStrRev(strName, "\")
            nameLocation = Len(strName) - slashLocation
            displayName = Right(strName, nameLocation)
            If Right(strName, 4) = ".dxf" Then
                Set chkBox = selectFolderUserForm.Controls.Add("Forms.CheckBox.1")
                chkBox.Caption = CStr(displayName)
                'chkBox.Name = "CheckBox" & "i"                 '------------> Here is where I am trying to name the checkboxes to be able to access value from different sub
                chkBox.Left = 5
                chkBox.Top = 5 + ((i - 1) * 20)
                chkBox.Value = True
                chkBox.Font.Size = 10
                chkBox.AutoSize = True
                chkBox.WordWrap = False
                i = i + 1
            End If
        Next oFile
    End Sub
    
    Sub Process()
         'If CheckBox1.Value = True Then
               'MsgBox ("Checked")
         'End If
    End Sub

  2. #2
    Registered User
    Join Date
    07-12-2022
    Location
    United States
    MS-Off Ver
    2016
    Posts
    2

    Re: VBA - How to access checkbox values on userform that were created with VBA

    I found an answer on StackOverflow. I can make this work, but is there anyway to access these by name without looping?

    Sub OKButn_Click()
    z = 0
    y = 0
    'Loops through the controls on the form, to get values of the Checkboxes
       For Each ctlLoop In ColumnCopyForm.Controls
    'Checks to see if the value is true so it can be recorded
       If ctlLoop.Value = True Then
       'Records the column title in the correct place for future referencing
          Sheets("Report").Cells(SavedHdrsRow + 1 + y, SavedHdrsCol).Value = HdrArray(0, z)
          y = y + 1
    
        End If
        z = z + 1
        Next ctlLoop
    
    
    Unload ColumnCopyForm
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Make Worksheet Active-X CheckBox value same as Userform CheckBox value
    By L plates in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-21-2016, 08:23 AM
  2. [SOLVED] Userform: Have multiple checkbox values populate to one cell
    By ciresuark in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-22-2014, 12:44 PM
  3. Positioning newly created checkbox
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2014, 03:57 PM
  4. Run different macros based on userform checkbox values
    By nymanns in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2013, 06:26 PM
  5. Checkbox dynamically created
    By Vale1976 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-13-2013, 02:22 AM
  6. Can't Hide Last dynamically created checkbox
    By craigba in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2011, 07:23 AM
  7. To move the values from excel userform to Access table using vba
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2009, 09:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1