How to exclude a portion of the VBA script? In this script, how do I exclude the script for ''last name''?

Sunnygreet

New Member
Joined
Apr 4, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Date Time User ID User Name Worksheet Cell Action Old Value New Value
Dim nRow As Long
Dim bCompliant As Boolean
Dim wsAudit As Worksheet
Dim ChangeDate As String, ChangeTime As String, FirstName As String, LastName As String, FullName As String, UserID As String
Dim oldValue(1 To 4) As Variant '1 to 4 non-contiguous blocks of cells may be changed at a time. This limit is arbitrary.
Dim newValue(1 To 4) As Variant 'Should be same as oldValue
Dim ar As Range, cel As Range, rg As Range, cellHome As Range
Dim i As Long, n As Long, j As Long, cols As Long, k As Long, nAreas As Long

Set wsAudit = Worksheets("Audit Trail") 'The Audit Trail worksheet must be named Audit Trail
Set rg = Target
Set cellHome = ActiveCell
nAreas = rg.Areas.Count

If Sh.Name = wsAudit.Name Then 'Don't trap changes on Audit Trail worksheet
ElseIf rg.Cells.Count > 20 Then 'Too many cells changed. Don't trap changes, as might have been row or column insertion/deletion
ElseIf nAreas > UBound(oldValue) Then 'Too many non-contiguous cell ranges being changed. Don't accept or trap changes.

MsgBox "Please change " & UBound(oldValue) & " or fewer non-contiguous blocks of cells"
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Else
Application.EnableEvents = False
For k = 1 To nAreas
newValue(k) = rg.Areas(k).Value
Next

Application.Undo
For k = 1 To nAreas
oldValue(k) = rg.Areas(k).Value
Next
Application.Undo

nRow = wsAudit.Cells(wsAudit.Rows.Count, 1).End(xlUp).Row + 1
FirstlName = InputBox("What is your first name?")
If FirstName <> "" Then
bCompliant = True
LastName = InputBox("What is your last name?")
If LastName <> "" Then
FullName = FirstName & " " & LastName
Else
bCompliant = False
End If
End If

If bCompliant = False Then
MsgBox "This workbook is being closed because you didn't enter your name."
ThisWorkbook.Close SaveChanges:=False

End If

Application.EnableEvents = False
Application.ScreenUpdating = False
'If wsAudit.Visible <> xlSheetHidden Then wsAudit.Visible = xlSheetVeryHidden

ChangeDate = Format(Date, "m/d/yyyy")
ChangeTime = Format(Now(), "h:mm")
If rg.Cells.Count > 1 Then
For k = 1 To nAreas
Set ar = rg.Areas(k)

If ar.Cells.Count = 1 Then
wsAudit.Cells(nRow, 1).Resize(1, 9).Value = _
Array(ChangeDate, ChangeTime, , FullName, Sh.Name, cel.Address, "Change", oldValue(k), newValue(k))
nRow = nRow + 1
Else
n = ar.Rows.Count
cols = ar.Columns.Count
For i = 1 To n
For j = 1 To cols
Set cel = ar.Cells(i, j)
wsAudit.Cells(nRow, 1).Resize(1, 9).Value = _
Array(ChangeDate, ChangeTime, , FullName, Sh.Name, cel.Address, "Change", oldValue(k)(i, j), newValue(k)(i, j))
nRow = nRow + 1
Next
Next
End If
Next
Else
wsAudit.Cells(nRow, 1).Resize(1, 9).Value = Array(ChangeDate, ChangeTime, , FullName, Sh.Name, rg.Address, "Change", oldValue, rg.Value)
End If

Application.EnableEvents = True
End If
Application.Goto cellHome

End Sub
 
I have saved the worksheet here. Please edit and save

 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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