Need to make address that was located by my variable as activecell

  • Driving me crazy. I know that I'm close but can't seem to get the syntax right on the 'select' line: ActiveSheet.Range(sChoice).Select
    Column #2 should be considered as text as it has values such as "73g"
    This process does a search through a range. It works good and picks out the value of the cell as it should. (my MsgBox 'es prove that to me). My problem is that I need my cursor to go to that cell when it is picked, so that the user can see the whole row that is adjacent to it. Here is the code:



    Any help will be greatly appreciated
    Thanks Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Hi


    Seems the search result is not in ActiveSheet. You have to Activate the sheet where the search result is:

    Code
    Sheets("Search Trial").activate
    activesheet.range(schoice).select


    Regards


    Maqbool

  • Hi Magbool
    I added your suggestion and It still doesn't do what I need. Everything is on the same sheet. I'm creating a search box at the top of the page to go through around 200 items in col 'A', but needs to identify the cell in col 'B' next to it, as I have the 200 items set to run a macro, if one of them are selected. It works perfectly except I can't seem to find the simple syntax to make the cursor go to the cell where the variable sRes has chosen. The MsgBox sChoice shows the value of the cell I want to go to but the syntax:
    [SIZE=18px]ActiveSheet.Range(sChoice).Select won't go there...so frustrating... Application defined or object defined error Thanks Jimmy[/SIZE]

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Hello,


    You are over-complicating everything ...


    Why do you want to use Vlookup ... which is the worst ' spreadsheet ' function ...


    With a simple range.find ... life would be easier


    However, if you really want to stick to your approach



    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Just as an illustration ... version 2


    Code
    Sub SearchBox2()
    Dim rg As Range
    Dim sRes As String
    Dim sItem As String
            sItem = Sheets("Search Trial").Range("E1")
            Set rg = Sheets("Search Trial").Range("A4:B217")
            sRes = rg.Find(sItem).Address
            ActiveSheet.Range(sRes).Offset(0, 1).Select
    End Sub

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • I agree you would be better using .Find. It could be reduced to


    Code
    Sheets("Search Trial").Range("A4:B217").Find(Sheets("Search Trial").Range("E1").Value).Offset(, 1).Select


    Or with some error handling


    Option Explicit


    Code
    Sub SearchBox()
    On Error GoTo not_found
      Sheets("Search Trial").Range("A4:B217").Find(Sheets("Search Trial").Range("E1").Value).Offset(, 1).Select
       Exit Sub
    not_found:
       MsgBox Sheets("Search Trial").Range("E1").Value & " could not be found"
    End Sub
  • Thanks Carim. It works perfectly. The first one didn't work...it kept choosing the wrong item,,,not even close. I didn't know that vlookup was a crappy function...First time I use it...will be the last. I spent all afternoon yesterday searching the web for ideas to make it work.
    Thanks again
    Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Thanks Roy. Carim set me up with code that works fine. I'll keep your recommendation in my back pocket for future considerations.
    Thanks again
    Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Glad you could fix your problem ...:wink:


    Thanks for your Thanks ...AND for the Like ...:smile:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Hi Carim. Your welcome
    ("would be nice" question)
    Is there a line of code, I could add to the block that you provided (if there are multiple items with common data) that will search 'next'... IE: 'Ice Cream (Peach)' will be chosen instead of 'Peach' and stop without offering 'peach' as a choice. The user could just keep hitting the activation button until the proper item is selected.
    Found new issue also with it. If a word is misspelled I get a run-time error...can we add something to messagebox if there is an error so it doesn't kick out of the module?
    Thanks Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Hi again,


    For multiple searches, you would need to use .FindNext


    You can then locate all occurrences ...


    If you feel like digging into the specifics :


    https://excelmacromastery.com/excel-vba-find/


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Thanks Carim I'll check it out
    About the Misspelling in the search...can I set something up,so that it won't kick out of the module on error?...I could have a message box come up saying something like please retry your search?
    Thanks again
    Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Glad you could fix your problem ...:smile:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Hi Carim...I'm workng on [SIZE=20px]day 2 [/SIZE]and batting 100. (10am yesterday till 1:30am this morning then 11:00am this morning till now +... LOL). I never ask for help without first searching the net and trying my best to solve. Most of the time I'm ok but sometimes...GRRRRR.
    I appreciate your help in getting this to work, but am still struggling with a few specifics


    I got the code for .FindNext from the Excel Macro Mastery:and am running into problems with object errors. I've tried a million different variations...no luck just different error messages. I think that I've seen them all, these last 2 days
    I put in the fix (if sRes is nothing then) for "no match in the search", that I got from 'Excel Macro Mastery' & I get the error: [SIZE=18px]Compile error Type mismatch [/SIZE][SIZE=16px]on my variable sRes
    Here's my code now as it stands. HELP! :thanx:[/SIZE]

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Hello,


    Below is your macro to be tested



    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Once you have tested the macro .... feel free to share your comments

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • HEY CARIM
    You're astonishing...as usual. You saved the day [SIZE=18px]again.[/SIZE] I hope that someday I'll be as awesome as you. LOL...Ok how's that for buttering you up. The sub routine works perfectly. :yourock:
    Thanks again
    Jimmy

    Thanks for your help

    JimmyB


    Some plans fail on execution

    All plans fail without

    8)

  • Pleased to hear it is all sorted out ..:wink:


    Many Many Thanks for your very kind words ... AND for the Like ... :smile:



    P.S How do you know I am spreading plenty of Butter on my toasts ...??? :lol:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!