Run a macro if cell is not empty

DJ333 Posted messages 5 Status Member -  
DJ333 Posted messages 5 Status Member -
Bonjour
I am new to macros... I want that when clicking on a button, the macros only execute if a specified cell is filled. However, my code does not work, it responds "missing an if condition...
How do I do it? Thank you in advance!

Sub crefeuille1()
' crefeuille1 Macro
If Range("C2") = "" Then
Sheets("Revue type").Select
Sheets("Revue type").Copy After:=Sheets(2)
Set MySheet = ActiveSheet
With MySheet
.Name = Worksheets("Trame").Range("e2")
End If
End With
End Sub
Sub crefeuille2()
'
' crefeuille2 Macro
'
If Range("C3") = "" Then
Sheets("Revue type").Select
Sheets("Revue type").Copy After:=Sheets(2)
Set MySheet = ActiveSheet
With MySheet
.Name = Worksheets("Trame").Range("e3")
End If
End With
End Sub

3 answers

  1. via55 Posted messages 14393 Registration date   Status Member Last intervention   2 759
     
    Good evening

    1° your macro only executes if cell C2 is empty
    if you want it to execute only when C2 is filled, you should write If Range("C2") <>"" Then

    2° The End if should be placed after the End with

    3° With is only useful when there are multiple statements to apply to the same range to avoid repetition; here you only have one statement, so you can simplify by replacing
    With MySheet
    .Name = Worksheets("Trame").Range("e2")
    End with
    with
    ActiveSheet.Name = Worksheets("Trame").Range("e2")

    Best regards
    Via

    --
    "Imagination is more important than knowledge." A. Einstein
    0