+ Reply to Thread
Results 1 to 15 of 15

If checkbox is checked then copy entire row to new tab

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    If checkbox is checked then copy entire row to new tab

    Hi!

    I've been searching this forum without finding the answer to my problem.

    In my first tab (GEO-Export) I have a several rows with data. The Column A is filled with checkboxes on every row.

    All I want to happen is that when a checkbox is checked, the entire row should be copied to a another tab (City) .
    So if I check 10 checkboxes then 10 rows should appear in my second tab.
    It's ok if I have to press a button after I have checked the boxes for the rows to be copied.

    I found this code online but I cant get it to work
    Sub CopyRows()
     
    'Go through each check box in active sheet
    For Each chkbx In ActiveSheet.CheckBoxes
         
        'If check box is enabled
        If chkbx.Value = 1 Then
     
            'Go through each row on worksheet
            For r = 1 To Rows.Count
     
                'Check if checkbox is on the same row
                If Cells(r, 1).Top = chkbx.Top Then
     
                    'Simplify syntax
                    With Worksheets("City")
     
                        'Identify the cell right below the last non empty cell
                        LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
     
                        'Copy record from Sheet1 and paste to first empty row on Sheet2
                        .Range("A" & LRow & ":ND" & LRow) = _
                        Worksheets("GEO-Export").Range("B" & r & ":ND" & r).Value
                    End With
                     
                    'Exit For Loop
                    Exit For
                End If
            Next r
        End If
    Next
     
    End Sub
    Any help would be appreciated
    Last edited by alansidman; 10-10-2019 at 10:39 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    It will be nice to send an Excel sample file to avoid to rebuild the layout for checking the macro
    BTW you forgot the Code tags ...!
    Last edited by PCI; 10-10-2019 at 01:42 PM.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    I tried, but since I'm new to this forum I'm not allowed to put any links in my posts

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    See file attached
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    Nice!

    Attached you will find my document.
    There are some other codes there as well but I think you will understand what I'm trying to do.

    BTW the document is in Swedish so the tab Städer = City

    Also if I press the button AddCB everything works fine when I press the button Copy.

    But I need (I thnk) to have the checkboxes I put in manually because they are triggering some other codes that I need.


    Thank You!
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    See how next code help: An update of your file attached
    Option Explicit
    Sub CopyRows()
    Dim chkbx As CheckBox
    Dim r  As Integer
    
        'Go through each check box in active sheet
        For Each chkbx In ActiveSheet.CheckBoxes
            'If check box is enabled
            If chkbx.Value = 1 Then
                'Go through each row in the used range
                For r = 1 To ActiveSheet.UsedRange.Rows.Count
                    'Check if checkbox is on the same row
                    If Cells(r, 1).Top = chkbx.Top Then
                        Rows(r).Copy Destination:=Worksheets("City").Cells(Rows.Count, "B").End(3)(2, 0)
                        Exit For
                    End If
                Next r
            End If
        Next
    End Sub
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    PCI!

    Thank you so much! Works almost perfect

    One "last" question, is it poosible to skip to copy the first column? The one with the checkbox?
    So start to copy the row from column B and then copy it to the other tab?

    Again thank you!

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,982

    Re: If checkbox is checked then copy entire row to new tab

    @2Times

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  9. #9
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    Thank you alansidman, I will make sure to do it the right way next time.

  10. #10
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: If checkbox is checked then copy entire row to new tab

    amended code and another method of addin checkboxes with link to row
    Option Explicit
    Sub CopyRows()
    Dim chkbx As CheckBox
    Dim r  As Integer
    Dim lcol As Long
    lcol = Cells(1, Columns.Count).End(xlToLeft).Column
        'Go through each check box in active sheet
        For Each chkbx In ActiveSheet.CheckBoxes
            'If check box is enabled
            If chkbx.Value = 1 Then
                'Go through each row in the used range
                For r = 1 To ActiveSheet.UsedRange.Rows.Count
                    'Check if checkbox is on the same row
                    If Cells(r, 1).Top = chkbx.Top Then
      Cells(r, 2).Resize(, lcol).Copy Destination:=Worksheets("City").Range("a" & Rows.Count).End(xlUp)(2)
                        Exit For
                    End If
                Next r
            End If
        Next
    End Sub
    Public Sub chbox_forRows()
    Dim Frow As Variant: Dim Lrow As Variant
    Dim i As Long
    Dim str As String
    str = InputBox("Enter first and Last row separated by -")
    Frow = InStr(str, "-")
    Frow = Left(str, Frow - 1)
    Lrow = Right(str, Len(str) - InStr(str, "-"))
    'MsgBox "Add checkboxes from row " & Frow & " to " & Lrow
    For i = Frow To Lrow
    XAddCheckBoxes Cell:=Range("a" & i)
    Next
    End Sub
    Private Sub XAddCheckBoxes(Cell As Range)
       With ActiveSheet.CheckBoxes.Add(Cell.Left, _
                                       Cell.Top, _
                                       Cell.Width, _
                                       Cell.Height)
          .LinkedCell = Cell.Offset(, 0).Address
          .Caption = Range(.LinkedCell).Row
       End With
    End Sub

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    One "last" question, is it poosible to skip to copy the first column? The one with the checkbox?

    See update
    
    Option Explicit
    Sub CopyRows()
    Dim chkbx As CheckBox
    Dim r  As Integer
    
        'Go through each check box in active sheet
        For Each chkbx In ActiveSheet.CheckBoxes
            'If check box is enabled
            If chkbx.Value = 1 Then
                'Go through each row in the used range
                For r = 1 To ActiveSheet.UsedRange.Rows.Count
                    'Check if checkbox is on the same row
                    If Cells(r, 1).Top = chkbx.Top Then
                        Range(Cells(r, 2), Cells(r, Columns.Count).End(xlToLeft)).Copy _
                         Destination:=Worksheets("City").Cells(Rows.Count, "A").End(3)(2)
                        Exit For
                    End If
                Next r
            End If
        Next
    End Sub

  12. #12
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    PCI!

    We are getting closer
    I hope you understand how much this helps.

    The only thin now that's missing is that in the tab with the checkboxes I have some cells with formulas. The cells with formulas in them are just empty in the City tab.

    Any suggestions?

  13. #13
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    Is to paste only values good enought ???
    If yes, see next code
    
    Option Explicit
    Sub CopyRows()
    Dim chkbx As CheckBox
    Dim r  As Integer
    
        'Go through each check box in active sheet
        For Each chkbx In ActiveSheet.CheckBoxes
            'If check box is enabled
            If chkbx.Value = 1 Then
                'Go through each row in the used range
                For r = 1 To ActiveSheet.UsedRange.Rows.Count
                    'Check if checkbox is on the same row
                    If Cells(r, 1).Top = chkbx.Top Then
                        Range(Cells(r, 2), Cells(r, Columns.Count).End(xlToLeft)).Copy
                        Worksheets("City").Cells(Rows.Count, "A").End(3)(2).PasteSpecial _
                         Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                        Exit For
                    End If
                Next r
            End If
        Next
    End Sub

  14. #14
    Registered User
    Join Date
    10-10-2019
    Location
    Sweden
    MS-Off Ver
    360
    Posts
    18

    Re: If checkbox is checked then copy entire row to new tab

    PCI!

    You are my hero, this is exactly what I was looking for.
    Thank you so much for taking the time to help me.

    If you ever are in Sweden I'm buying you a beer (or coffee)

  15. #15
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If checkbox is checked then copy entire row to new tab

    Good news.
    If you are please have a look on the reputation star at the bottom left

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Checkbox Copy and Paste info only when checked to another cell
    By MonTheEck in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-22-2018, 06:23 AM
  2. [SOLVED] When checkbox is checked it does not copy to new sheet. Only if not checked using false
    By thelisa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2013, 07:59 AM
  3. [SOLVED] VBA code to uncheck checkbox 2 & 3 if checkbox 1 is checked
    By hydz1213 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-25-2013, 03:10 AM
  4. Copy values if checkbox is checked
    By jtone32512 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-21-2012, 03:54 AM
  5. Delete entire row if checkbox is checked
    By jeshua56 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2012, 01:54 PM
  6. Copy row data for checked checkbox
    By Christeen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2011, 09:50 PM
  7. copy if checkbox checked to sheet two
    By BillyRogers in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2006, 04:35 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1