PC Review


Reply
Thread Tools Rate Thread

Clear Cells and Worksheet_Change Q

 
 
Sean
Guest
Posts: n/a
 
      23rd Oct 2007
I have the following code that clear cell content with a Range

Sub ClearCells()
Application.ScreenUpdating = False

Sheets("Log").Activate
ActiveSheet.Unprotect Password:="123"
Range("D7").Select
Selection.ClearContents

Application.DisplayFormulaBar = False

Range("D7").Select
Selection.Copy
Range("D7:J30").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone,
SkipBlanks:=False _
, Transpose:=False

Range("D7").Select
ActiveSheet.Protect Password:="123", DrawingObjects:=True,
Contents:=True, Scenarios:=True

End Sub


I also have a Worksheet_Change code that, basically changes a cells
colour when data is selected.

'-----------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
'-----------------------------------------------------------------
Const WS_RANGE As String = "D7:J30" '<==== change to suit

Application.EnableEvents = True
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Select Case LCase(.Value)
Case "Country 1":
.Font.ColorIndex = 2
.Interior.ColorIndex = 3 'red

Case "Country 2":
.Font.ColorIndex = 2
.Interior.ColorIndex = 3 'red

Case "Country 3":
.Font.ColorIndex = 2
.Interior.ColorIndex = 5 'blue

End Select
End With
End If
End Sub


My issue is that when I run my ClearContents macro (it clears the
cells) but I hit debug within the Worksheet_Change code at line (Type
Mismatch):-

Select Case LCase(.Value)

Why is that and how can I fix?

Thanks

 
Reply With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      23rd Oct 2007
Hi
A few things

You can clear the range by using - Range("D7:J30").ClearContents
There is no need to clear one cell and then copy to all others.

The Target returned is a range, therefore your Case test cannot be applied,
and has to carried out for each value in the range. Something like

For Each tcell In Target

With tcell

Select Case LCase(.Value)
Case Is = "country 1"
.Font.ColorIndex = 2
.Interior.ColorIndex = 3 'red

Case Is = "country 2"
.Font.ColorIndex = 2
.Interior.ColorIndex = 3 'red

Case Is = "country 3"
.Font.ColorIndex = 2
.Interior.ColorIndex = 5 'blue

End Select
End With

Next tcell

The logic to test using LCase values, then compare to an upper case value
(Lcase(.value) = "Country 1") will never work, should all be lower case in
the right hand side of the test.

Also in the Case test, using the single line operator ( will not work as
you have multiple lines, change the case testing to - Case Is = "country
1" etc. as shown in above example.

Also your logic to test the intersect may not be what you wanted?


--

Regards,
Nigel
(E-Mail Removed)



"Sean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have the following code that clear cell content with a Range
>
> Sub ClearCells()
> Application.ScreenUpdating = False
>
> Sheets("Log").Activate
> ActiveSheet.Unprotect Password:="123"
> Range("D7").Select
> Selection.ClearContents
>
> Application.DisplayFormulaBar = False
>
> Range("D7").Select
> Selection.Copy
> Range("D7:J30").Select
> Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone,
> SkipBlanks:=False _
> , Transpose:=False
>
> Range("D7").Select
> ActiveSheet.Protect Password:="123", DrawingObjects:=True,
> Contents:=True, Scenarios:=True
>
> End Sub
>
>
> I also have a Worksheet_Change code that, basically changes a cells
> colour when data is selected.
>
> '-----------------------------------------------------------------
> Private Sub Worksheet_Change(ByVal Target As Range)
> '-----------------------------------------------------------------
> Const WS_RANGE As String = "D7:J30" '<==== change to suit
>
> Application.EnableEvents = True
> If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
> With Target
> Select Case LCase(.Value)
> Case "Country 1":
> .Font.ColorIndex = 2
> .Interior.ColorIndex = 3 'red
>
> Case "Country 2":
> .Font.ColorIndex = 2
> .Interior.ColorIndex = 3 'red
>
> Case "Country 3":
> .Font.ColorIndex = 2
> .Interior.ColorIndex = 5 'blue
>
> End Select
> End With
> End If
> End Sub
>
>
> My issue is that when I run my ClearContents macro (it clears the
> cells) but I hit debug within the Worksheet_Change code at line (Type
> Mismatch):-
>
> Select Case LCase(.Value)
>
> Why is that and how can I fix?
>
> Thanks
>


 
Reply With Quote
 
Bill Renaud
Guest
Posts: n/a
 
      23rd Oct 2007
This code in your ClearCells routine:

Range("D7").Select
Selection.Copy
Range("D7:J30").Select
Selection.PasteSpecial _
Paste:=xlAll, _
Operation:=xlNone,
SkipBlanks:=False, _
Transpose:=False

1. You are changing more than one cell at a time, so Target is a
multiple-cell range. This is what is producing the Type Mismatch error in
the Worksheet_Change event handler. Change your ClearCells routine to set
each cell one at the time.

2. In your Worksheet_Change event handler, you are lower-casing the value
of the cell in your Select Case statement, then comparing it to a string
that has upper-case letters in it (i.e. "Country 1").

You should probably also have a "Case Else" clause after the 3 cases you
have listed, in case the value of the Target cell changes back to some
other value that needs the cell colors changed back to the original.

Case Else
.Font.ColorIndex = xlColorIndexAutomatic
.Interior.ColorIndex = xlColorIndexNone

--
Regards,
Bill Renaud



 
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
cells with formula fire worksheet_change when navigating thru them pwrob Microsoft Excel Programming 1 13th Feb 2010 01:17 AM
RE: Lock or Unlock Range of Cells on Worksheet_Change Event Mike H Microsoft Excel Worksheet Functions 0 13th Jul 2008 05:29 PM
Create a Clear button to clear unprotected cells Jcraig713 Microsoft Excel Programming 2 26th Nov 2007 03:55 PM
Worksheet_Change with discontinuous cells =?Utf-8?B?SktH?= Microsoft Excel Programming 1 13th Jul 2005 07:14 PM
worksheet_change event when multiple cells changed (pasted) noddy26 Microsoft Excel Programming 15 24th Jul 2004 09:59 PM


Features
 

Advertising
 

Newsgroups
 


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