Having problems with Nested If Then Statements

RLJ

Active Member
Joined
Mar 15, 2011
Messages
417
Office Version
  1. 365
Platform
  1. Windows
I need some help with the below code. I have the code working for all the True Statements, but can't quite figure out how to add in the end Statement where if all are not true then the formula at the end of the code needs to be in Range NSOBDate. I don't know if a Select Case would work better for this and if it does, I'm not familiar with Select Case.

VBA Code:
Sub TestMe()
            If Range("NSDate").Value = 30 Then
                Range("NSOBDate").Formula = _
                    "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+1,DAY(Origination_Date))"
             End If
                    
            If Range("NSDate").Value = 60 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+2,DAY(Origination_Date))"
            End If
            
            If Range("NSDate").Value = 90 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+3,DAY(Origination_Date))"
            End If
          
            If Range("NSDate").Value = 120 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+4,DAY(Origination_Date))"
            End If
            
             If Range("NSDate").Value = 150 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+5,DAY(Origination_Date))"
            End If
            
            If Range("NSDate").Value = 180 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+6,DAY(Origination_Date))"
            End If
            
             If Range("NSDate").Value = 270 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+9,DAY(Origination_Date))"
            End If
          
            If Range("NSDate").Value = 360 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date)+1,MONTH(Origination_Date),DAY(Origination_Date))"
            End If

'Need this to be the Formula for NSOBDate if all above are not true

        Range("NSOBDate").Formula = _
            "=DATE(YEAR(Origination_Date),MONTH(Origination_Date),DAY(Origination_Date)+RC[-3])"
End Sub

Thank you for your Help!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
try this or you could use elseif

VBA Code:
Sub TestMe()
    Dim isTrue As Boolean
    
            If Range("NSDate").Value = 30 Then
                Range("NSOBDate").Formula = _
                    "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+1,DAY(Origination_Date))"
                    isTrue = True
             End If
                    
            If Range("NSDate").Value = 60 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+2,DAY(Origination_Date))"
                        isTrue = True
            End If
            
            If Range("NSDate").Value = 90 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+3,DAY(Origination_Date))"
                    isTrue = True
            End If
          
            If Range("NSDate").Value = 120 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+4,DAY(Origination_Date))"
                        isTrue = True
            End If
            
             If Range("NSDate").Value = 150 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+5,DAY(Origination_Date))"
                        isTrue = True
            End If
            
            If Range("NSDate").Value = 180 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+6,DAY(Origination_Date))"
                        isTrue = True
            End If
            
             If Range("NSDate").Value = 270 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+9,DAY(Origination_Date))"
                        isTrue = True
            End If
          
            If Range("NSDate").Value = 360 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date)+1,MONTH(Origination_Date),DAY(Origination_Date))"
                        isTrue = True
            End If

'Need this to be the Formula for NSOBDate if all above are not true
        If Not isTrue Then
            Range("NSOBDate").Formula = _
                "=DATE(YEAR(Origination_Date),MONTH(Origination_Date),DAY(Origination_Date)+RC[-3])"
        End If
End Sub
 
Upvote 0
Solution
try this or you could use elseif

VBA Code:
Sub TestMe()
    Dim isTrue As Boolean
   
            If Range("NSDate").Value = 30 Then
                Range("NSOBDate").Formula = _
                    "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+1,DAY(Origination_Date))"
                    isTrue = True
             End If
                   
            If Range("NSDate").Value = 60 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+2,DAY(Origination_Date))"
                        isTrue = True
            End If
           
            If Range("NSDate").Value = 90 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+3,DAY(Origination_Date))"
                    isTrue = True
            End If
         
            If Range("NSDate").Value = 120 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+4,DAY(Origination_Date))"
                        isTrue = True
            End If
           
             If Range("NSDate").Value = 150 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+5,DAY(Origination_Date))"
                        isTrue = True
            End If
           
            If Range("NSDate").Value = 180 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+6,DAY(Origination_Date))"
                        isTrue = True
            End If
           
             If Range("NSDate").Value = 270 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date),MONTH(Origination_Date)+9,DAY(Origination_Date))"
                        isTrue = True
            End If
         
            If Range("NSDate").Value = 360 Then
                    Range("NSOBDate").Formula = _
                        "=DATE(YEAR(Origination_Date)+1,MONTH(Origination_Date),DAY(Origination_Date))"
                        isTrue = True
            End If

'Need this to be the Formula for NSOBDate if all above are not true
        If Not isTrue Then
            Range("NSOBDate").Formula = _
                "=DATE(YEAR(Origination_Date),MONTH(Origination_Date),DAY(Origination_Date)+RC[-3])"
        End If
End Sub
Thank you. It works
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top