I'm looking to populate a worksheet depending on the checkboxes selected on the userform. In my search for a solution, I've found the following code which I've adapted . However, it doesn't provide the solution what I'm looking for. If checkbox AM is selected, then it writes the values required. However, if I select 2 or more checkboxes then the ID on all of the rows are the same. If the selection is either checkbox PM or Sat then a blank row is put in. Any suggestion or help would be appreciated. Thanks
VBA Code:
Private Sub cmdRouteDetailAdd()
' Declare variables
Dim ws As Worksheet
Dim lastRow As Long
Dim IDVal As String, RouteVal As String, LocateVal As String
' Set the worksheet
Set ws = ThisWorkbook.Sheets("RouteData") ' Ensure the sheet "RouteData" exists
IDVal = TextBoxID.Text
RouteVal = TextBoxRoute.Text
LocateVal = TxtPickDropLocation
' Find the last used row in the sheet and calculate the next empty row
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).row + 1
If CheckBoxAM.Value = True Then
' Add the entries
' First Entry: AM
ws.Cells(lastRow, "A").Value = IDVal
ws.Cells(lastRow, "B").Value = RouteVal ' Column B: Route
ws.Cells(lastRow, "C").Value = "AM" ' Column B: "AM"
ws.Cells(lastRow, "D").Value = LocateVal ' Column D: Location
lastRow = lastRow + 1
End If
If CheckBoxPM.Value = True Then
' Second Entry: PM
lastRow = lastRow
ws.Cells(lastRow, "A").Value = IDVal
ws.Cells(lastRow, "B").Value = RouteVal ' Column B: Route
ws.Cells(lastRow, "C").Value = "PM" ' Column B: "PM"
ws.Cells(lastRow, "D").Value = LocateVal ' Column D: Location
lastRow = lastRow + 1
End If
If CheckBoxSat.Value = True Then
' Third Entry: PM
lastRow = lastRow + 1
ws.Cells(lastRow, "A").Value = IDVal
ws.Cells(lastRow, "B").Value = RouteVal ' Column B: Route
ws.Cells(lastRow, "C").Value = "Sat" ' Column B: "Sat"
ws.Cells(lastRow, "D").Value = LocateVal ' Column D: Location
lastRow = lastRow + 1
End If
' Clear the text boxes for new input
TextBoxRoute.Text = ""
CheckBoxAM = False
CheckBoxPM = False
CheckBoxSat = False
TxtPickDropLocation.Text = ""
TextBoxRoute.SetFocus ' Set focus back to the first TextBox
Call ReSequenceRouteOrder
Unload Me
End Sub