+ Reply to Thread
Results 1 to 10 of 10

VBA Nesting If Statements for Dates

  1. #1
    Registered User
    Join Date
    01-11-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2007
    Posts
    38

    VBA Nesting If Statements for Dates

    Hello,

    I am having issues writing the VBA code for IF statements. I am essentially trying to nest IF statements within one another. I have it done through the regular If functions as seen in the attachment, but I cannot seem to get it to work with VBA. I need it to work in VBA without knowing how many rows of data there are. In the attachment you will see the rules that I need the data follow, but here is an example. Essentially, depending upon how large cell C2 is, I need to add a certain amount of months to whatever is in E2.

    =IF(C2="","",IF(C2<500000,DATE(YEAR(E2),MONTH(E2)+1,DAY(E2)),IF(AND(C2>=500000,C2<1000000),DATE(YEAR(E2),MONTH(E2)+1,DAY(E2)),IF(AND(C2>=1000000,C2<=2000000),DATE(YEAR(E2),MONTH(E2)+1,DAY(E2)),IF(AND(C2>2000000,D2="Critical"),DATE(YEAR(E2),MONTH(E2)+2,DAY(E2)),IF(C2>2000000,DATE(YEAR(E2),MONTH(E2)+1,DAY(E2)),"Reevaluate"))))))

    Thanks for your help.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: VBA Nesting If Statements for Dates

    It's really hard to work out from this mega formula what you are trying to do here. I suspect that this is not the best way to do what ever it is. Can you explain in natural language what you are trying to achieve.

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: VBA Nesting If Statements for Dates

    The logic of your formula makes no sense. In each "IF" test, you are stating you want to increase the date in C2 by one month.

    Below is the breakdown of your tests using psudeo-VBA code structure (NOT valid code). All of this is broken out of the function and, as you can see, what this boils down to is:

    If D2 = "Critical" and C2 value is greather than 2000000,then add two months to the date in E2, otherwise, if C2 is not blank, then add one month to the date and if C2 is empty, then return a blank.

    So, adjust this as needed to return "Reevaluate". I got a bit of eye strain just looking at this formula.

    =IF(C2="","",IF(AND(D2="Critical",C2>2000000),DATE(YEAR(E2),MONTH(E2)+2,DAY(E2)),IF(C2<>"",DATE(YEAR(E2),MONTH(E2)+1,DAY(E2)))))

    Please Login or Register  to view this content.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  4. #4
    Registered User
    Join Date
    01-11-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: VBA Nesting If Statements for Dates

    So I apologize if the previous post was confusing, so here is another go. Depending upon the amount of money, I need to add a certain amount of months to the current date. Here is an example of what I need the code to do and then I can adapt it.

    If the cost is over 20 (Cell C2) and critical (Cell D2)l, I need to add 2 months to the date in Cell E2.
    If the cost is over 20 (Cell C2) and regular (Cell D2), I need to add 1 month to the date in Cell E2
    If the Cost is between 10 and 20 (Cell C2) and regular (Cell D2), I need to add 1 month to the date in Cell E2.
    If the the Cost is between 5 and 10 (Cell C2) and regular (Cell D2), I need to add 1 month to the date in Cell E2.
    If the Cost is less than 5 (Cell C2) and regular (Cell D2), I need to add 1 month to the date in Cell E2.

    Even though in this instance there is only one case where you will add 2 months, I need to have each IF Statement written so that I can change the code for other instances. Thanks!

  5. #5
    Registered User
    Join Date
    01-11-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: VBA Nesting If Statements for Dates

    Also, I am trying to do this in a loop because I do not know what the last row will be. In writing the code below, the debugger keeps stopping on "year". Any idea why?

    Sub a()
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    For I = 2 To FinalRow
    If Cells(I, 3).Value < 500000 Then
    Cells (I, 6).Value = Date(Year(I, 5),Month(I, 5)+1,Day(I, 5))

    End Sub

  6. #6
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: VBA Nesting If Statements for Dates

    Hi
    Fisrt of all, I think you want to use DateSerial function, not Date.
    Second, unless Year, Month and Day are arrays (but I'm sure they are not), your statement doesn't make sence to me.
    Buran
    If you are pleased with a member's answer then use the Star icon to rate it.

  7. #7
    Registered User
    Join Date
    01-11-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: VBA Nesting If Statements for Dates

    The Date(Year,Month,Day) works if you just use it in the formula bar. I am unsure of how to use the dateserial in vba to add a certain amount of months to a date that is currently in a cell. If you know how to do so, I will gladly try it. Thank you.

  8. #8
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: VBA Nesting If Statements for Dates

    The same function in VBA is DateSerial(Year, Month, Day)
    I assume in the fifth column (i.e. column E) you have dates and want to increase the month with 1.
    The statement should be

    Please Login or Register  to view this content.
    Here you take the year from the date in cell (i,5), Month from the date in cell (i,5) and increase it with 1, and you take the day again from the date in cell (i,5)

    The same code could be written as

    Please Login or Register  to view this content.
    Last edited by buran; 01-28-2010 at 03:22 PM.

  9. #9
    Registered User
    Join Date
    01-11-2010
    Location
    North Carolina
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: VBA Nesting If Statements for Dates

    That is exactly what I needed. I had one parenthesis in the wrong place. Thank you!

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: VBA Nesting If Statements for Dates

    Hi,

    Quote Originally Posted by Topdrop18 View Post
    Also, I am trying to do this in a loop because I do not know what the last row will be. ...
    This is an oldish thread, but it's not yet marked as solved, although perhaps it should be ;-) so I thought I'd show that looping through each cell one at a time is not needed.

    This code snippet shows how the formula can be written to the range in one go (after identifying the last row).
    Please Login or Register  to view this content.
    This macro shows arrays can be used (after identifying the last row) to process "in memory" & then write back to the sheet in one go.
    Note that I may have not applied the right logic in the Select Case statements (copied & changed (?) from Palmetto's post).
    Please Login or Register  to view this content.

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

+ 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