Sunnygreet
New Member
- Joined
- Apr 4, 2023
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
In the attached VBA code below, how do I add an user verification (''FullName'') where the user list (master list) is managed through a different excel file? Only users in the separate master list should be able to access the workbook with the below VBA code when they enter their FullName.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime NextBackup, "AutoBackup", , False 'Stops the auto backup
On Error GoTo 0
End Sub
Private Sub Workbook_Open()
AutoBackup 'Starts the auto backup
End Sub
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)
End If
If ans = vbOK Then
Application.Quit
ThisWorkbook.Close SaveChanges:=False
ElseIf ans = vbCancel Then
Application.Quit
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