Consulting

Results 1 to 7 of 7

Thread: Solved: Find and select a cell based on user input

  1. #1

    Solved: Find and select a cell based on user input

    I am trying (unsucessfully) to create a macro that opens an InputBox and allows the user to enter a document name. Then I want to select the cell in a single column that contains that input. There are no duplicates.

    Thanks

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This might work for you.
    [VBA]Sub test()
    Dim userInput As String
    Dim rowFound As Variant
    Dim dataColumn As Range

    Set dataColumn = ThisWorkbook.Sheets("sheet1").Range("A:A"): Rem adjust

    Do
    userInput = Application.InputBox(userInput & "Which document do you want?", Type:=2)
    If userInput = "False" Then Exit Sub: Rem cancel pressed

    rowFound = Application.Match(userInput, dataColumn, 0)

    If IsError(rowFound) Then userInput = "That document not found. Try again." & vbCr
    Loop Until Right(userInput, 1) <> vbCr

    dataColumn.Parent.Activate
    dataColumn.Cells(rowFound, 1).Select
    End Sub[/VBA]

  3. #3
    Thanks for the help...

    It does not seem to find the document, which is a number not text, when I change "Dim userInput As String" to "Dim userInput As Integer" I get "Type Mismatch" on this line

    If userInput = "False" Then Exit Sub: Rem cancel pressed

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The looping structure et.al is dependent on userInput being a string. Leave userInput as string and change

    [VBA]rowFound = Application.Match(Val(userInput), dataColumn, 0) [/VBA]

  5. #5
    Works Like A Charm!!!!

    Thanks Mike....

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You're welcome.

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi John,
    If your question is answered, please mark it solved using the Tread Tools dropdown
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •