+ Reply to Thread
Results 1 to 4 of 4

Need help with searching for a string in column and pasting into new sheet

  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    Potomac
    MS-Off Ver
    2010
    Posts
    2

    Need help with searching for a string in column and pasting into new sheet

    Hi, I am new to macros and VBA on Excel.

    I need to figure out a way to search for a string in column D and then paste the entire row that string is in to a new sheet. I'd like for this to continue until I paste all of the rows of that string into the next sheet. This is what I got so far, and when I run it, it says an error occurs and then it says either press enter or paste. After I hit enter, it pastes the first row to the next sheet, but that's it. Also should I be doing this with a button?

    Thanks,

    Code:
    Sub SearchForString()

    Dim LSearchRow As Integer
    Dim LCopyToRow As Integer

    On Error GoTo Err_Execute

    'Start search in row 2
    LSearchRow = 2

    '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 D = "HS06", copy entire row to Sheet2
    If Range("D" & CStr(LSearchRow)).Value = "HS06" Then

    'Select row in Sheet1 to copy
    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
    Selection.Copy

    'Paste row into Sheet2 in next row
    Sheets("Sheet2").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste

    'Move counter to next row
    LCopyToRow = LCopyToRow + 1

    'Go back to Sheet1 to continue searching
    Sheets("evs2013_PRDF.csv").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

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Need help with searching for a string in column and pasting into new sheet

    Why not use Autofilter?

    Post a workbook so we can see your data structure.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    07-16-2014
    Location
    Potomac
    MS-Off Ver
    2010
    Posts
    2

    Re: Need help with searching for a string in column and pasting into new sheet

    I'm not sure how to use autofilter or what it does. I attached a sample file. For example, for column D I need to find everything that says "HS06"
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need help with searching for a string in column and pasting into new sheet

    Maybe:

    Please Login or Register  to view this content.
    copies to Sheet2

+ 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] VBA for searching string in a column and copy rows depending on string in adjacent cell
    By xprakash in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-20-2013, 05:58 AM
  2. Searching for a value, finding it on another file and pasting on a different column
    By kurtwagner in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-27-2012, 03:57 PM
  3. Replies: 0
    Last Post: 04-24-2008, 12:03 PM
  4. Replies: 2
    Last Post: 04-23-2008, 01:41 PM
  5. Searching for a string in a cell, and then creating a Sheet for that string
    By vmc62 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2008, 12:07 PM

Tags for this Thread

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