View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Ken Johnson Ken Johnson is offline
external usenet poster
 
Posts: 1,073
Default How can I leave the contents of a cell alone in an IF statemen

Hi Ken,

If my interpretation of your question is correct I would like to add
that it is not a very good way to handle your data.
Say you wish to correct a column B value after the column C value had
already been added and therefore converted to the sum of the B and C
values. Excel can't then revise the new column C value because there is
no record of the original value typed in by the user. If the user
neglects to reenter the original column C value (I assume stored on
Paper or some other medium external to Excel) after revising a column B
value the worksheet will contain an error. The only solution to that
would be to prompt the user to reenter the original column C value if
the corresponding B cell was changed while the C cell is not blank.
All too complicated.

The following code changes incorrect C values to "Re-Enter" and a
MsgBox prompts the user to re-enter those C values that would be
incorrect because of a review of the B value...

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, _
Range("C2:C" & Rows.Count)) Is Nothing Then
With Application
..EnableEvents = False
..ScreenUpdating = False
End With
Dim rngCell As Range
For Each rngCell In Intersect(Target, _
Range("C2:C" & Rows.Count))
On Error Resume Next
rngCell.Value = rngCell.Value + _
rngCell.Offset(0, -1).Value
Next rngCell
End If
If Not Intersect(Target, _
Range("B2:B" & Rows.Count)) Is Nothing Then
Dim blnReEnter As Boolean
For Each rngCell In Intersect(Target, _
Range("B2:B" & Rows.Count)).Offset(0, 1)
If rngCell.Value < "" Then
rngCell.Value = "Re-Enter"
blnReEnter = True
End If
Next rngCell
End If
If blnReEnter Then MsgBox _
"One or more Column C values need to be re-entered"
blnReEnter = False
Application.EnableEvents = True
End Sub

However, you should instead have separate columns for the original C
values and the sum of B and C values, removing the need for any code.

Ken Johnson