+ Reply to Thread
Results 1 to 3 of 3

Advanced Find and Replace function or macro help

  1. #1
    Registered User
    Join Date
    01-11-2007
    Posts
    1

    Cool Advanced Find and Replace function or macro help

    Hi all,

    I have a accounting flat file that I open in Excel. It lists a file number and a GL code. Based on the start of the file number and its existing GL code, I need to change the GL code.

    Example

    A B C D E F G H
    I139708 11/15/06 ST-53017882 12002 GLOBISCL 46.49 6040-3-5 JMFILE
    I139728 11/16/06 ST-51017809 12003 ACLSHIPP 236.16 6050-2-4 JMFILE
    I139728 11/16/06 BT-54016873 12002 GLOBISCL 136.29 6050-2-4 JMFILE

    I need to have a forumula or macro that will search each line and compare against multiple options
    If ST-51 and 6050-2-4 , then change 6050-2-4 to 6050-7-4
    If ST-53 and 6040-3-5 , then change 6040-3-5 to 6040-7-5

    I have about 25 conditions to check on each line. If match change GL code to new GL code. If not match, leave alone and check the next row. This task occurs every week day.

    Any Help Appreciated.
    Rgds
    John

  2. #2
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    You could do this with a macro. I am going to assume that since you posted this under Worksheet functions you would prefer to do this with a function is you could.

    Macro or function, I would start the same way. I assume the rules could change every day, so you do not want them "hard coded". Therefore I set up a workbook with one sheet for the data and another for the "rules".

    I used logical array expressions with sumproduct to return the row number where both criteria are matched, or zero if no match. Then, used INDEX function to use the row number (if there is one), or the current GL code if no match was found.

    You could combine the INDEX and SUMPRODUCT expressions to get a single formula. But, I suspect that this logic is challenging enough as it is for most folks.

    Obviously, with this approach, you would need to do some manual work. Namely, replace the functions with their results (Edit >> Copy, then Edit >> Paste Special Values) and replace the current GL code column with the results in the final column.

    If you prefer the macro approach, respond accordingly.


    ---------- ADDED LATER ----------------------

    I should mention ... there will be an issue if ever a case arises where the data matches criteria ("rules") in more than a single row. Namely, since this is a SUMPRODUCT, it will return the SUM of the row numbers ... which is not what you want. I am thinking there will never be such a case. But, I need to warn you of that.

    This could be re-written to take that possibility into account and return either the first match (minimum row number), or the last match (maximum row number). But, to do that, you could no longer use SUMPRODUCT (at least I am not smart enough to know how). In such a case you would need to use Array Formulas.
    Attached Files Attached Files
    Last edited by MSP77079; 01-11-2007 at 10:12 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    07-11-2004
    Posts
    851
    here's a macro for you - assumes your data is in columna a-h

    Sub macro1()
    firstrow = Cells(1, 1).End(xlDown).Row
    lastrow = Cells(64000, 1).End(xlUp).Row
    If firstrow = lastrow Then firstrow = 1
    Columns("G:G").NumberFormat = "@"
    For i = firstrow To lastrow
    If Left(Cells(i, 3), 5) = "ST-51" And Cells(i, 7) = "6050-2-4" Then _
    Cells(i, 7) = "6050-7-4"
    If Left(Cells(i, 3), 5) = "ST-53" And Cells(i, 7) = "6040-3-5" Then _
    Cells(i, 7) = "6040-7-5"
    ' add more conditions here if you need
    Next i
    End Sub
    not a professional, just trying to assist.....

+ 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