View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Kevin Baker[_2_] Kevin Baker[_2_] is offline
external usenet poster
 
Posts: 13
Default Can't Seem to find problem with this VBA Code

Dave,

Your code was perfect.. I do have more code under the same Private Sub and
wanted to get your take on how to "tweak" it. I've tried but I can't seem
to get it to work. Here is the code:

If Target.Column = 6 And Target.Value 1 And Target.Offset(0, 1) = "" Then
Target.Offset(0, 1).Interior.ColorIndex = 36
ElseIf Target.Column = 6 Then
Target.Offset(0, 1).Interior.ColorIndex = xlNone
ElseIf Target.Column = 7 Then
Target.Interior.ColorIndex = xlNone
End If

I want to change the color of the cell in Column G if cell F is greater than
0 AND Cell H is blank.. I was copied the above code from somewhere else, so
I'm not exactly sure why the elseif statements are there. The above worked
but I would sometimes get the same error code I was having trouble with
before. Any ideas?

Thanks a million,
Kevin
"Dave Peterson" wrote in message
...
In fact, if I'm going to check for numbers, I should do it before I
add/subtract, too:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

With Target
If .Cells.Count 1 Then Exit Sub
If IsNumeric(.Value) = False Then Exit Sub

If Intersect(.Cells, Me.Range("f:f,h:h")) Is Nothing Then
Exit Sub
End If

If Target.Value <= 1 Then Exit Sub

On Error GoTo errHandler:

Application.EnableEvents = False
Select Case .Column
Case Is = 6
If IsNumeric(.Offset(0, 2).Value) Then
.Offset(0, 3).Value = .Value - .Offset(0, 2).Value
End If
Case Is = 8 'only thing left
If IsNumeric(.Offset(0, -2).Value) Then
.Offset(0, 1).Value = .Offset(0, -2).Value - .Value
End If
End Select
End With

errHandler:
Application.EnableEvents = True

End Sub

Dave Peterson wrote:

This portion:

Target.Offset(0, -2).Value

Refers to the cell that is 2 columns to the left. If you're typing
something
into column A (or even column B), then there's trouble.

I'm not quite sure if this fits your needs, but I'd do the checking up
front:

<<snipped

The .enableevents stuff tells excel to not invoke the worksheet_change
event
when the code writes to the worksheet.

Kevin Baker wrote:

If Target.Column = 6 And Target.Value 1 And Target.Offset(0, 2).Value
1
Then
Target.Offset(0, 3).Value = Target.Value - Target.Offset(0,
2).Value
ElseIf Target.Column = 8 And Target.Value 1 And
Target.Offset(0, -2).Value 1 Then
Target.Offset(0, 1).Value = Target.Offset(0, -2).Value -
Target.Value
End If

I get the following when entering data in Cell A on a new line:
"Run-Time error '1004'
Application-defined or object-defined error

Any thoughts?
Kevin


--

Dave Peterson


--

Dave Peterson