+ Reply to Thread
Results 1 to 3 of 3

Using VBA to add a formula to a range with a variable as part of the formula

  1. #1
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    540

    Using VBA to add a formula to a range with a variable as part of the formula

    I'm trying to create a cell formula using VBA with a variable but am running into errors. The objective of the formula is to identify the sheet name of the last created worksheet. Here's a working example of the formula:

    Please Login or Register  to view this content.
    This would identify that the last sheet created is called 'Sheet2'- if you enter it in a cell, the output is 'Sheet2'
    The problem is that I obviously need to use a variable in place of where the above formula contains references to Sheet2!A1. I've tried a few variations to no avail, the most recent being the following:

    Please Login or Register  to view this content.
    ```
    I suspect that it might be something to do with all the inverted commas but am stumped! A couple of Google searches didn't quite fix it for me and a quick search here didn't seem to point the way out. Anyone see a fix? Many thanks to all who respond.
    Last edited by deadlyduck; 05-11-2021 at 06:02 AM.
    Excel 365 user. To unblock a downloaded macro-enabled workbook, go to your "Downloads" folder > right click on the workbook name > click 'Properties' > check the 'Unblock' checkbox. You can now open the workbook.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Using VBA to add a formula to a range with a variable as part of the formula

    Try

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    540

    Re: Using VBA to add a formula to a range with a variable as part of the formula

    Thanks for this Richard- it's a neater solution than the one I came up with soon after I had requested help!

    My solution was to create a string variable [MyString = "'" & ws.Name & "'!" & tgtCell.Address] and use this string variable in place of where your single step solution has tgtcell.value &"!A1

    Am marking the post as SOLVED

+ 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. [SOLVED] COUNTIF formula where part of the range reference needs to use the result of a formula
    By zookeepertx in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 11-09-2018, 05:39 PM
  2. Can I make the row lookup array/range part in an array formula variable?
    By OLLY-7 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-18-2014, 09:06 AM
  3. [SOLVED] Set part of formula as variable
    By Brukernavn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2012, 04:45 AM
  4. Capture variable from part of cell formula
    By tbyrd1084 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2011, 05:27 PM
  5. Using a variable to find part of a formula
    By rebell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2011, 09:22 PM
  6. Using Variable in Cell Formula as part of For Next
    By grey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-13-2009, 11:37 AM
  7. Replies: 2
    Last Post: 07-16-2009, 12:30 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