Help with modifying data

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
289
Office Version
  1. 2016
Platform
  1. Windows
I am using the VBA below. I need it to do two things. One is if a user tries to "clear content" in column D, they should get a message box saying that they can't do that without first clearing their entry in column A. Second I noticed that if I select multiple entry's in column A and use the clear content, the info in column D does not clear. If I select just one entry it works fine, can this be fixed?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

 On Error GoTo Catch

 '// Prevent any changes firing this event again
 Application.EnableEvents = False

 '// Change the '1' to ignore any heading rows
1 If Target.Row > 1 Then

 '// Which column was changed?
Select Case Target.Column
'// A
Case 1
Target.Offset(, 3).Value = IIf(Len(Target.Value), "Changed By " & Environ("UserName") & " on " & Date & " @ " & Time, "")
Case 4
'// Prevent user changes to Col D
Application.Undo
Beep
 End Select

 End If

Catch:

 '// Make sure event handling turned on again
 Application.EnableEvents = True

End Sub

If anyone can help, thank you for your time.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'variables used
    Dim colA As Range, colD As Range, Cel As Range
'set ranges as required
    Set colA = Range("A2").Resize(Rows.Count - 1)
    Set colD = colA.Offset(, 3)
'cells amended that are in required ranges
    Set colA = Intersect(colA, Target)
    Set colD = Intersect(colD, Target)
'ensure events are ALWAYS switched back on
    On Error GoTo Catch
    Application.EnableEvents = False
'Prevent user changes to Col D
    If Not colD Is Nothing Then
        Application.Undo
        Beep
    End If
'loop all changed cells in column A and amend corresponding cell in column D
    If Not colA Is Nothing Then
        For Each Cel In colA
            Cel.Offset(, 3).Value = IIf(Len(Cel.Value), "Changed By " & Environ("UserName") & " on " & Date & " @ " & Time, "")
        Next Cel
    End If

Catch:
'ensure event handling turned on again
    Application.EnableEvents = True
End Sub
 
Upvote 0
The only thing that it doesn't do is give an error msg box when the user tries to clear column D without first clearing column A. As far as I can tell, it does EVERYTHING else I asked for!!!
 
Upvote 0
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'variables used
    Dim colA As Range, colD As Range, Cel As Range
'set ranges as required
    Set colA = Range("A2").Resize(Rows.Count - 1)
    Set colD = colA.Offset(, 3)
'cells amended that are in required ranges
    Set colA = Intersect(colA, Target)
    Set colD = Intersect(colD, Target)
'ensure events are ALWAYS switched back on
    On Error GoTo Catch
    Application.EnableEvents = False
'Prevent user changes to Col D
    If Not colD Is Nothing Then
        Application.Undo
        Beep
    End If
'loop all changed cells in column A and amend corresponding cell in column D
    If Not colA Is Nothing Then
        For Each Cel In colA
            Cel.Offset(, 3).Value = IIf(Len(Cel.Value), "Changed By " & Environ("UserName") & " on " & Date & " @ " & Time, "")
        Next Cel
    End If

Catch:
'ensure event handling turned on again
    Application.EnableEvents = True
End Sub


I think I like it like it is though!! Thank You Very Much. Works Perfectly!!!
 
Upvote 0
Amend this bit
Rich (BB code):
    If Not colD Is Nothing Then
        Application.Undo
        Beep
        MsgBox "Can't do that",,""
    End If
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top