macro to exit sub if A2 is blank

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,691
Office Version
  1. 2019
Platform
  1. Windows
I have a pivot table on sheet3

I have a macro which refreshes the Pivot table

where A2 on sheet2 is blank, then macro to Exit , otherwise to refresh Pivot


Code:
 Sub Update_Pivot()
With Sheets(2)
If .Range("A2") = "" Then
Exit Sub
End If

Else
End With

Sheets(3).Select

    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh


End Sub


however, when running the macro, I get a message Compile Error: Else without if


I don't know how to resolve this


It would be appreciated if someone could kindly assist me
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,049
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
This should work (untested)
VBA Code:
Sub Update_Pivot()

If Sheets(2).Range("A2") = "" Then
Exit Sub
Else

Sheets(3).PivotTables("PivotTable1").PivotCache.Refresh
End If

End Sub
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,049
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
You're welcome and thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,119,091
Messages
5,576,049
Members
412,697
Latest member
khanhvy31
Top