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