+ Reply to Thread
Results 1 to 8 of 8

.formula property referencing a defined string constant

  1. #1
    Registered User
    Join Date
    03-30-2012
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    9

    .formula property referencing a defined string constant

    I am running into a problem when I try to call out a string that I previously defined in the .formula property. Below is just part of the code. I define Replen to be one of 12 numbers.
    The line in the code: .Formula = "=IF(And(A2=Replen), B2<(today()+7)), C2+C3, C3)" is what is giving me problems. I am able to get the code to work if I use an an OR statement embedded in the code listing all 12 items. The reason I dont' want to do that is because this list needs to be updated from time to time and I would have to update it in multiple places in the rest of the code.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: .formula property referencing a defined string constant

    you are missing a bracket in your formula, give this a shot.

    =IF(And((A2=Replen), B2<(today()+7)), C2+C3, C3)
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Registered User
    Join Date
    03-30-2012
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: .formula property referencing a defined string constant

    DGagnon,

    Thanks, I was looking at that took long yesterday. That solves why it didn't compile. It now compiles and runs, however, but there is still a a problem:

    When it copies
    =IF(And((A2=Replen), B2<(today()+7)), C2+C3, C3)
    into the cells in the workbook, and I look at it by stepping through the code, the results do not properly add up. Instead, it gives #Name? because I do not think the work books recognizes what Replen is that I defined in the code.

  4. #4
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: .formula property referencing a defined string constant

    it looks like you are trying to declare Replen as an array.. but i dont think you can do that in a string the way you are doing there. also to add it into the formula you will need to do this:

    Please Login or Register  to view this content.
    could you clarify what you are trying to do with Replen though?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: .formula property referencing a defined string constant

    REPLEN is a function you're trying to create? You can actually create a new function called REPLEN if you'd like. Put this in a separate module (Insert > Module):

    Please Login or Register  to view this content.

    Now that can be used in a cell as a formula like so:

    =REPLEN(A2)

    ...and it will return a TRUE/FALSE answer, so it's perfect for use in an IF/THEN formula.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 04-20-2012 at 12:22 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Registered User
    Join Date
    03-30-2012
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: .formula property referencing a defined string constant

    Jerry,

    Thanks a lot, that works as intended now.

    There is just one little issue I didn't forsee because I was treating them as strings. Often these replen items undergo revisions. For example, 981541 could become 981541R1 and then two weeks later become 981541R2. When I was literally putting all replen item numbers (treating them as strings) in the .formula statement (the only way I originally got it to work), by putting in the substring 981541, it would work on 981541R1 and 981541R2, etc. Is there any way to add this ability to the function without being two complicated. Otherwise I'd have to be updating the function when something had a revision. The other solution would be to update all my .formula statements when there was a new replen item all together. Either way, you've been a huge help and clearly I need to expand my use of functions and not just sub routines. Thanks again.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: .formula property referencing a defined string constant

    You can use the REPLEN function to just test the first 6 characters:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-30-2012
    Location
    Grand Rapids, MI
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: .formula property referencing a defined string constant

    Jerry and DGagnon,

    Thanks for your help and insight. The code does exactly what I need it to do.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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