Results 1 to 4 of 4

Thread: removing references in excel formulas

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    removing references in excel formulas

    i am looking for a piece of vb code which replaces all references in a cell formula. the references may be to other cells, other worksheets or other workbooks, so that the new formula will just have variables. any help will be appreciated.

  2. #2
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: removing references in excel formulas

    (in module)
    VB Code:
    1. private sub My_Sub()
    2. dim tmpString as string
    3.  
    4. tmpString = ReplaceText(range("A1").formula, "[Otherworkbook.xls]Sheet1!", "")
    5. end sub
    6.  
    7. Function ReplaceText(SourceString As String, ReplaceAllofThese As String, WithThese As String)
    8. strModulePart = "Replacing Text Module"
    9.     Dim temp As Variant
    10.     temp = Split(SourceString, ReplaceAllofThese)
    11.     ReplaceText = Join(temp, WithThese)
    12. End Function


    This removes references to other workbooks/sheets in a specified cell,
    you would have to do a loop to do them all.


    eg
    VB Code:
    1. for x = 1 to 20     'NO_OF_ROWS_TO_CHECK
    2.     for y = 1 to 30       'NO_OF_COLUMNS_TO_CHECK
    3.         tmpString = ReplaceText(cells(y, x).formula, "[Otherworkbook.xls]Sheet1!", "")
    4.     next y
    5. next x
    Last edited by arnie1066; Jun 1st, 2006 at 02:51 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: removing references in excel formulas

    thanks a lot. i was actually looking for a piece of code which removes references inside the same worksheet as well as other work sheets/workbooks. so it is supposed to detect a cell reference(i.e. A19) / or worksheet reference inside a formula and go on resolving it until there is no more references.

    split function certainly helps but can i use wild cards in delimiters?

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: removing references in excel formulas

    Eager:

    What you are trying to do is going to be very tricky depending on the range of cells used on all of the sheets, and the number for substitutions you are trying to implement.

    As an example, if one of your "variables" replaces a reference to cell "A1", you can't use a simple "InStr" because you need to differentiate that from cells "A10", "A11", "A199", "AA1", "BA123", etc., remembering that there can be up to 256 columns and 65,536 rows in the used range of a Worksheet. Also, remember that "$A$19", "$A19", and "A$19" all point to the same cell.

    Here is a process idea that might help you for "A19" - say you want to replace it with a reference named "SalesTax":

    1) Iterate through all cells in the UsedRange/ActualUsedRange of the sheet.
    2) You want the aCell.Formula String to operate on.
    3) Do an Instr on the String for "A19"
    4) If found, make sure the character position BEFORE is NOT A-Z, and make sure there are no numerals 0-9 in the character position AFTER.
    5) If your candidate formula is still valid, REPLACE the substring "A19" with "SalesTax". (REPLACE is not available in older versions of VBA - '97).

    P.S. I think I read your post wrong. Are you trying to pull apart a SINGLE cell formula and replace ALL of the direct references in it with known variables? How many 'known variables' are candidates for this replacement process?
    Last edited by Webtest; Jun 3rd, 2006 at 01:47 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

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



Click Here to Expand Forum to Full Width