+ Reply to Thread
Results 1 to 8 of 8

How to replace part of a formula with VBA

  1. #1
    Registered User
    Join Date
    06-26-2013
    Location
    Texas
    MS-Off Ver
    2010
    Posts
    5

    How to replace part of a formula with VBA

    Apologies if I have a hard time explaining what I'm doing up to the point where i'm having an issue...

    I have created a UserForm that edits a workbook based on user entry. The user selects a value from a combo box and enters a value in a text box. Based on the combo box selection it opens the applicable workbook, creates a new sheet in that workbook and renames it based on the text box entry. It then navigates to an existing template > select all > copy > navigate to the new sheet and paste. It then navigates to a "Summary" sheet which summarizes all information on existing sheets via formulas. The VBA navigates to the bottom, inserts a row then copies the values from above (Selection.FillDown). The formulas from above contain the sheet name from the above row. I'm trying to find/replace that sheet name within the formulas with the text box value, but having trouble doing so. Here's what I've got and everything works like I'd like up to this point:

    Please Login or Register  to view this content.
    ActiveCell.Offset(-1,1) contains the text that needs to be replaced (in formula below: Old Sheet Name) and NewProjectName is the text box value that needs to go into the formula (New Sheet Name), which is essentially this:
    ='Old Sheet Name'!C$421+'Old Sheet Name'!G$421+'Old Sheet Name'!K$421+'Old Sheet Name'!O$421+'Old Sheet Name'!S$421+'Old Sheet Name'!W$421+'Old Sheet Name'!AA$421+...etc

    Part of the problem is that it is not consistent whether or not the existing sheet name is a single word or multiples with spaces; e.g. it could be MySheet instead of Old Sheet Name

    Thanks in advance

  2. #2
    Registered User
    Join Date
    06-26-2013
    Location
    Texas
    MS-Off Ver
    2010
    Posts
    5

    Re: How to replace part of a formula with VBA

    It seems that this can be done with an If / Then based on determining if ActiveCell.Offset(-1,-1).Value contains a space, but I dont know how to do this...

    Please Login or Register  to view this content.

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: How to replace part of a formula with VBA

    Assuming your ActiveCell is the correct cell that needs formula replaced, try changing that Selection.Replace line to these:

    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  4. #4
    Registered User
    Join Date
    06-26-2013
    Location
    Texas
    MS-Off Ver
    2010
    Posts
    5

    Re: How to replace part of a formula with VBA

    Thx for the response.

    No dice...same result I was getting with nothing being replaced.

    I know it is selecting the right ActiveCell, which is actually a range of four cells that contain this formula.

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: How to replace part of a formula with VBA

    Since it's a range of 4 cells, then which cell is ActiveCell.Offset(-1, -1) ?

    Assuming S's are your selected cells, the "sheet's name" is in N?
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-26-2013
    Location
    Texas
    MS-Off Ver
    2010
    Posts
    5

    Re: How to replace part of a formula with VBA

    Yes...that is correct. The ActiveCell is B2 while B2:E2 are all selected, and the value that needs replacing would be in A1.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: How to replace part of a formula with VBA

    Try:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-26-2013
    Location
    Texas
    MS-Off Ver
    2010
    Posts
    5

    Re: How to replace part of a formula with VBA

    Works like BOSS!

    Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find and Replace part of formula using VBA
    By chococ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-14-2014, 01:26 PM
  2. Replace part of formula VBA
    By Testnntest in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2013, 04:20 AM
  3. find and replace part of formula only
    By iceshimmer in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-06-2013, 06:51 AM
  4. How do I replace part of a formula using VBA?
    By Vaslo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2012, 12:46 PM
  5. Q: DDE Link - Replace Part of the Formula
    By Reto Walker in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-14-2006, 12:15 PM

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