View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.newusers,microsoft.public.excel.programming
Johan De Schutter Johan De Schutter is offline
external usenet poster
 
Posts: 5
Default Cell right next to colored cells is automatically colored on entering a value

Hello,

I recently started to program in VBA for Excel.

I'm using Excel 2000 (9.0.4402 SR-1) on Windows XP.
The formatting from all the cells in the workbook and worksheet is General
(Right-click on a cell, Format cells, then tab Number, Category-listbox:
General)

When I put a commandbutton CommandButton1 on the first worksheet of my
workbook and
I put the following code in the CommandButton1-onclick-event.

-------------------
Option Explicit

Private Sub CommandButton1_Click()
Dim WeekdayNames(0 To 6) As String
Dim WeekdayColors(0 To 6) As Long

WeekdayNames(0) = "monday"
WeekdayNames(1) = "tuesday"
WeekdayNames(2) = "wednesday"
WeekdayNames(3) = "thursday"
WeekdayNames(4) = "friday"
WeekdayNames(5) = "saturday"
WeekdayNames(6) = "sunday"

WeekdayColors(0) = RGB(255, 0, 0) ' red
WeekdayColors(1) = RGB(255, 0, 0) ' red
WeekdayColors(2) = RGB(255, 0, 0) ' red
WeekdayColors(3) = RGB(255, 0, 0) ' red
WeekdayColors(4) = RGB(255, 0, 0) ' red
WeekdayColors(5) = RGB(0, 0, 255) ' blue
WeekdayColors(6) = RGB(0, 0, 255) ' blue

Dim i As Integer
For i = 0 To 6
ThisWorkbook.Worksheets(1).Range("A1").Offset(0, i).Interior.Color =
WeekdayColors(i)
Next i

Dim TempRange As Range
Set TempRange = ThisWorkbook.Worksheets(1).Range("A1:G1")
TempRange.Value = WeekdayNames
End Sub
--------------------

When I click on the button the code is executed without any errors.
The code writes the name of the day in the cells A1 till G1 and it colors
the cells with the workdays (monday till friday) as red and
the cells with the days of the weekend (saturday and sunday) as blue. No
problem until so far.

!!! But when I select cell H1 and a enter a character (for example 'm' or
'j') cell H1 is also colored red.
And when I select cell I1 and a enter a character, cell I1 is also colored
red.
And when I select J1 and enter a value, J1 is colored red..... and so on.

When I enter a number into cell H1, nothing happens, the color of H1 does
not change.
Then I select I1 and enter a character, nothing happens, the color of I1
does not change.

It looks like there happens some kind of auto-fill for the colors of the
cells.

My questions:
1) Why does the color of H1,I1, .... changes, when I enter a character?
Maybe because the formatting of the cells is General and not Text?
Can I prevent this "auto-coloring" ?
2) How do I change the formatting of a cel by VBA-code? For example: change
from General to Text
3) TempRange.Value = WeekdayNames automatically fills a range with the
array-values
Can i make an array of RGB-values and assign this array to the
background colors of a range

Like this:
Dim WeekColors(0 To 6) As Long

Colors(0) = RGB(0,255,255)
... = .........
Colors(6) = RGB(255,128, 0)

TempRange.Interior.Color = WeekColors

kind regards,

Johan De Schutter
Software designer

mailto:

Nisus nv
Antwerpsesteenweg 107, B-2390 Oostmalle
Tel: +32(0)3/312.92.30

Please visit our website:
http://www.nisus.be