Checkbox problems

randomdrums

New Member
Joined
Jan 1, 2025
Messages
6
Office Version
  1. Prefer Not To Say
Platform
  1. MacOS
Hi all, new forum member here so please be kind and let me know if ever I haven't posted in the right place or the right thing.

I am having problems with checkboxes in a very large Workbook that involves multiple sheets and what not, but I will try to be as precise as possible.

This particular worksheet is an Invoice register, which has a few columns and that automatically get added via another macro directly from my Invoice Template sheet. At the end of each row of data, I have setup a checkbox. The idea is that when the invoice is paid, the checkbox gets clicked and the infomation in that invoice's row get put into a table in another worksheet, making it easy to keep track of what has been paid or not, and then have the "Transactions" page calculate all my tax and income tax stuff (which already contains huge amounts of formulas and other information).
After writing it all out, I get a Run-Time Error 1004 on the couloured line and I'm not sure how to fix it. Any help would be appreciated.

Here is the VBA code for said Macro, and I will also include a screenshot of the sourceSheet and targetSheet as well. I will also note that each Checkbox is cell linked to the cell is it in/on, but the TRUE or FALSE statement is just hidden by making the text white.

VBA Code:
Sub TransferDataToTransactions()

    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim nextFreeRow As Long
    Dim dataToCopy As Variant
    Dim i As Long
    Dim lastRow As Long
    Dim currentRow As Long
    Dim chkBox As CheckBox
    
    Set sourceSheet = ThisWorkbook.Sheets("Invoice Record") ' Change to your source sheet name
    Set targetSheet = ThisWorkbook.Sheets("Transactions") ' Change to your target sheet name
    
    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "B").End(xlUp).Row
    
    For i = 1 To lastRow
[COLOR=rgb(250, 197, 28)]        Set chkBox = sourceSheet.CheckBoxes("CheckBox" & i)[/COLOR]
        If chkBox.Value = 1 Then
        dataToCopy = Array(sourceSheet.Cells(i, "B").Value, sourceSheet.Cells(i, "C").Value, sourceSheet.Cells(i, "D").Value, sourceSheet.Cells(i, "E").Value)
            
            ' Find the next free row in the target sheet's table
            nextFreeRow = targetSheet.ListObjects("TransactionTable").ListRows.Count + 1 ' Change "TransactionTable" to your table name
            
            ' Copy data to the next free row in the target sheet
            targetSheet.ListObjects("TransactionTable").ListRows.Add
            targetSheet.Cells(nextFreeRow, 1).Value = Date
            targetSheet.Cells(nextFreeRow, 2).Value = dataToCopy(0)
            targetSheet.Cells(nextFreeRow, 3).Value = dataToCopy(1)
            targetSheet.Cells(nextFreeRow, 5).Value = dataToCopy(2)
            targetSheet.Cells(nextFreeRow, 6).Value = dataToCopy(3)
            
            ' Sort after each row is added
            ActiveWorkbook.Worksheets("Transactions").ListObjects("TransactionTable").Sort. _
                SortFields.Clear
            ActiveWorkbook.Worksheets("Transactions").ListObjects("TransactionTable").Sort. _
                SortFields.Add2 Key:=Range("TransactionTable[[#All],[AAAA-MM-DD]]"), SortOn _
                :=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With ActiveWorkbook.Worksheets("Transactions").ListObjects("TransactionTable"). _
                Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End If
    Next i

End Sub
 

Attachments

  • Screenshot 2025-01-01 at 11.20.07 PM.png
    Screenshot 2025-01-01 at 11.20.07 PM.png
    135.6 KB · Views: 8
  • Screenshot 2025-01-01 at 11.20.49 PM.png
    Screenshot 2025-01-01 at 11.20.49 PM.png
    86 KB · Views: 8

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
As far as the colored line goes, when you inserted the checkboxes on the sheet - did the names appear (in the name box) as "Checkbox1" or as "Check Box 1"? Or did you change the names to be exactly as you have "CheckBox" in your code (no spaces)?
 
Upvote 0
Update: I modified the Macro that add the data from my invoice to my Invoice Record sheet by having it add a Checkbox automatically at the end of each row in Column F, which automatically links to the Cell of that row and adds the Macro that I am trying to fix to the checkbox automatically as well. However, the Macro that needs fixing still doesn't work.
 
Upvote 0
I wasn't meaning the text box, I meant the actual name from the name box (right click on the first check box and look at the name - top left of the screen). If I add a check box to a sheet & refer to it in code as "CheckBox1" - the code cannot find a check box of that name & will fail. But if I refer to it as "Check Box 1" then I don't have an issue.
 

Attachments

  • Picture1.png
    Picture1.png
    12.3 KB · Views: 3
Upvote 0
So I just looked and the Checkbox does have a name. See inclosed screenshot. So now what do I do?
 

Attachments

  • Screenshot 2025-01-02 at 12.24.17 AM.png
    Screenshot 2025-01-02 at 12.24.17 AM.png
    94.8 KB · Views: 7
Upvote 0
You see the problem, your code would be expecting "CheckBox7" on that row - a long way from what it's actually called. Normally I wouldn't refer to check boxes by their exact name in code for this very reason - you can soon lose track of what they're actually called. I prefer to loop through each checkbox on the sheet, along the lines of:

VBA Code:
Dim cx as Excel.CheckBox
For Each cx In ActiveSheet.CheckBoxes
If cx.Value = xlOn Then
do your actions here if the check box is true

And to refer to the row the checkbox is on, you can use something like:

VBA Code:
cx.TopLeftCell.Row
 
Upvote 0
But in your code the name of the checkbox:
VBA Code:
"CheckBox" & i
See Check Box 2157 difference or not? Also the checkbox number should have the same as the row number, like CheckBox7
.
 
Upvote 0
Ok sorry guys, I'm more of a noob that I thought and I'm not following you here... a little more detail would be appreciated
 
Upvote 0
I can't spend time on this now, but if you can provide an XL2BB - Excel Range to BBCode sample of your source & target sheets (you can disguise any sensitive data, and only a few records would be needed) then I could show you an alternative method of achieving your aim - just need to know if there's any other way of marking a record as "paid" other than the checkbox. It'll be tomorrow (my time) before I can look at it.
 
Upvote 0

Forum statistics

Threads
1,225,131
Messages
6,183,036
Members
453,146
Latest member
scarabeovini

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