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
 

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.
If I understand you...comment out these lines

LastName = InputBox("What is your last name?")
If LastName <> "" Then
FullName = FirstName & " " & LastName
Else
bCompliant = False
End If
 
Upvote 0
Thanks that worked! I'm having another different issue..

In the above script, MsgBox "This workbook is being closed because you didn't enter your name." The workbook actually doesn't close when I click cancel in the msgbox. I want the script to close the workbook when cancel is clicked. With the current script it just deactivates the entire script when I click cancel in the MsgBox.
 
Upvote 0
Change these lines

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


to

ans =MsgBox ("This workbook is being closed because you didn't enter your name.",vbQuestion + vbOKCancel)
If ans = vbOK then ThisWorkbook.Close SaveChanges:=True
 
Upvote 0
You need to reset the macro under the Run button to take you out of error mode
 
Upvote 0
Actually I'm getting ''Compile error: Variable not defined'' and then on clicking ok I get '' Can't execute code in break mode''
 
Upvote 0
It should be FirstName in the line below
Rich (BB code):
FirstlName = InputBox("What is your first name?")
 
Upvote 0
Still getting same error. This is the code I have so far. Please review

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, 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
FullName = InputBox("Scan your full name")
If FullName <> "" 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
ans = MsgBox("This workbook is being closed because you didn't enter your name.", vbQuestion + vbOKCancel)
If ans = vbOK Then ThisWorkbook.Close SaveChanges:=True
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
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,706
Members
449,118
Latest member
MichealRed

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