+ Reply to Thread
Results 1 to 11 of 11

Looking for a Record Match with a Macro

  1. #1
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    Exclamation Looking for a Record Match with a Macro

    Greetings, and thank you for your feedback

    It is quite certain that many of you can do this in your sleep so forgive my ignorance as I am new at this VBA thing. Lotus was easy but is now extinct. (well, not used that much anymore)

    The application is Excel from the MS Office 2003 Suite for Windows

    Here is my delimia
    I am trying to figure out how to write a macro that when invoked with a button, will search a column of numbers for a record match based on search criteria in another cell.

    The idea is, when a user selects or inputs a valid number using a drop-down list, a button is punched called "Get Record" The Records Stack is searched in the record number column until a match is found. Error handling with the drop down list should allow only a valid number for input, unless it is blank. (Not quite sure what to do about this one.)

    When the record is found, it is copied then "transposed-pasted" to the edit screen where the data is modified by the user. Once changes are done, the record is reposted to the same row in the records stack by using another button.

    The Input cell is at A1 and again, is a drop-down list created with validation. It currently has only a few test record numbers from pre-existing records residing in the stack. The entire record number range was selected in validation to allow future population when new records are created.


    The search cell is a drop-down list at A1
    The search range is A101 to A1000

    Any good ideas on how this can be accomplished is appreciated

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    =MATCH(A1,$A$101:$A$1000,0)+100 will return the row number of the data.

    This sounds like a VLOOKUP issue rather than VB.

    If you need VB, this will give you the row number of the matching data.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    Response to mikerickson

    Hi and thank you

    Nice... The Msgbox returning the row number is cool

    The code works well but how can it be simplified to just simply end at the found record; then exit this sub?

    The reason for this is to invoke another subroutine at the cursors current location to do something else.

    Thanks again

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Please Login or Register  to view this content.
    This will set foundCell to the matching cell. (If the search term is not there, foundCell is nothing)
    foundCell can be passed to your other rountine as an argument.

  5. #5
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    Smile Reply to mikerickson-round-2

    Thank you very much

    One last question; I hope.

    The code ends at the required target

    The data that was selected to be pasted where the cursor is now, how do I select the current location in order to invoke the pastespecial routine replacing the current record?

    I can do this by specifying a cell but don't know how to select a cell otherwise; ie found with a subroutine,

    I really appreciate your help in this..

    Thanks again

    By the way; I'm a newbe here, how do I post a favorable rating for you?

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Copy / PasteSpecial-Values is probably not the best way to go with this.

    I would use
    Please Login or Register  to view this content.
    since I don't know how many columns are invovled, the syntax in this situation would be
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    Reply to mikerickson-round-3

    Hi again

    First, let me apologize for this lengthy post but it was found to be necessary in order to Clarify.

    Second, thank you for your assistance to date; it was greatly appreciated but I am afraid there was a little confusion on the intent. I do apologize for this. Re-posting an edited record to where it came from is the current issue.

    There are a total of 32 col of data per completed record (22 cells input and 10 cells calculated)

    When the initial input is ready to be posted:
    When a new record gets processed following input, part of this process involves copying the input data to a formulas row beginning at A96.
    The formulas start at W96 of this row. This is done so that the formulas work on the new data just pasted before it.
    Once resolved, the entire row containing the Input data and the formulas are copied.

    SInce it is unknown to me as to how to target a cell found with a loop statement or any other programmed function, the copied range is pasted as values to an implicitly targeted row just above the record stack.

    The range was then selected and cut.
    The Cut range is then normally-pasted at the next empy row of the record stack when a "DoWhile" loop expression ends; completing the addition of the record; the Input screen is then returned for the next input.

    During the Edit:
    When a record gets recalled to an edit screen for any needed changes, the record is retrieved using lookup formulas that are copied from elsewhere in the sheet then pasted as values to the Edit Screen.
    The data is edited then gets copied and Transposed-pasted to the same Formula row that was used during the input of the record.

    Since the Now-edited row in 96 contains formulas again, it must be reposted as values; In essence, repeating the original input but getting posted to the same row in the Record stack it was recalled from for editing. Hence, the confusion-factor. Recalling a record to Edit is easy-enough but the "End-Edit and Post" is the problem.

    The posting of a "New" record was easy-enough to figure out as all that was needed was to find the next empty row in the records stack to post it This was accomplished using a "DoWhile and Loop" routine targeting the next empty cell in col-A; the record#

    This is where I am stuck;
    Finding the record match by using some sort of "Conditional-Loop" to locate it in the record stack. The search criteria would be the record number that is part of the edited record in the Edit screen.
    Once found, the edited record is simply pasted in place, replacing the original record.

    The original thought for this was to use a "DoWhile" or similar expression that was used in finding the next empty cell for the new record; but was unsuccessful in finding the right expression for this.

    I hope this clarified things.

    And thanks is given once-again for your help and consideration.
    Last edited by OverTheHill; 09-04-2007 at 11:41 AM. Reason: Clarification of original post

  8. #8
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    The code didn't work and Reason for this scribe

    The last bit-o-code I tried didn't seem to work and was the reason for the wordy scribe that preceeds this follow-up message.

    Again, my apologies for that.

    The objective again is to have a subroutine go down the Record number column of the record stack that starts at A101 (first record) until a match is found based on the search criteria of A1. This cell may also be a formula in the form of "=some cell"

    The elusive subroutine will pick up after the last action of current code.

    The following code is currently being used (less the needed code for what I would like for it to do); maybe this will help.

    Private Sub CommandButton7_Click()
    ' end_edit_post Macro
    MsgBox "This Option will only write the updated record to A98. The Subroutine needed to replace existing record is in process", vbInformation
    Range("BA3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Application.Goto Reference:="R96C1"
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=True
    ' endright Macro
    Range("A96").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("a98").Select

    'Insert subroutine to Scan record column until the selected cell =A1 then append the rest of the code to complete this function

    Tentative appended code for Replacing Record in stack
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False
    Range("b41").Select
    Range("ba4").Select

    End Sub

    Example;
    If the cell contents of A1 = 1012, the code needs to go down the record column (A, or C1) until 1012 is found. Once found, the subroutine exits, allowing the rest of the Macro to resume.

    The latest code attempt is here; thank you again for the attempt.
    Please Login or Register  to view this content.


    Thank you again for your efforts
    Last edited by OverTheHill; 09-04-2007 at 04:00 PM.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I've inserted the code into this routine. I hope it does what you want.

    1) if the contents of A1 are not in A101:A1000, it exits the sub inelagantly (without pasting).

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-03-2007
    Posts
    16

    Smile Reply to mikerickson-round-4


    I was going to use the Thumbs-Up in the title-line but in at least one culture, it is considered a rude gesture.

    I just wanted to thank you for your time.

    The routines you provided works well.

    I had to remove the periods in one line of the code preceeding the range expressions though; now looking like this...

    Please Login or Register  to view this content.
    Again; MANY THANKS;
    and Cheers

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You're welcome.

+ 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