+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: search a string and copy rows thru vba

  1. #1
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Question search a string and copy rows thru vba

    I want to search sheet2 for some word or number that i put in a textbox on sheet1 and copy the rows from sheet2 to sheet1. In any colum.

    i had found a code and adjusted that, but it does not the copying and pasting.

    could anyone help me please
    here is the code:
        Sub SearchForString()
        Sheets("sheet1").Range("A18:J50000").ClearContents
        Range("A18").Select
        
            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 18 in sheet1 (row counter variable)
            LCopyToRow = 18
            Sheets("sheet2").Select
            While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
                'If value in column C = "", copy entire row to sheet1
                If Range("c" & CStr(LSearchRow)).Value = Sheets("sheet1").Textboxing.Value Then
            
    
                    'Select row in sheet2 to copy
                    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
                    Selection.copy
    
                    'Paste row into sheet1 in next row
                    
                    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
                    Sheets("sheet1").Select
                    ActiveSheet.Paste
    
                    'Move counter to next row
                    LCopyToRow = LCopyToRow + 1
    
                    'Go back to sheet2 to continue searching
                    Sheets("sheet2").Select
    
                End If
    
                LSearchRow = LSearchRow + 1
    
            Wend
    
            'Position on cell A18
            Application.CutCopyMode = False
            Sheets("sheet1").Select
            Range("A18").Select
    
            MsgBox "All matching data has been copied."
    
            Exit Sub
    
    Err_Execute:
            MsgBox "An error occurred."
    
        End Sub
    Last edited by promoboy2; 12-10-2010 at 09:52 AM.

  2. #2
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: search a string and copy rows thru vba

    Quote Originally Posted by davesexcel View Post
    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Here is a attachment with the xls file
    you see a add button to add data to the Articledata sheet and a textbox for looking up data.
    I added some radio button to select the search criteria (but this is not yet active, because i don't know how to program it)

    Its a sort of google for this file i won't

    thanks for looking in
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    Only problem was that the code was trying to match TEXT from the textbox with a value on your sheet2.
    See attached spreadsheet. You will notice that all I changed was .Value to .Text
    I also added Application.Screenupdating=False to make your macro run "smoother"
    If you need help with the coding behind the rario buttens, just shout.

    Regards:
    Dan
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: search a string and copy rows thru vba

    Quote Originally Posted by DanExel View Post
    Only problem was that the code was trying to match TEXT from the textbox with a value on your sheet2.
    See attached spreadsheet. You will notice that all I changed was .Value to .Text
    I also added Application.Screenupdating=False to make your macro run "smoother"
    If you need help with the coding behind the rario buttens, just shout.

    Regards:
    Dan
    thanks for looking in to it

    I still have some questions:

    1) when i search for the string "7254" i get only 1 row instead of 4 rows

    2) is it possible to search for a range (A2:J65000) instead of 1 collum, or must i write as much macro's as rows i want to search at.

    3) my previous question has something to do with the radio buttons, how do i get them to work?

  5. #5
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    HI,
    The statement : "Sheets("Gegevens Inbrengen").Select" was slightly out of place.
    Find attachment with correction.
    Working on your next problem.
    Regards
    Dan
    Attached Files Attached Files
    Last edited by DanExel; 12-07-2010 at 03:20 AM. Reason: Left out the attachment

  6. #6
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    Hi
    Here is version 3.
    It solve your multi collumb search.(Question 2)
    Note the "OR" statements.
    Working on your 3rd question
    Regards
    Dan
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: search a string and copy rows thru vba

    Quote Originally Posted by DanExel View Post
    Hi
    Here is version 3.
    It solve your multi collumb search.(Question 2)
    Note the "OR" statements.
    Working on your 3rd question
    Regards
    Dan
    I just tried your last version and works fantastic.

    can't wait to see the radio buttons working.

    thanks for all your fine programming!

  8. #8
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    At last, here is your final vesrion.
    I opted for a Combo box rather than Radio buttens, but it does the same job.
    Hope it will meet your requierments.

    Regards
    Dan
    Attached Files Attached Files
    Last edited by DanExel; 12-10-2010 at 06:38 AM. Reason: I cant spell

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007,2010
    Posts
    6,843

    Re: search a string and copy rows thru vba

    Quote Originally Posted by DanExel View Post
    At last, here is your final vesrion.........
    Hi Dan,
    Are you missing the Named range "LocationList" in your example?

  10. #10
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Thumbs up Re: search a string and copy rows thru vba

    Quote Originally Posted by DanExel View Post
    At last, here is your final vesrion.
    I opted for a Combo box rather than Radio buttens, but it does the same job.
    Hope it will meet your requierments.

    Regards
    Dan
    I want to thank Dan for his work, this is what i wanted.

    regards promoboy2

    solved

  11. #11
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    Hi Daves,
    Do I have your name correct?
    Yes it seems like I lost it (probably deleted it)
    Dan

  12. #12
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007,2010
    Posts
    6,843

    Re: search a string and copy rows thru vba

    Quote Originally Posted by DanExel View Post
    Hi Daves,
    Do I have your name correct?
    Yes it seems like I lost it (probably deleted it)
    Dan
    Your userform.show will error without the range LocationList
    For Each cLoc In ws.Range("LocationList")
      With Me.cboLoc
        .AddItem cLoc.Value
      End With
    Next cLoc
    Without the range how can it load the userform?

  13. #13
    Registered User
    Join Date
    11-24-2010
    Location
    antwerpen
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: search a string and copy rows thru vba

    Hi Dan,

    I have adjusted the file a little bit and occurred an error what i can't solve.

    when i choose ALL there is no problem, but when i choose something else it gives an error.

    can you look into it please?

    and i am also wondering if it is possible to look for part of string using the Cstr function?
    (for looking to a part of a string) something like this "looking" when i give "look" in textbox it copys that row. (but only for the collum D "omschrijving") with the other collums its not necessary.
    or is it possible to do a search with % before or after the text to search.

    It already is verry good but when this option is possible its even better.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    Here is your fix for your error.
    will look into your new queries.
    The problem was in "Named ranges"
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    11-18-2010
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: search a string and copy rows thru vba

    You will have to use the instr funktion.
    if you need help, shout
    Regards
    Dan

+ 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.2.0