+ Reply to Thread
Results 1 to 6 of 6

Excel 2003 VBA: Find Method with Variable

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2011
    Location
    Bandera, Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Excel 2003 VBA: Find Method with Variable

    My problem is with the line beginning "Selection.Find."

    I want it to paste the clipboard contents into the search box, but it's
    remembering the specific value used when I set up the macro (i.e. copied the
    keystrokes).


        Selection.Copy
        Windows("Master TEST.xls").Activate
        Columns("D:D").Select
        Selection.Find(What:="359", After:=ActiveCell, LookIn:=xlFormulas,
        LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
            MatchCase:= _
            False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, 1).Select
        Range(ActiveCell, ActiveCell.Offset(0, 1)).Copy
        Windows("New.xls").Activate
        ActiveCell.Offset(0, 1).Select
        ActiveSheet.Paste
        ActiveCell.Offset(1, -1).Select
    Thanks for any help
    Last edited by DrNyet; 02-18-2011 at 08:41 AM. Reason: add code tags for newbie PM rules

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,806

    Re: Excel 2003 VBA: Find Method with Variable

    You need to get the search value from "somewhere". This could be from an InputBox or by putting the data in a cell. The value in the cell could be from a Data Validation drop down box based on data in the spreadsheet, that is, a list of (unique) values.

    Let's say the data you want to search for is in cell A1, the line would look like:

        Selection.Find(What:=Range("A1").Value, After:=ActiveCell, LookIn:=xlFormulas,

    But you don't need to select the column first; you can use:

        Columns("D:D").Find(What:=Range("A1").Value, After:=ActiveCell, LookIn:=xlFormulas,

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 VBA: Find Method with Variable

    TMShucks is correct in that using a cell is probably better than the clipboard, you don't really need to copy it into memory first.

    But this routine WILL put the current clipboard contents into a usable variable:

    Dim MyData As DataObject:   Set MyData = New DataObject
    Dim sClipText As String
    
    'Remember to copy something into memory before running this
    
    On Error Resume Next
    MyData.GetFromClipboard         ' Get data from the clipboard.
    If Err <> 0 Then
        MsgBox "Data on clipboard is not text."
        Exit Sub
    End If
    
    sClipText = MyData.GetText(1)   ' Assign clipboard contents to string variable.

    To use this method you will need to add a reference to Microsoft Forms 2.0 Library. (VBA > Tools > References)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,320

    Re: Excel 2003 VBA: Find Method with Variable

    Based on your original code:
    DIm rngFound as Range
        set rngfound = Workbooks("Master TEST.xls").Activesheet.Columns("D:D").Find(What:=Selection.Value, LookIn:=xlFormulas,
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext,
            MatchCase:=False, SearchFormat:=False)
        If not rngFound is Nothing then
           rngFound.Resize(, 2).Copy selection.offset(0, 1)
    I assumed you were copying back to the current workbook.

    It also appears that this is probably inside a loop, in which case all the selecting you are doing is unnecessary and slows the code down - if you post the full code, I'm sure we can streamline it.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel 2003 VBA: Find Method with Variable

    e.g.

    on error resume next
    selection.offset(, 1).resize(,2)=Workbooks("Master TEST.xls").Activesheet.Columns(4).Find(Selection.Value, ,xlFormulas,xlPart).Resize(, 2).Value



  6. #6
    Registered User
    Join Date
    02-15-2011
    Location
    Bandera, Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Excel 2003 VBA: Find Method with Variable

    Thank you for the responses. You have been very generous.

    This is the entire code other than the subroutine name and close.

    The goal is to enter a item number in a "New" workbook and have it retrieve the description and color from a Master workbook, and add those to New, to be used as a list for production.

    I am sadly unversed in VBA and only know enough to look for examples to incorporate. The two workbooks involved are small so speed is not much of an issue although I appreciate any improvement and the opportunity to understand more about it.

+ 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