Forum Discussion
GranSupt
Feb 19, 2024Copper Contributor
Userform code to send values to next available column of next available row
I have a userform with several checkbox commands used to populate data into a table in a worksheet. There are plenty of examples on the forum showing how to select the next blank row OR the next blan...
smylbugti222gmailcom
Feb 20, 2024Iron Contributor
Here's the improved code for your userform, incorporating the best aspects of both responses and addressing potential issues:
VBA
Private Sub PMCheckBox1_Click() Dim ws As Worksheet Dim lastRow As Long, colOffset As Long Set ws = Sheets("Fleet Maint Records") If Me.PMCheckBox1.Value = True Then ' Find the last row with data in the first column (assuming column A) lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 ' Find the first empty column starting from column 9 colOffset = 9 Do While ws.Cells(lastRow, colOffset).Value <> "" And colOffset <= ws.ListObjects("FleetMaintTable").ListColumns.Count ' Check both emptiness and table boundary colOffset = colOffset + 1 Loop ' Check if a suitable column was found within the table boundaries If colOffset <= ws.ListObjects("FleetMaintTable").ListColumns.Count Then ' Write the checkbox text to the found cell ws.Cells(lastRow, colOffset).Value = "ENGINE OIL CHANGED" Else ' Handle the case where no empty column is found within the table ' (Optional: Display an error message, disable further selections, etc.) MsgBox "No more space available in the table." End If End If End Sub
Explanation:
Declare variables:
- ws: Worksheet object for "Fleet Maint Records".
- lastRow: Last row with data in the first column (assuming column A).
- colOffset: Offset from column 9 to find the next empty column.
Check checkbox value:
- If PMCheckBox1 is checked (Value = True), proceed.
Find last row:
- Use Range("A" & Rows.Count).End(xlUp).Row to find the last row with data in column A.
- Add 1 to get the next available row.
Find next empty column:
- Initialize colOffset to 9 (starting column for checkbox data).
- Use a Do While loop to iterate through columns:
- Check if the cell value in the current row (lastRow) and current column (colOffset) is empty ("") and if the column offset is still within the table boundaries (colOffset <= ws.ListObjects("FleetMaintTable").ListColumns.Count).
- If not empty or outside the table, increment colOffset to move to the next column.
- The loop stops when an empty cell within the table is found.
Write checkbox text (with error handling):
- Check if a suitable column was found (colOffset <= ws.ListObjects("FleetMaintTable").ListColumns.Count).
- If yes, write the text "ENGINE OIL CHANGED" to the cell.
- If no, handle the case where no empty column is found within the table boundaries. You can add an optional MsgBox to display an error message or take other appropriate actions.
Improvements:
- Combines the clarity and error handling from both responses.
- Uses ListObjects("FleetMaintTable").ListColumns.Count to ensure the search stays within the table boundaries.
- Provides optional error handling for the case where no empty column is found.
- You can adapt the error handling logic to suit your specific needs.
Repeating for other checkboxes:
- Copy and modify this code for each checkbox, replacing "PMCheckBox1" with the corresponding checkbox name and "ENGINE OIL CHANGED" with the appropriate text for each checkbox.
This refined code addresses the potential issues raised in the ratings and provides a more robust and user-friendly solution for populating your table with selected checkbox values.
GranSupt
Feb 20, 2024Copper Contributor
Thanks for taking the time to help me. I especially like that you explained each line, that helps a lot for someone like myself just learning VBA. I copied and pasted your code.
At first I was getting a compile - expected end of statement error highlighting the words "Do While" and "Loop". I am getting a syntax error on both the colOffset = 9 Do While ws.Cells(lastRow, colOffset).Value <> "" and colOffset <= and colOffset = colOffset + 1 Loop lines.
When I copied your code it was all one long line so I broke it up the best way I thought. Maybe I messed that up?
(I am using office 365 64 bit version if that makes any difference).
At first I was getting a compile - expected end of statement error highlighting the words "Do While" and "Loop". I am getting a syntax error on both the colOffset = 9 Do While ws.Cells(lastRow, colOffset).Value <> "" and colOffset <= and colOffset = colOffset + 1 Loop lines.
When I copied your code it was all one long line so I broke it up the best way I thought. Maybe I messed that up?
(I am using office 365 64 bit version if that makes any difference).