+ Reply to Thread
Results 1 to 6 of 6

Using a VBA variable in a cell formula?

  1. #1
    Registered User
    Join Date
    05-11-2015
    Location
    West Cornwall, UK
    MS-Off Ver
    2010
    Posts
    55

    Using a VBA variable in a cell formula?

    Hi Guys,

    Not sure if this should be in the VBA or Formulas section.

    I have the formula in cell B9 =sheetoffset(-1,B9)-sheetoffset(-1,B10)+B8 which "brings forward" figures from the previous sheet.

    Please Login or Register  to view this content.
    If I have an alternate figure in a variable, I would like to use that instead of the above, BUT retain the above when going to the next sheet.
    So, can I have something like =If(MyVariable<>,MyVariable, sheetoffset(-1,B9)-sheetoffset(-1,B10)+B8)

    Thanks for looking.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Using a VBA variable in a cell formula?

    You should be able to call your UDF from within an IF() worksheet function just like you describe https://support.office.com/en-us/art...c-aa8bbff73be2
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    05-11-2015
    Location
    West Cornwall, UK
    MS-Off Ver
    2010
    Posts
    55

    Re: Using a VBA variable in a cell formula?

    Thank you for your reply.

    The variable that has been generated comes from choices made using option buttons on a userform. Can you suggest a way that I can get from the userform to the cell?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Using a VBA variable in a cell formula?

    I never use userforms, so I cannot suggest a way. My first thought would be to have the code behind the userform write the desired option(s) into a cell or cells in the spreadsheet (using the .value property of the desire range object), which your IF() worksheet function can then refer to, but I will defer to others with more experience working with userforms.

    In the meantime, are there other aspects of this project that you need to explain? What calls the userform? What kind of values are in the userform? How do you determine which values in the userform are impacting this decision/calculation? or anything else that might be pertinent. If you can upload a sample workbook, that will likely help whoever comes along to understand exactly what you are trying to do.
    Last edited by MrShorty; 07-07-2016 at 02:02 PM.

  5. #5
    Registered User
    Join Date
    05-11-2015
    Location
    West Cornwall, UK
    MS-Off Ver
    2010
    Posts
    55

    Re: Using a VBA variable in a cell formula?

    Hi MrShorty,

    I can't attach the file using the paperclip on this page, so below is a link that should give anyone the sample file.

    On sheet2 there is a command button "Move Crewman" this loads the userform.


    https://drive.google.com/open?id=0B8...EV2M0dwVjhUbTQ

    I said in my initial post that the formula is in cell B9, this was just an example,

    I need this to work for 8 cells, B9, F9, B18, F18, B27, F27, B36 & F36.

    Thanks

  6. #6
    Registered User
    Join Date
    05-11-2015
    Location
    West Cornwall, UK
    MS-Off Ver
    2010
    Posts
    55

    Re: Using a VBA variable in a cell formula?

    Although not "exactly" what I was hoping for, MrShorty has offered a workable sustitute, so I will mark this 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. How to map a formula to a variable cell?
    By cgnkc in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-21-2014, 09:37 PM
  2. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  3. [SOLVED] How to set formula with cell reference with a variable
    By momotaro in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-13-2013, 04:19 AM
  4. Replies: 4
    Last Post: 11-05-2012, 07:56 PM
  5. how to refer to cell with variable in a formula?
    By seb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2006, 11:15 AM
  6. Adding 3 variable formula to a cell
    By kssflmail in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-20-2006, 01:15 PM
  7. Refrencing a variable cell in a formula
    By C Brehm in forum Excel General
    Replies: 2
    Last Post: 10-10-2005, 11:05 AM
  8. Insert variable cell in the formula
    By Gian in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-07-2005, 04:06 PM

Tags for this Thread

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