randomdrums
New Member
- Joined
- Jan 1, 2025
- Messages
- 6
- Office Version
- Prefer Not To Say
- Platform
- 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.
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