Stop a simple macro

D

Dee

Hi,

Below is a simple macro run to show detail of a pivot table on a required
field.
May I know what shall i do to stop the macro if the pivot table don't contain
the respective field ??

******************************************************
Worksheets("Pivot Table 1")Activate
For i = 26 To 60
If Cells(i, "u") = "Key" Then
Cells(i, "v").Select
Selection.ShowDetail = True
Exit For
End If
Next i

Thanks in advance !!
 
G

Gary''s Student

Use a flag to determine if the match occurred and take action:

Sub servient()
Worksheets("Pivot Table 1")Activate
got_it = False
For i = 26 To 60
If Cells(i, "u") = "Key" Then
got_it = True
Cells(i, "v").Select
Selection.ShowDetail = True
Exit For
End If
Next i
If got_it = False Then
Exit Sub
End If
End Sub
 
Top