View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Jon Peltier Jon Peltier is offline
external usenet poster
 
Posts: 6,582
Default Can't get VBA to work without select

For completeness, you should also explicitly reference r.Cells:

For Each i In r.Cells

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Gary''s Student" wrote in message
...
Sub BoldCells()
Dim r As Range
Dim i As Range

Dim w As Worksheet
Set w = Worksheets(1)

Set r = w.Range("A1", w.Range("A65536").End(xlUp))
For Each i In r
i.Font.Bold = Not i.Font.Bold
Next i
End Sub

Note that in the
set r =
statement we need to define to which worksheet each Range() function
refers.
--
Gary''s Student - gsnu2007d


"D." wrote:

I have this code that works fine only if I am on worksheet(1),
If I am on another sheet it does not work I get runtime error 1004
Here it is

#Sub BoldCells()
Dim r As Range
Dim i As Range
Set r = Worksheets(1).Range("A1", Range("A65536").End(xlUp))
For Each i In r
i.Font.Bold = Not i.Font.Bold
Next i
End Sub#

What am I missing, so the code will work without having to select the
sheet?

Thanks