Hello,
Example one worked.
> you could declare an array variable in your code like this:
>
[quoted text clipped - 17 lines]
> Declarations. Look up "Using Arrays" in VBA Help for more info. I don't
> recall if sheets are 0 based or 1 based. Expierment with it and see.
Example two did not work and this is my macro: How can it be modified
to work?
Sub WorkSheetsTabColor3()
'Worksheet Tab Color Assigned in Array
'Define Variables
Dim i As Integer
'Explicitly declare a 1-based 30 element integer array variable
Dim pintColorIndex(1 To 30) As Integer
'Sreen does not flash (Turn Screen Updating "OFF")
Application.ScreenUpdating = False
'Assignning array with Color Index Values
pintColorIndex = Worksheets("config").Range("I3:I32")
'''or use the following line
'pintColorIndex = Worksheets("config").Range("I3:I32").Value
'Changes Tab Color every other worksheet 2-count
For i = 1 To Worksheets.Count
'Assignning each array color to worksheet tab
Worksheets(i).Tab.ColorIndex = pintColorIndex(i)
'Continuing to next worksheet
Next i
'Turn Screen Updating "ON"
Application.ScreenUpdating = True
End Sub
> You might also be able to do something like this:
>
[quoted text clipped - 13 lines]
> Notice there is just an "i" in the array instead of "i - 1" because I
> explicitly declared it as 1-based, not 0 based.
Thank you for your help,
jfcby
Conan Kelly - 24 Jan 2008 19:19 GMT
jfcby,
Here is one way you could do it (read important notes after the code):
Sub WorkSheetsTabColor3()
'Worksheet Tab Color Assigned in Array
'Define Variables
Dim i As Integer
'Explicitly declare a 1-based 30 element integer array variable
Dim pintColorIndex(1 To 30) As Integer
Dim prngCell As Range
Dim prngRange As Range
'Sreen does not flash (Turn Screen Updating "OFF")
Application.ScreenUpdating = False
Set prngRange = Worksheets("config").Range("I3:I32")
i = 1
'Changes Tab Color every other worksheet 2-count
For Each prngCell In prngRange.Cells
'Assignning each array color to worksheet tab
Worksheets(i).Tab.ColorIndex = prngCell.Value
i = i + 1
'Continuing to next worksheet
Next prngCell
'Turn Screen Updating "ON"
Application.ScreenUpdating = True
End Sub
In this example, I added a Range variable and a Cell variable:
Dim prngCell As Range
Dim prngRange As Range
I then assigned I3:I32 to the range variable (also set i equal to 1):
Set prngRange = Worksheets("config").Range("I3:I32")
i = 1
Then I looped through each cell in the range, setting the "i'th" worksheet
tab color index to the value of the cell, and I incremented "i".
There could be issues with this though. If you have 30 sheets to change the
color of and the range you use has more than 30 cells, then you will get
errors. If the range you use has less than 30 cells, only the same number
of sheets will get their colors changed, leaving off the last few sheets.
I'll work through an alternate method, more similar to your original code.
I'll post it in a few mins.
HTH,
Conan
Hello,
Example one worked.
> you could declare an array variable in your code like this:
>
[quoted text clipped - 17 lines]
> Declarations. Look up "Using Arrays" in VBA Help for more info. I don't
> recall if sheets are 0 based or 1 based. Expierment with it and see.
Example two did not work and this is my macro: How can it be modified
to work?
Sub WorkSheetsTabColor3()
'Worksheet Tab Color Assigned in Array
'Define Variables
Dim i As Integer
'Explicitly declare a 1-based 30 element integer array variable
Dim pintColorIndex(1 To 30) As Integer
'Sreen does not flash (Turn Screen Updating "OFF")
Application.ScreenUpdating = False
'Assignning array with Color Index Values
pintColorIndex = Worksheets("config").Range("I3:I32")
'''or use the following line
'pintColorIndex = Worksheets("config").Range("I3:I32").Value
'Changes Tab Color every other worksheet 2-count
For i = 1 To Worksheets.Count
'Assignning each array color to worksheet tab
Worksheets(i).Tab.ColorIndex = pintColorIndex(i)
'Continuing to next worksheet
Next i
'Turn Screen Updating "ON"
Application.ScreenUpdating = True
End Sub
> You might also be able to do something like this:
>
[quoted text clipped - 14 lines]
> Notice there is just an "i" in the array instead of "i - 1" because I
> explicitly declared it as 1-based, not 0 based.
Thank you for your help,
jfcby
Conan Kelly - 24 Jan 2008 19:32 GMT
jfcby,
Now for this one:
Sub WorkSheetsTabColor3()
'Worksheet Tab Color Assigned in Array
'Define Variables
Dim i As Integer
'Explicitly declare a 1-based 30 element integer array variable
Dim pintColorIndex(1 To 30) As Integer
Dim prngCell As Range
Dim prngRange As Range
'Initialize variables
Set prngRange = Worksheets("config").Range("I3:I32")
i = 1
'Assignning array with Color Index Values
For Each prngCell In prngRange.Cells
pintColorIndex(i) = prngCell.Value
i = i + 1
Next prngCell
'Sreen does not flash (Turn Screen Updating "OFF")
Application.ScreenUpdating = False
For i = 1 To Worksheets.Count
'Assignning each array color to worksheet tab
Worksheets(i).Tab.ColorIndex = pintColorIndex(i)
'Continuing to next worksheet
Next i
'Turn Screen Updating "ON"
Application.ScreenUpdating = True
End Sub
I haven't tested this...don't know if it will work.
With this one, I left your loop alone, but I added a loop before that to
assign the ColorIndex values to each element of the array variable.
There are still possiblity for errors here: if the number of cells does not
equal the number of elements in the array variable, or the number of sheets
does not equal the number of elements in the array.
This code needs some work to be completely error free (or to handle errors)
if the number of cells or number of sheets change from file to file.
HTH,
Conan
Hello,
Example one worked.
> you could declare an array variable in your code like this:
>
[quoted text clipped - 17 lines]
> Declarations. Look up "Using Arrays" in VBA Help for more info. I don't
> recall if sheets are 0 based or 1 based. Expierment with it and see.
Example two did not work and this is my macro: How can it be modified
to work?
Sub WorkSheetsTabColor3()
'Worksheet Tab Color Assigned in Array
'Define Variables
Dim i As Integer
'Explicitly declare a 1-based 30 element integer array variable
Dim pintColorIndex(1 To 30) As Integer
'Sreen does not flash (Turn Screen Updating "OFF")
Application.ScreenUpdating = False
'Assignning array with Color Index Values
pintColorIndex = Worksheets("config").Range("I3:I32")
'''or use the following line
'pintColorIndex = Worksheets("config").Range("I3:I32").Value
'Changes Tab Color every other worksheet 2-count
For i = 1 To Worksheets.Count
'Assignning each array color to worksheet tab
Worksheets(i).Tab.ColorIndex = pintColorIndex(i)
'Continuing to next worksheet
Next i
'Turn Screen Updating "ON"
Application.ScreenUpdating = True
End Sub
> You might also be able to do something like this:
>
[quoted text clipped - 14 lines]
> Notice there is just an "i" in the array instead of "i - 1" because I
> explicitly declared it as 1-based, not 0 based.
Thank you for your help,
jfcby
jfcby - 24 Jan 2008 20:53 GMT
Hello Conan Kelly,
Thank you for help, The codes work great and learned how to use
arrays!
jfcby
Conan Kelly - 24 Jan 2008 21:20 GMT
Glad I could help.
> Hello Conan Kelly,
>
> Thank you for help, The codes work great and learned how to use
> arrays!
>
> jfcby