+ Reply to Thread
Results 1 to 4 of 4

How do you find the difference between two cells in a macro?

  1. #1
    Registered User
    Join Date
    05-31-2012
    Location
    Portland
    MS-Off Ver
    Excel 2010
    Posts
    6

    How do you find the difference between two cells in a macro?

    Help please!

    This is a tiny chunk of a couple pages of code... I didn't write it, I'm just trying to edit it according to my boss' specifications so it will run faster. Anyways, so I've defined First Year Premium and First Year Premium Prior below, and the macro works up to that point. I want to define cell E3 (k=3 at this point) as the difference between FYPrem (current YTD) and FYPremP (Previous YTD) so I can find the current quarter to date data. Currently, the data that is being pulled for FYPrem is negative and FYPremP is positive...

    I'm getting run-time error 1004: application-defined or object-defined error. How do I fix this??

    FYPrem = "" & Worksheets(TabNames(3)).Name & "!" & Cells(13 + i, 9).Address
    FYPremP = "" & Worksheets(TabNames(8)).Name & "!" & Cells(k, 5).Address
    Worksheets(TabNames(5)).Range("E" & k) = "=-" & (FYPrem + FYPremP)

    I'm really new to VBA... So if you could explain your answer really well, that would be amazing. I don't really want to just copy/paste whatever solution you find without understanding it.

    THANK YOU.

    ***Edit: I can give more code as a context if that helps, I just need to edit some of it because some of the names in the code are confidential...
    Last edited by jr13; 05-31-2012 at 03:02 PM.

  2. #2
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: How do you find the difference between two cells in a macro?

    Quote Originally Posted by jr13 View Post
    Help please!

    Currently, the data that is being pulled for FYPrem is negative and FYPremP is positive...


    FYPrem = "" & Worksheets(TabNames(3)).Name & "!" & Cells(13 + i, 9).Address
    FYPremP = "" & Worksheets(TabNames(8)).Name & "!" & Cells(k, 5).Address
    Worksheets(TabNames(5)).Range("E" & k) = "=-" & (FYPrem + FYPremP)

    The "data being pulled" isn't numeric it's an address

    I can't tell you whether or not your formula will work but I can tell you it won't work unless you use the Value

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How do you find the difference between two cells in a macro?

    jr13,

    You're treating the code as if you're still using the spreadsheet. Creating a cell reference in the manner of Sheet1!A1 doesn't work in VBA, it just creates a string of "Sheet1!A1".

    Give this a try instead:
    Please Login or Register  to view this content.


    Or, if you want Sheet5!E(k#) to be a formula instead of a value:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Registered User
    Join Date
    05-31-2012
    Location
    Portland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How do you find the difference between two cells in a macro?

    Thank you so much! That solved the problem! Sadly, I was wrong on what FYPremP was (It may or may not exist...). I've never used VBA before today so I was self teaching myself off this one macro by walking through each step. haha. In case you're curious, this is what I ended up using!

    Please Login or Register  to view this content.
    Last edited by jr13; 05-31-2012 at 07:27 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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