Run VBA code under certain conditions

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello, I have this code to show a spinning message when I enter a value in column A and column B is empty. Can I modify the code to make it appear only when I type only the word ttc or ttc10
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row > 1 Then
    If Target.Offset(0, 2).Value = "" Then
        MsgBox "Attestation"
        Target.Offset(0, 2).Select
    End If
End If
End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    15.8 KB · Views: 2

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try changing this line:
VBA Code:
    If Target.Offset(0, 2).Value = "" Then
to this:
VBA Code:
    If (Target.Offset(0, 2).Value = "") And ((Target.Value="ttc") Or (Target.Value = "ttc10")) Then

Note however that "Target.Offset(0, 2)" is actually checing column C, not column B.
You mentioned checking column B in your explanation to see if it is blank, but your code is actually looking at column C.
So I am not sure which of these two columns you really want to check.

Since the Target is in column A, column B would look like: "Target.Offset(0, 1)".
 
Upvote 0
Solution
Try changing this line:
VBA Code:
    If Target.Offset(0, 2).Value = "" Then
to this:
VBA Code:
    If (Target.Offset(0, 2).Value = "") And ((Target.Value="ttc") Or (Target.Value = "ttc10")) Then

Note however that "Target.Offset(0, 2)" is actually checing column C, not column B.
You mentioned checking column B in your explanation to see if it is blank, but your code is actually looking at column C.
So I am not sure which of these two columns you really want to check.

Since the Target is in column A, column B would look like: "Target.Offset(0, 1)".
Thank you very much my friend, this is what is needed
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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