+ Reply to Thread
Results 1 to 9 of 9

Table search and position output question

  1. #1
    Registered User
    Join Date
    05-21-2007
    Posts
    79

    Table search and position output question

    I haven't had any experience with VBA, but I have had some experience with C++, and I know that in C++, I could code a couple nested loops to run through a table (matrix) by row to search for an particular entry.

    What I'd want to do when the entry was found would be to store the location (row/col) in some local variable, then output the first entry of the row and column of that cell (the headings). This would let me output something like the date in month/year format that a specific record occurred.

    Anyone suggestions?

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Take a look at the VBA .Find method. The Excel VBA help has a useful example
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    It looks like the value that I'd be interested in is what would be stored in the firstAddress variable, as that would be the reference to the location of the match. I'm not actually interested in changing any values, so the c.Value reassignment can be omitted. Since each entry in my table is unique, this method should only assign firstAddress one time.

    Here's the next question, I guess:

    If I write this into a macro, how would I structure the thing to take in a value and return the value of firstAddress?

    What I'd like would be something like Find_Match(55) to be my function, where 55 is what I want to search for, and upon writing that into a cell, the displayed result is something like B7.

    The ultimate goal will be to eventually display the row and column header for that cell, so if there's a better approach to this end than by going through the cell address, I'd probably prefer that.

    Sorry if I'm not getting somet of this right. I'm still really fuzzy on how to define variables and inputs and impliment macros.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I'm not sure if his is what you want. It will find an entry & store the row & column numbers of that entry. Assuming, the headers are in Row1 it will return the relevant column's header

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    I have my row headings in column A, and my column headings in row 1. Having a way to store the row and column numbers of the cell containing the sought entry is awesome, but I'm wondering now if there's a way to add to the macro some output of the row and column headers for the entry it finds.

    Also, I'm not sure if I'm entering the code correctly, but the VBA compiler is having trouble when I try to run the macro. It's indicating some "non optional" problem with the Sub FindIt() line. I have no idea what that means.

    I hate being a bother, but the VBA help topics can't really teach me the object properties and syntax I'm having trouble with.

    Another question is, will this code, in some final form, simply display the result in the cell, or will there be some "MsgBox" popping up? For ease of use (this function will mainly be a time-saving shortcut to looking up tabled values from a large worksheet), it'd probably be best if there were just a cell into which a user can type the value they wish to find, and the output could exist in another cell below, displaying the row and column header of the entry once it is found in the table.

    Does the MsgBox Cells(1, c).Value line provide the output?
    Does the str line cause a pop-up field? Would there be a way to allow the input in some form of typing =FindIt(55)?

    I'm very thankful for all the help already.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    can you attach your workbook with the code in?

  7. #7
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    This is the table I'm working with. I want to be able to add a calculation field somewhere in the workbook that, when a value from the table is typed in, the month and year are returned. So, instead of someone having to dig through the table for 2550, they can just type it in and get the date they want.

    What would be perfect would be some function that I could type in another sheet, with the number as an argument, and then upon entering that, the return would be the date, formatted as "Month, Year". I don't know how to go about the formatting, really, but the code you listed before looked like a really good step ahead of where I've been stuck.
    Attached Files Attached Files

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your macro is in the wron place, it should be in a Standard Module

    I have added a macro to this workbook that does what I think you want.

    '
    Please Login or Register  to view this content.
    Last edited by royUK; 11-03-2008 at 12:09 PM.

  9. #9
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    Awesome work, Roy. You saved me a headache, I'm sure!

    Though I'll probably have to learn some VBA sometime...

    Thanks though!

+ 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