• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

run macro if cell text is "uk"

sprorigh

New Member
hi all,

I know almost nothing about VBA, but I'm trying to run a macro (which I registered and works fine) every time the text in a specific cell changes. ultimately, I'd like to have something like this:
if "a1" text is "uk" then run macro1
if "a1" text is "de" then run macro2
etc.

I somehow managed to make it happen whenever I change the cell content, but I'm not able to make it run by a specific text. see below the code I currently have:


Sub uk()

Columns("A:b").Select
Selection.sort Key1:=Range("b2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("q1")) Is Nothing Then
If Target.Value = "uk" Then
Application.EnableEvents = False
Application.ScreenUpdating = False

Call uk
Application.EnableEvents = True
Application.ScreenUpdating = True

Else
End If

End Sub

I keep getting an error message which is: compile error, block if without end if.

can anyone please help me!?

thanks!
 
Hi,

You are putting an else so you must specify some command like
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("q1")) Is Nothing Then
If Target.Value = "uk" Then
Application.EnableEvents = False
Application.ScreenUpdating = False

Call uk
Application.EnableEvents = True
Application.ScreenUpdating = True

Else
if target.value = "de" then

  your code here

End If
End If

End Sub



or remove "else" and run the code.

Regards,
 
Hi,

You are putting an else so you must specify some command like
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("q1")) Is Nothing Then
If Target.Value = "uk" Then
Application.EnableEvents = False
Application.ScreenUpdating = False

Call uk
Application.EnableEvents = True
Application.ScreenUpdating = True

Else
if target.value = "de" then

  your code here

End If
End If

End Sub



or remove "else" and run the code.

Regards,

thank you so so much. it works perfectly now!
 
Back
Top