0

I'm trying to create a VBA code to checkmark specific lube routes based on field data. My code works to generate a dynamic userform with the equipment number & part name as a checkbox.

My question is how do I then code the enter command button code to insert an "X" in the Done column?

My current code is below. The image of the sheet is also attached.enter image description here

Option Explicit

Private Sub CommandButton1_Click()

Dim curColumn   As Long
Dim LastRow     As Long
Dim i           As Long
Dim chkBox      As MSForms.CheckBox
Dim ctrl        As Control

curColumn = 1 'Set your column index here
LastRow = Worksheets("Monday").Cells(Rows.Count, curColumn).End(xlUp).Row

For Each ctrl In Me.Controls
    If TypeName(ctrl) = "Checkbox" Then
        If ctrl.Value = True Then
             Worksheets("Monday").Cells(i, curColumn + 4).Value = "X"
        End If
    End If
    i = i + 1
Next ctrl
Unload Me
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
 
Dim curColumn   As Long
Dim LastRow     As Long
Dim i           As Long
Dim chkBox      As MSForms.CheckBox

curColumn = 1 'Set your column index here
LastRow = Worksheets("Monday").Cells(Rows.Count, curColumn).End(xlUp).Row

For i = 2 To LastRow
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkBox.Caption = Worksheets("Monday").Cells(i, curColumn).Value & " " & 
Worksheets("Monday").Cells(i, curColumn + 2).Value
    chkBox.Left = 5
    chkBox.Top = 5 + ((i - 1) * 20)
Next i

End Sub

1 Answer 1

0

The simple answer to your question is that you need to work out the row number i. You can achieve this by extracting it from the name of the checkbox (which was set when the form was initialised). After the line If ctrl.Value = True Then add the following line:

i = Val(Replace(ctrl.Name, "CheckBox_", ""))

Replace(ctrl.Name, "CheckBox_", "") removes the string "CheckBox_" from the name. For example if the name is "CheckBox_15", you will end up with the string "15". You then use Val("15") to evaluate the string "15" to the numeric value 15.

Further Suggestions

To help you make your code a little more dynamic, copy this function into a regular module:

Function GetColNo(sHeading As String, sSheetName As String, lHeadingsRow As Long) As Long
  Dim vHeadings As Variant
  Dim lLastCol As Long
  Dim j As Long
  
  With ThisWorkbook.Sheets(sSheetName)
    lLastCol = .Cells(lHeadingsRow, Columns.Count).End(toleft).Column
    vHeadings = .Range(.Cells(lHeadingsRow, 1), .Cells(lHeadingsRow, lLastCol))
    GetColNo = 0
    For j = 1 To lLastCol
      If LCase(vHeadings(1, j)) = LCase(sHeading) Then
        GetColNo = j
        Exit Function
      End If
    Next j
  End With
  
End Function

Replace all the code in the userform module with this:


Option Explicit

Private Sub CommandButton1_Click()

    Dim curColumn   As Long
    Dim LastRow     As Long
    Dim i           As Long
    Dim chkBox      As MSForms.CheckBox
    Dim ctrl        As Control
    
    ' ****
    Dim sSheetName  As String:  sSheetName = "Monday"
    Dim lDoneCol    As Long:    lDoneCol = GetColNo("Done", sSheetName, 1)
    
    ' No need for this now
    ' curColumn = 1 'Set your column index here
    ' ****
    
    With ThisWorkbook.Sheets(sSheetName)
        LastRow = .Cells(Rows.Count, lEquipNoCol).End(xlUp).Row
        
        For Each ctrl In Me.Controls
            If TypeName(ctrl) = "Checkbox" Then
                If ctrl.Value = True Then
                     ' *****
                     ' Extract the row number i
                     i = Val(Replace(ctrl.Name, "CheckBox_", ""))
                     ' *****
                     .Cells(i, lDoneCol).Value = "X"
                End If
            End If
            ' ****
            ' No need to increment i
            'i = i + 1
        Next ctrl
    End With
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
 
    Dim curColumn   As Long
    Dim LastRow     As Long
    Dim i           As Long
    Dim chkBox      As MSForms.CheckBox
    
    ' ****
    Dim sSheetName      As String:  sSheetName = "Monday"
    Dim lEquipNoCol     As Long:    lEquipNoCol = GetColNo("Equip No", sSheetName, 1)
    Dim lPartNameCol    As Long:    lPartNameCol = GetColNo("Part Name", sSheetName, 1)
    Dim lDoneCol        As Long:    lDoneCol = GetColNo("Done", sSheetName, 1)
    
    ' No need for this now
    ' curColumn = 1 'Set your column index here
    ' ****
    
    With ThisWorkbook.Sheets(sSheetName)
        LastRow = Worksheets(sSheetName).Cells(Rows.Count, curColumn).End(xlUp).Row
        
        For i = 2 To LastRow
            Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
            chkBox.Caption = .Cells(i, lEquipNoCol).Value & " " & .Cells(i, lPartNameCol).Value
            ' ****
            ' You probably should check the boxes if the corresponding value is X
            If UCase(.Cells(i, lDoneCol).Value) = "X" Then
                chkBox.Value = True
            End If
            ' ****
            chkBox.Left = 5
            chkBox.Top = 5 + ((i - 1) * 20)
        Next i
    End With
End Sub

Unfortunaltely, I wasn't able to test the code so there might be a few typos/bugs, which you should easily be able to correct.

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.