Combine multiple VBA IF formulas in worksheet

Empirer

New Member
Joined
Oct 15, 2020
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Hi Excel wizards,

I'm trying to run this macro in the worksheet but only the first two if formulas are working. The last one if the condition is "Ceased" does not seem to work. Kindly advice on how to fix the VBA code.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("H:H")) Is Nothing Then
      If Target <> "" Then
         Target.Offset(0, -6) = Now
      Else
         Target.Offset(0, -6) = ""
      End If
   ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
      If Target = "Completed" Then
         Target.Offset(0, -1) = Now
      ElseIf Target = "" Then
         Target.Offset(0, -1) = ""
      End If
    ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
      If Target = "Ceased" Then
         Target.Offset(0, 19) = Now
      ElseIf Target = "" Then
         Target.Offset(0, 19) = ""
      End If
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The last one if the condition is "Ceased" does not seem to work
Is this supposed to be on column J as well? If yes, it wouldn't work because you are using ElseIf, and the previous condition is already column J.

Also, it seems you are missing an "End If"
 
Upvote 0
Is this supposed to be on column J as well? If yes, it wouldn't work because you are using ElseIf, and the previous condition is already column J.

Also, it seems you are missing an "End If"
Yes this is based on Column J's value as well. I want it to populate a date in another cell if column J is "Ceased". How do I add another criteria while keeping the old ones?
 
Upvote 0
If the Target is in column J and the Target = "", you have two different actions :
Target.Offset(0, -1) = ""
Target.Offset(0, 19) = ""

Which one do you want?
 
Upvote 0
Maybe this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target <> "" Then
        Target.Offset(0, -6) = Now
    Else
        Target.Offset(0, -6) = ""
    End If
ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
    If Target = "Completed" Then
        Target.Offset(0, -1) = Now
    If Target = "Ceased" Then
        Target.Offset(0, 19) = Now
    ElseIf Target = "" Then
        Target.Offset(0, -1) = ""
        Target.Offset(0, 19) = ""
    End If
End If
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Corrected
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target <> "" Then
        Target.Offset(0, -6) = Now
    Else
        Target.Offset(0, -6) = ""
    End If
ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
    If Target = "Completed" Then
        Target.Offset(0, -1) = Now
    ElseIf Target = "Ceased" Then
        Target.Offset(0, 19) = Now
    ElseIf Target = "" Then
        Target.Offset(0, -1) = ""
        Target.Offset(0, 19) = ""
    End If
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Corrected
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target <> "" Then
        Target.Offset(0, -6) = Now
    Else
        Target.Offset(0, -6) = ""
    End If
ElseIf Not Intersect(Target, Range("J:J")) Is Nothing Then
    If Target = "Completed" Then
        Target.Offset(0, -1) = Now
    ElseIf Target = "Ceased" Then
        Target.Offset(0, 19) = Now
    ElseIf Target = "" Then
        Target.Offset(0, -1) = ""
        Target.Offset(0, 19) = ""
    End If
End If
Application.EnableEvents = True
End Sub
thank you so much, this one works!
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,798
Members
449,337
Latest member
BBV123

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