Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Need help with Range object in Macro programming

I have a macro that puts data into a sheet and sets the font to bold and
color to yellow. It looks like this:

Cells(x, 7).Select
Selection.Font.Bold = True
Selection.Interior.ColorIndex = 6

I was reading some websites that say this is not efficient, that I should
use with:
example:

With Range("A1")
.Font.Bold = True
.Interior.ColorIndex = 6
End With

Since my code is in a loop I use Cells(x,7). How do I format the Range
object to use a variable instead of "A1"? I tried Range(Cells(x,7)) but got
an error.

Dan


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Need help with Range object in Macro programming

Hi
For x = 1 to 10
With Cells(x,7)
.Font.bold = True
.Interior.Colorindex = 6
end with
next x

This refers to the Cells(1,7) to Cells(10,7) on the whole sheet. If you
want to loop through ten cells somewhere else e.g B3 to B13 use
With Range("B3:B13")
For x = 1 to 10
With .Cells(x,1)
.Font.bold = True
.Interior.Colorindex = 6
End With 'Cells
next x
End With 'Range

regards
Paul


Dan wrote:
I have a macro that puts data into a sheet and sets the font to bold and
color to yellow. It looks like this:

Cells(x, 7).Select
Selection.Font.Bold = True
Selection.Interior.ColorIndex = 6

I was reading some websites that say this is not efficient, that I should
use with:
example:

With Range("A1")
.Font.Bold = True
.Interior.ColorIndex = 6
End With

Since my code is in a loop I use Cells(x,7). How do I format the Range
object to use a variable instead of "A1"? I tried Range(Cells(x,7)) but got
an error.

Dan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Need help with Range object in Macro programming

Sub gsnu()
For x = 2 To 7
With Cells(x, 7)
.Interior.ColorIndex = 6
End With
Next
End Sub

You don't need to use the range function.
--
Gary's Student


"Dan" wrote:

I have a macro that puts data into a sheet and sets the font to bold and
color to yellow. It looks like this:

Cells(x, 7).Select
Selection.Font.Bold = True
Selection.Interior.ColorIndex = 6

I was reading some websites that say this is not efficient, that I should
use with:
example:

With Range("A1")
.Font.Bold = True
.Interior.ColorIndex = 6
End With

Since my code is in a loop I use Cells(x,7). How do I format the Range
object to use a variable instead of "A1"? I tried Range(Cells(x,7)) but got
an error.

Dan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 477
Default Need help with Range object in Macro programming

why not:
With Cells(x, 7)
.Font.Bold = True
.Interior.ColorIndex = 6
End With


"Dan" wrote:

I have a macro that puts data into a sheet and sets the font to bold and
color to yellow. It looks like this:

Cells(x, 7).Select
Selection.Font.Bold = True
Selection.Interior.ColorIndex = 6

I was reading some websites that say this is not efficient, that I should
use with:
example:

With Range("A1")
.Font.Bold = True
.Interior.ColorIndex = 6
End With

Since my code is in a loop I use Cells(x,7). How do I format the Range
object to use a variable instead of "A1"? I tried Range(Cells(x,7)) but got
an error.

Dan



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 272
Default Need help with Range object in Macro programming

Dan, there is nothing magical about using Range vs using Cells, the
difference is whether or not you select the cell, so as Gary, Paul, and Jim
have shown you don't need to use the Range object, if you would like to use
Range it can be done this way:
With Range("G" & x)
.Font.Bold = True
.Interior.ColorIndex = 6
End With
--
Charles Chickering

"A good example is twice the value of good advice."


"Dan" wrote:

I have a macro that puts data into a sheet and sets the font to bold and
color to yellow. It looks like this:

Cells(x, 7).Select
Selection.Font.Bold = True
Selection.Interior.ColorIndex = 6

I was reading some websites that say this is not efficient, that I should
use with:
example:

With Range("A1")
.Font.Bold = True
.Interior.ColorIndex = 6
End With

Since my code is in a loop I use Cells(x,7). How do I format the Range
object to use a variable instead of "A1"? I tried Range(Cells(x,7)) but got
an error.

Dan



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro error, range object need data? Daniel Charts and Charting in Excel 1 June 15th 07 11:27 AM
Programming code to new object Martin Excel Programming 12 August 9th 05 10:45 AM
returning pivottable object from a range object Grant Excel Programming 2 September 27th 04 02:22 AM
Range object to Array object conversion Myrna Larson[_2_] Excel Programming 1 August 1st 03 02:27 AM
Range object to Array object conversion Alan Beban[_3_] Excel Programming 0 August 1st 03 01:24 AM


All times are GMT +1. The time now is 03:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"