View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Gary Brown[_5_] Gary Brown[_5_] is offline
external usenet poster
 
Posts: 236
Default macro skips a command

I think it was a matter of 'scope'.
I can't tell from the code you've given, but I'll bet the active worksheet
that you call the macro from is NOT the 'Summaries' worksheet. Since the
range name 'widthselection' is associated with the 'Summaries' worksheet, the
macro couldn't find the 'widthselection' range until we added the
'Worksheets("Summaries").' qualified in front of the
'Range("widthselection").ColumnWidth' statement.

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



"cm" wrote:

Perfect; I am new to VBA -- why did the first way get ignored?
--

cm


"Gary Brown" wrote:

Try...
Worksheets("Summaries").Range("widthselection").Co lumnWidth = 18

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



"cm" wrote:

I have a button that runs the following codle. All of it works as intended
excpet the two lines that attempt to set the column widths of range named
'widthselection' to 18, after the pivot table has been refreshed.

Would like some advice, please.

Application.DisplayAlerts = False
With Sheets("Summaries")
.Unprotect Password:="topSecret"
Sheets("Summaries").PivotTables("PivotTable2").Piv otCache.Refresh
Dim pt As PivotTable
Dim ws As Worksheet
Dim pc As PivotCache
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Next pt
Next ws
For Each pc In ActiveWorkbook.PivotCaches
On Error Resume Next
pc.Refresh
Next pc
Range("widthselection").Select
Selection.ColumnWidth = 18
.Protect Password:="topSecret"
End With
--

cm