Consulting

Results 1 to 6 of 6

Thread: copy rows with checkbox

  1. #1

    copy rows with checkbox

    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
    Last edited by Paul_Hossler; 04-01-2023 at 09:28 AM. Reason: Added code tags to supplied code

  2. #2

  3. #3
    Quote Originally Posted by arnelgp View Post


    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

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    here i made a demo from the linked i gave on post#2.
    Attached Files Attached Files

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Due to lack of interest in this thread by RuneDefour, I am going to close it.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •