PC Review


Reply
Thread Tools Rate Thread

Auto Cell Color using VB

 
 
TNfisherman
Guest
Posts: n/a
 
      13th Jun 2008
I am trying to auto color cells using this VB code.



Private Sub Worksheet_Change(ByVal Target As Range)
Dim icolor As Integer

If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
Select Case Target
Case 1
icolor = 6
Case 2
icolor = 12
Case 3
icolor = 7
Case 4
icolor = 53
Case 5
icolor = 15
Case 6
icolor = 42
Case 7
icolor = 1
Case 8
icolor = 20
Case 9
icolor = 30
Case 10
icolor = 40
Case 11
icolor = 51
Case 12
icolor = 14

Case Else
'Whatever
End Select

Target.Interior.ColorIndex = icolor
End If

End Sub

The problem I am having is I have to either hard code in data to change the
color of the cell or I can only copy data into cells one cell at a time to
change the color. What I would like to do is copy data that is on multiple
rows and past it to the work sheet with the VB code and have all of those
cells auto color to my specified colors. I am getting a Debug error stating
"Run time error '13' Type Mismatch" Any help will be greatly appreciated.




 
Reply With Quote
 
 
 
 
cush
Guest
Posts: n/a
 
      13th Jun 2008
Try changing your Worksheet_Change code to:


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
ColorMultiCells
End If
End Sub

Then add the following sub to a standard Module:

Option Explicit

Sub ColorMultiCells()

Dim icolor As Integer
Dim oCell As Range

For Each oCell In Selection
Select Case oCell.Value
Case 1
icolor = 6
Case 2
icolor = 12
Case 3
icolor = 7
Case 4
icolor = 53
Case 5
icolor = 15
Case 6
icolor = 42
Case 7
icolor = 1
Case 8
icolor = 20
Case 9
icolor = 30
Case 10
icolor = 40
Case 11
icolor = 51
Case 12
icolor = 14

Case Else
'Whatever
End Select

oCell.Interior.ColorIndex = icolor

Next oCell
End Sub




"TNfisherman" wrote:

> I am trying to auto color cells using this VB code.
>
>
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim icolor As Integer
>
> If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
> Select Case Target
> Case 1
> icolor = 6
> Case 2
> icolor = 12
> Case 3
> icolor = 7
> Case 4
> icolor = 53
> Case 5
> icolor = 15
> Case 6
> icolor = 42
> Case 7
> icolor = 1
> Case 8
> icolor = 20
> Case 9
> icolor = 30
> Case 10
> icolor = 40
> Case 11
> icolor = 51
> Case 12
> icolor = 14
>
> Case Else
> 'Whatever
> End Select
>
> Target.Interior.ColorIndex = icolor
> End If
>
> End Sub
>
> The problem I am having is I have to either hard code in data to change the
> color of the cell or I can only copy data into cells one cell at a time to
> change the color. What I would like to do is copy data that is on multiple
> rows and past it to the work sheet with the VB code and have all of those
> cells auto color to my specified colors. I am getting a Debug error stating
> "Run time error '13' Type Mismatch" Any help will be greatly appreciated.
>
>
>
>

 
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
Auto Fill Color Cell Steve Microsoft Excel Worksheet Functions 1 15th Jul 2008 01:18 AM
Auto Cell Color using VB TNfisherman Microsoft Excel Misc 4 13th Jun 2008 07:56 PM
Auto Fill Color of a particular Cell if conditions are put Akash Microsoft Excel Programming 8 4th Jan 2007 12:55 PM
Auto Cell Color =?Utf-8?B?Q2h1Y2tlcnM=?= Microsoft Excel Misc 9 30th Mar 2006 01:56 AM
Auto Change in Cell Color =?Utf-8?B?Y2FybA==?= Microsoft Excel Worksheet Functions 1 24th Mar 2006 04:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:01 PM.