Populate Worksheet Rows Based on Userform Checkbox selection

TellM1955

New Member
Joined
Apr 8, 2021
Messages
44
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
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
 
High, my apologies as I meant to write yo both of you on my previous reply. Both solution work well except I would like that the IDVAL is unique on each row.
Regards
Terry
This will start off at 1 and add 1 for each row added.

It does not depend on the user entering a unique ID.

VBA Code:
Private Sub cmdRouteDetailAdd_Click()
' Declare variables.
Dim Ws As Worksheet
Dim lngNextRow As Long
Dim RouteVal As String, LocateVal As String
Dim lngNext As Long

  ActiveWorkbook.Save
    
  ' Ensure the sheet "RouteData" exists.
  If Not (ActiveWorkbook.Sheets("RouteData").Index > 0) Then
    Exit Sub
  End If
    
  ' Set the worksheet.
  Set Ws = ActiveWorkbook.Sheets("RouteData")
    
  ' Get the last ID.
  If Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row = 1 Then
    lngNext = 0
  Else
    lngNext = WorksheetFunction.Max(Ws.Range("A2:A" & Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row))
  End If
  
  RouteVal = TextBoxRoute.Text
  LocateVal = TxtPickDropLocation
      
  If CheckBoxAM.Value = True Then
    ' Add the entries
    ' First Entry: AM
      lngNext = lngNext + 1
      Ws.Cells(Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row + 1, "A").Resize(1, 4). _
        Value = Array(lngNext, RouteVal, "AM", LocateVal)
  End If
  
  If CheckBoxPM.Value = True Then
    ' Second Entry: PM
    lngNext = lngNext + 1
    Ws.Cells(Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row + 1, "A").Resize(1, 4). _
      Value = Array(lngNext, RouteVal, "PM", LocateVal)
  End If
  
  If CheckBoxSat.Value = True Then
    ' Third Entry: PM
    lngNext = lngNext + 1
    Ws.Cells(Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row + 1, "A").Resize(1, 4). _
      Value = Array(lngNext, RouteVal, "Sat", LocateVal)
  End If
  
  Call ReSequenceRouteOrder
  
  Unload Me
  
End Sub
 
Upvote 0
Solution

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,272
Messages
6,177,632
Members
452,786
Latest member
k3calloway

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top