+ Reply to Thread
Results 1 to 3 of 3

Search for a value in a column and copy row to new sheet for all matching values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-20-2010
    Location
    Pretoria, South Africa
    MS-Off Ver
    Excel 2016
    Posts
    151

    Cool Search for a value in a column and copy row to new sheet for all matching values

    I have a button with macro code. When you click the button, it searches for a value in a column and copy the entire row to a new sheet for all matching values.

    THIS IS THE MACRO CODE:
    Sub MyBestSystems_Click()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
        
        On Error GoTo Err_Execute
        
        'Start search in row 4
        LSearchRow = 4
        
        'Start copying data to row 2 in Sheet2 (row counter variable)
        LCopyToRow = 2
        
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
            
            'If value in column A = "MY BEST SYSTEMS", copy entire row to Sheet2
            If Range("A" & CStr(LSearchRow)).Value = "MY BEST SYSTEMS" Then
                
                'Select row in ALL_SP's to copy
                Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
                Selection.Copy
                
                'Paste row into MY BEST SYSTEMS_A in next row
                Sheets("MY BEST SYSTEMS_A").Select
                Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
                ActiveSheet.Paste
                
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
                
                'Go back to ALL_SP's to continue searching
                Sheets("ALL_SP's").Select
                
            End If
            
            LSearchRow = LSearchRow + 1
            
        Wend
        
        'Position on cell A3
        Application.CutCopyMode = False
        Range("A3").Select
        
        MsgBox "All matching data has been copied."
        
        Exit Sub
        
    Err_Execute:
        MsgBox "An error occurred."
        
    End Sub
    The problem I have is that I want the code NOT to copy the ENTIRE row but only up to Column O. Please see attached file as an example...
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Search for a value in a column and copy row to new sheet for all matching values

    Try this on a test copy of your workbook:

    Sub MyBestSystems_Click()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
        
        On Error GoTo Err_Execute
        
        'Start search in row 4
        LSearchRow = 4
        
        'Start copying data to row 2 in Sheet2 (row counter variable)
        LCopyToRow = 2
        
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
            
            'If value in column A = "MY BEST SYSTEMS", copy entire row to Sheet2
            If Range("A" & CStr(LSearchRow)).Value = "MY BEST SYSTEMS" Then
                
                'Select row in ALL_SP's to copy
                Range(Cells(LSearchRow, "A"), Cells(LSearchRow, "O")).Copy
                
                'Paste row into MY BEST SYSTEMS_A in next row
                Sheets("MY BEST SYSTEMS_A").Range("A" & LCopyToRow).PasteSpecial xlPasteValues
                
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
                
                'Go back to ALL_SP's to continue searching
                Sheets("ALL_SP's").Select
                
            End If
            
            LSearchRow = LSearchRow + 1
            
        Wend
        
        'Position on cell A3
        Application.CutCopyMode = False
        Range("A3").Select
        
        MsgBox "All matching data has been copied."
        
        Exit Sub
        
    Err_Execute:
        MsgBox "An error occurred."
        
    End Sub
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Forum Contributor
    Join Date
    04-20-2010
    Location
    Pretoria, South Africa
    MS-Off Ver
    Excel 2016
    Posts
    151

    Re: Search for a value in a column and copy row to new sheet for all matching values

    Brilliant it works 100%!! Thanks very much for the quick response...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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