PC Review


Reply
Thread Tools Rate Thread

Creating a range that indexes in a For loop

 
 
Patel
Guest
Posts: n/a
 
      8th Aug 2008

For i = 2 To 20
If Cells(i, 1) = Cells(i + 1, 1) Then

Else
i = i + 1
Rows(i).Insert
' Here's where I would like to select the range for row i, column 1
through row i, column 12 and then format it with colors, borders, etc.
After that I would continue my loop. How do I write my range to be
able to index the rows since i changes each time that that the "if"
statement isn't true?

End If
Next i


Any help would be appreciated! Thank you.
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      8th Aug 2008
I'm not sure exactly where you wanted to use the code but here is one way to
do it.

For i = 2 To 20
If Cells(i, 1) = Cells(i + 1, 1) Then
For j = 1 To 12
Cells(i, j).Interior.ColorIndex = 3 + x
x = x + 1
Next
Else
i = i + 1
Rows(i).Insert

If the first statement is true, then the first 12 cells on that row would be
colored with colors 3 through 15 consecutively. You could set up an
algorithm instead of the for next loop to set a varying color pattern.

"Patel" wrote:

>
> For i = 2 To 20
> If Cells(i, 1) = Cells(i + 1, 1) Then
>
> Else
> i = i + 1
> Rows(i).Insert
> ' Here's where I would like to select the range for row i, column 1
> through row i, column 12 and then format it with colors, borders, etc.
> After that I would continue my loop. How do I write my range to be
> able to index the rows since i changes each time that that the "if"
> statement isn't true?
>
> End If
> Next i
>
>
> Any help would be appreciated! Thank you.
>

 
Reply With Quote
 
Patel
Guest
Posts: n/a
 
      11th Aug 2008
On Aug 8, 10:15*am, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> I'm not sure exactly where you wanted to use the code but here is one wayto
> do it.
>
> *For i = 2 To 20
> * * * * *If Cells(i, 1) = Cells(i + 1, 1) Then
> * * * * * * * *For j = 1 To 12
> * * * * * * * * * * Cells(i, j).Interior.ColorIndex = 3 + x
> * * * * * * * * * * * *x = x + 1
> * * * * * * * *Next
> * * * * *Else
> * * * * * * *i = i + 1
> * * * * * * *Rows(i).Insert
>
> If the first statement is true, then the first 12 cells on that row wouldbe
> colored with colors 3 through 15 consecutively. *You could set up an
> algorithm instead of the for next loop to set a varying color pattern.
>
>
>
> "Patel" wrote:
>
> > * * For i = 2 To 20
> > * * * * If Cells(i, 1) = Cells(i + 1, 1) Then

>
> > * * * * Else
> > * * * * * * i = i + 1
> > * * * * * * Rows(i).Insert
> > ' Here's where I would like to select the range for row i, column 1
> > through row i, column 12 and then format it with colors, borders, etc.
> > After that I would continue my loop. How do I write my range to be
> > able to index the rows since i changes each time that that the "if"
> > statement isn't true?

>
> > * * * * End If
> > * * Next i

>
> > Any help would be appreciated! Thank you.- Hide quoted text -

>
> - Show quoted text -


Thank you for the idea. I was trying to get away from using another
loop and just having a dynamic range that indexed in my initial loop.
With the help of a friend, here's what we came up with:


With Range(Cells(i, 1), Cells(i, 3)) 'This selects all cells
between i,1 and i,3 with i being an integer in my loop
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
.BorderAround Weight:=xlThick
End With

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting row indexes on Range Selsted Microsoft Excel Programming 4 10th Feb 2010 12:48 PM
creating indexes diane in seattle Microsoft Word Document Management 2 24th Mar 2009 03:12 AM
Creating indexes for optimized performance =?Utf-8?B?cmlja293ZW4=?= Microsoft Access Database Table Design 2 16th Sep 2005 12:12 PM
plz help: creating named range in VBA, loop goes haywire KR Microsoft Excel Programming 2 14th Apr 2005 07:25 PM
Too many indexes when creating relationship =?Utf-8?B?QnJpYW4=?= Microsoft Access VBA Modules 7 23rd Jul 2004 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:29 AM.