macro to exit sub if A2 is blank

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,686
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,047
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,047
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
You're welcome and thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,296
Messages
5,571,396
Members
412,386
Latest member
Yasaman
Top