+ Reply to Thread
Results 1 to 16 of 16

Macro to search for value contained in a cell

  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Macro to search for value contained in a cell

    How would I get a macro to search only in Column B of a worksheet for whatever is contained in cell A1 and when it is found copy that row and everything below?

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, ScabbyDog,

    leaves open to find out: copy where? Have a look at Find in VBA-Help to build a range-object from the row found and use the column from the bottom up to find the last row.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to search for value contained in a cell

    HHi Holger. I want it to copy and then paste in sheet3.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, ScabbyDog,

    maybe we can start with this code:
    Please Login or Register  to view this content.
    This will copy, if you need paste of formulas please come back.

    Ciao,
    Holger

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Macro to search for value contained in a cell

    Do you really need a macro for this?

    Enter the string to find in A2

    In say, C2, Drag/Fill Down
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    See this workbook

    [EDIT]
    Added Sheet3 to example
    Attached Files Attached Files
    Last edited by Marcol; 11-17-2012 at 08:28 AM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to search for value contained in a cell

    Works great Holger. How would I change the code so that if data was already in sheet3 it would paste it after the last used row?

    I'll need a macro Marcol as I'll be adding other bits and pieces to the code myself. But thanks for your help. For what I've asked it does work!

  7. #7
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to search for value contained in a cell

    Add (e.g) this to your code

    Please Login or Register  to view this content.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  8. #8
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to search for value contained in a cell

    Quote Originally Posted by oeldere View Post
    Add (e.g) this to your code

    Please Login or Register  to view this content.
    I don't want to clear everything in sheet3. I just want to add whatever is pasted to the bottom of the data already in sheet3

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, ScabbyDog,

    maybe like this:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  10. #10
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to search for value contained in a cell

    change

    Please Login or Register  to view this content.
    in

    Please Login or Register  to view this content.
    and give it a try on a copy of your workbook.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, oeldere,

    with your sniplet the first free line would be searched on the ActiveSheet and not on Sheet3.

    Ciao,
    Holger

  12. #12
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to search for value contained in a cell

    @HaHoBe,

    Hi, Holger,

    I'm no VBA expert and are willing to learn.

    1) I don't know what the word sniplet means (my main language is Dutch instead of English).

    Please Login or Register  to view this content.
    I don't understand.

    Please explain it to me, already said, i'm willing to learn.

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, oeldere,

    at least we can agree that English is not our native language.

    In the Code I used a With Construct for the ease and easiness of understanding. So anything inside that code which is preceded by a dot is relying on the actual With-Construct (not the proper expression for that - I know, call it lack of proper knowledge).

    I referenced With ActiveSheet so the line you suggested could be written like
    Please Login or Register  to view this content.
    If a dot goes ahead the part of
    Please Login or Register  to view this content.
    will rely on that, if no dot is there it will automatically relate to the active sheet in Excel.

    Your line of code will work when you amend it to read
    Please Login or Register  to view this content.
    Using a With-Construct will reduce the time for VBA to get through the Application-model to find the object to use - itīs of no great use here with the sniplets but has caused some headaches for me in other codes which go beyond the scope of Forums.

    I hope I could explain somehow a little better here what I meant with that line. If not feel free to ask here or PM me about it.

    Ciao,
    Holger

  14. #14
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to search for value contained in a cell

    my code
    Please Login or Register  to view this content.
    Holgers code
    Please Login or Register  to view this content.
    I'm glad you pointed me on this "problem".

    I'm trying to use it next time.

    Thanks Holger,

    (Danke Holger)

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to search for value contained in a cell

    Hi, oeldere,

    by looking at the code again I must apologize because your code should work flawlessly - thatīs an effect if you do more than one thing at the same time and mix different things up (and think you saw something in the code you believed to be there - sad but true).

    Sorry for not paying as much attention as I should have had - I doubt that the Rows.Count will be any different on any of the sheets (should be 2^20 for Excel from 2007 on).

    Again, my mistake - I think Iīll take a break for today.

    Ciao,
    Holger

  16. #16
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to search for value contained in a cell

    Hi Holger,

    No apologize needed.

    Normaly I test my code in an file, so i can see if it's working.

    But the Question Starter didn't add an file, so I couldn't test it.

    With my "weak" knowledge of VBA I believe from the start what you're telling me is right.

    I'm glad you pointed me on my code and correct it yourself.

    Thanks for the reply Holger.

    Have a nice weekend..

+ 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