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
 
Still getting same error. This is the code I have so far. Please review
If you are getting the same error then you need to check all your variables have been declared (Dimmed) and you have spelt them correctly, Excel shows you which lines need looking at as you obviously have Option Explicit at the top of the code (I don't get the same error once that one has been corrected but I don't have a workbook set up to test the rest of your code is working correctly).
 
Last edited:
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
In your latest code ans isn't declared
Rich (BB code):
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
 
Last edited:
Upvote 0
AND, the full code you posted isn't the latest code, as it still has LastName in it !!
 
Upvote 0
AND, the full code you posted isn't the latest code, as it still has LastName in it !!

In your latest code ans isn't declared
Rich (BB code):
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
In your latest code ans isn't declared
Rich (BB code):
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
That fixed the error but in the below script, ans=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. It only closes it for a fraction of a second and then is open again. 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.

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

Dim ans



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
The workbook actually doesn't close when I click cancel in the msgbox
That is because you have a line to close it when clicking Ok (vbOK) and not for Cancel (vbCancel).
You need to add a line to tell it what to do if you click Cancel.
 
Upvote 0
If all you are doing is closing and not saving the workbook then It is the same line you are using for the vbOK just the vbOK in the new line changed to vbCancel, SaveChanges = False and IF changed to ElseIf, you can write that yourself.
 
Last edited:
Upvote 0
If all you are doing is closing and not saving the workbook then It is the same line you are using for the vbOK just the vbOK in the new line changed to vbCancel, SaveChanges = False and IF changed to ElseIf, you can write that yourself.
It workbook still doesn't close after adding a new line as below

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
If ans = vbOKCancel Then ThisWorkbook.Close SaveChanges:=False
 
Upvote 0
Not vbOKCancel... vbCancel
It still doesn't close the worksheet. It closes it for a blink and then is open again.

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
If ans = vbCancel Then ThisWorkbook.Close SaveChanges:=False
End If
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

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