PDA

View Full Version : Solved: Change worksheet value to ensure data integrity



khalid79m
09-08-2009, 04:13 AM
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A3") <> "" Then
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Intersect(Cells.SpecialCells(xlCellTypeAllValidation), Union(Columns(47), Columns(48), Columns(53), Columns(54), Columns(59), Columns(60), Columns(65), Columns(66), Columns(70), Columns(71)))
'rngDV.Select
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Target.Value = "" 'optional line depending on what you want to happen if Delete is pressed or an empty string is chosen
Else
Target.Value = oldVal & ", " & newVal
Target.Columns.AutoFit
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End If
End Sub




The above code is fine and I dont want to change it, all it does is allow multiple entries into 1 cell.

I need to write another bit of code which i have no idea how to do , this code needs to clearcontents within the same row if a value changes

eg in

AR7 changes i need it to clear contents in AS7 , AT7, AU7, AU8.

also if

AS7 changes i need to clear contetns in AT7 , AU7, AU8

In the example i have used row 7 but it could be any row from 3 to the lastrow od data.

Any help is appreicated.. Im at a complete dead end... :help

rbrhodes
09-08-2009, 10:50 PM
Hi,


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'//Added
Dim tRow As Long
'No recursion
Application.EnableEvents = False
'Get row that changed
tRow = Target.Row
' any row from 3 to the lastrow = must be 3 or gretaer
If tRow >= 3 Then
'AR# changes i need it to clear contents in AS7 , AT7, AU7, AU8.
If Not Intersect(Target, Columns("AR:AR")) Is Nothing Then
Range("AS" & tRow & "AU" & tRow).ClearContents
Range("AU" & tRow + 1).ClearContents
'Reset
Application.EnableEvents = True
'AS# changes i need to clear contents in AT7 , AU7, AU8
ElseIf Not Intersect(Target, Columns("AS:AS")) Is Nothing Then
Range("AT" & tRow & "AU" & tRow).ClearContents
Range("AU" & tRow + 1).ClearContents
End If
End If
'//End added
If Range("A3") <> "" Then

Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Intersect(Cells.SpecialCells(xlCellTypeAllValidation), Union(Columns(47), Columns(48), Columns(53), Columns(54), Columns(59), Columns(60), Columns(65), Columns(66), Columns(70), Columns(71)))
'rngDV.Select
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Not Intersect(Target, rngDV) Is Nothing Then
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal <> "" Then
If newVal = "" Then
'optional line depending on what you want to happen
if Delete is pressed or an empty string is chosen
Target.Value = ""
Else
Target.Value = oldVal & ", " & newVal
Target.Columns.AutoFit
End If
End If
End If
exitHandler:

'Cleanup
Set rangeDV = Nothing

'Reset
Application.EnableEvents = True
End If
End Sub

khalid79m
10-11-2009, 01:05 PM
thanks works a treeat :)

rbrhodes
10-12-2009, 03:44 AM
Cheers!