PDA

View Full Version : [SLEEPER:] copy rows with checkbox



RuneDefour
03-31-2023, 07:41 AM
when I press a button my table has to be expanded with 1 row. but also a checkbox has to be copied and its code behind it. How do I do this because it doesn't work this way? the line in bold is the error.


Private Sub CommandButton2_Click()
Dim lastRow As Long
Dim lastColumn As Long
Dim chkBox As CheckBox
'Determine the last row in the active worksheet
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'Determine the last column you want to expand
lastColumn = 7
'Insert a row above the last row
Rows(lastRow + 1).Insert
'Copy the formulas from the last row to the new row
Range(Cells(lastRow, 1), Cells(lastRow, lastColumn)).Copy Range(Cells(lastRow + 1, 1), Cells(lastRow + 1, lastColumn))
'Copy the last checkbox with his VBA code
Set chkBox = ActiveSheet.CheckBoxes(ActiveSheet.CheckBoxes.Count)
chkBox.Copy
ActiveSheet.CheckBoxes.Add(chkBox.Left, chkBox.Top + chkBox.Height + 5, chkBox.Width, chkBox.Height).Select
ActiveSheet.Paste
End Sub

arnelgp
03-31-2023, 09:06 PM
see the code (post#1) on this thread:
vba - Dynamically add checkboxes and event handler to a worksheet - Stack Overflow (https://stackoverflow.com/questions/39468821/dynamically-add-checkboxes-and-event-handler-to-a-worksheet)

RuneDefour
04-01-2023, 08:55 AM
see the code (post#1) on this thread:
vba - Dynamically add checkboxes and event handler to a worksheet - Stack Overflow (https://stackoverflow.com/questions/39468821/dynamically-add-checkboxes-and-event-handler-to-a-worksheet)



dear
this is not what i am looking for. when I press a button, a full row should be added to the row with a checkbox that immediately gets code from a checbox that I use before

Aussiebear
04-01-2023, 04:06 PM
RuneDefour, I have re read your post a number of times but still remain somewhat confused with your intent.

Do you actually have a table, as in a defined table on your worksheet or simply a worksheet with data that looks like a table? If so then try this to add a new row


Private Sub CommandButton_ Click(tableName As String, NewData As Range)
Dim ws As Worksheet
Dim tbl As ListObject
Dim col As Integer
Dim lRow As Range
Set ws = Range(tableName).Parent
Set tbl = ws.ListObjects.Item(tableName)
'First check if the last table row is empty; if not, add a row
If tbl.ListRows.Count > 0 Then
Set lRow = tbl.ListRows(tbl.ListRows.Count).Range
If Application.CountBlank(lastRow) < lRow.Columns.Count Then
tbl.ListRows.Add
End If
End If
'Copy NewData to new table record
Set lRow = tbl.ListRows(tbl.ListRows.Count).Range
lRow.Value = NewData.Value
End Sub

arnelgp
04-02-2023, 02:05 AM
here i made a demo from the linked i gave on post#2.

Aussiebear
04-05-2023, 04:24 AM
Due to lack of interest in this thread by RuneDefour, I am going to close it.