You had earlier helped me in creating change history, where changes that occur in certain cells in the MasterList are recorded in a sheet called Change_History. This sheet has 6 cols A:F where col A=Date / time, B=Changed By, C=Approved By, D=Old Parameter, E= new Parameter and F= Changed Cell Address.
The codes are as below for your ready reckoning.
now what I want is :
When the user selects cell in col C(row > 2) then the "Enter Password ............." msg box is popped up, the user enters one of the following two passwords 75296 or 75297, and the name corresponding to this password appears in the current cell in Col C.
The list of Passwords and their names are in sheet DV, range T2 and U2 down to T11 and U11 as below
Col T Col U
Password Name
75288 Ramesh K
75289 Sidharth Pawar
75290 Chitrakala
75291 Vinayak rao
75292 Durrani L
75293 Manoj Saraf
75294 Rajnath Singh
75295 Suresh Murthy
75296 Sayali
75297 Suryakant L
Please advise.
Best regards
Vilas Desai
Code: Select all
Option Explicit
Dim mbNoEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vPrevValue As Variant
Dim vCurValue As Variant
Dim lngIndex As Long
Dim strSource As String
Dim strDestination As String
Dim strList As String
Dim wshSource As Worksheet
Dim wshDestination As Worksheet
Dim wshList As Worksheet
Dim rngSource As Range
Dim rngDestination As Range
Dim lngRowCount As Long
Dim lngCount As Long
Dim wshDV As Worksheet
Dim strPW As String
Dim varUser As Variant
Dim current_row As Long
Dim column_numbers As Variant
Dim col_counter As Long
Static Exempt_Range As Range
If mbNoEvent Then Exit Sub
If Target.Row < 5 Or Target.Column > 27 Then Exit Sub
If Target.Address = ActiveSheet.ListObjects(1).HeaderRowRange.Offset(ActiveSheet.ListObjects(1).ListRows.Count).Address _
And Target.Rows.Count = 1 Then
current_row = Target.Row
column_numbers = Split("G,I,J,L,M,X,Y,Z", ",")
Set Exempt_Range = Nothing
For col_counter = LBound(column_numbers) To UBound(column_numbers)
If Exempt_Range Is Nothing Then
Set Exempt_Range = Range(column_numbers(col_counter) & current_row)
Else
Set Exempt_Range = Union(Exempt_Range, Range(column_numbers(col_counter) & current_row))
End If
Next
Exit Sub
End If
If Target.Address = Target.EntireRow.Address Then Exit Sub
If Exempt_Range Is Nothing Then
current_row = Target.Row
column_numbers = Split("G,I,J,L,M,X,Y,Z", ",")
Set Exempt_Range = Nothing
For col_counter = LBound(column_numbers) To UBound(column_numbers)
If Exempt_Range Is Nothing Then
Set Exempt_Range = Range(column_numbers(col_counter) & current_row)
Else
Set Exempt_Range = Union(Exempt_Range, Range(column_numbers(col_counter) & current_row))
End If
Next
End If
If Not (Application.Intersect(Target, Exempt_Range) Is Nothing) Then Exit Sub
If Target.Count > 1 Then
Application.EnableEvents = False
Application.Undo
MsgBox "Multiple cell changes are Not allowed.", vbExclamation
Application.EnableEvents = True
Exit Sub
End If
On Error GoTo ErrHandler
mbNoEvent = True
' Get previous and current value
vCurValue = Target.Value
Application.EnableEvents = False
Application.Undo
vPrevValue = Target.Value
If vPrevValue = "" Then
Target.Value = vCurValue
ElseIf vPrevValue = vCurValue Then
' Ignore
Else
strPW = InputBox("Enter your password")
Set wshDV = Worksheets("DV")
varUser = Application.VLookup(strPW, wshDV.Range("PasswordList"), 2, False)
If IsError(varUser) Then
MsgBox "Password invalid. Change not allowed!", vbCritical
GoTo ExitHandler
End If
Target.Value = vCurValue
Call SetHistory(varUser, vPrevValue, vCurValue, Target.Address)
End If
Application.EnableEvents = True
If Intersect(Target, Me.Range("D:D")) Is Nothing Then
If Not Intersect(Target, Me.Range("E:E")) Is Nothing Then
Call CheckCol_E(Intersect(Target, Me.Range("E:E")).Offset(0, -1))
End If
GoTo ExitHandler
End If
Set wshList = Worksheets("List")
If vCurValue = vPrevValue Then
GoTo ExitHandler
End If
If vPrevValue <> "" Then
lngCount = Application.WorksheetFunction.CountIf(Me.Range("D5:" & Target.Address), vPrevValue)
' Get info about previous source range
lngIndex = Application.WorksheetFunction.Match(vPrevValue, wshList.Range("A:A"), 0)
strSource = wshList.Range("B" & lngIndex)
Set wshSource = Worksheets(strSource)
strSource = wshList.Range("C" & lngIndex)
Set rngSource = wshSource.Range(strSource)
lngRowCount = rngSource.Rows.Count
' Get info about previous destination
strDestination = wshList.Range("D" & lngIndex)
Set wshDestination = Worksheets(strDestination)
strDestination = wshList.Range("E" & lngIndex)
Set rngDestination = wshDestination.Range(strDestination).Offset(lngCount * lngRowCount, 0)
Set rngDestination = rngDestination.Resize(rngSource.Rows.Count, rngSource.Columns.Count)
' Delete
rngDestination.Delete Shift:=xlShiftUp
End If
If vCurValue <> "" Then
lngCount = Application.WorksheetFunction.CountIf(Me.Range("D5:" & Target.Address), vCurValue) - 1
' Get info about current source range
lngIndex = Application.WorksheetFunction.Match(vCurValue, wshList.Range("A:A"), 0)
strSource = wshList.Range("B" & lngIndex)
Set wshSource = Worksheets(strSource)
strSource = wshList.Range("C" & lngIndex)
Set rngSource = wshSource.Range(strSource)
lngRowCount = rngSource.Rows.Count
' Get info about current destination
strDestination = wshList.Range("D" & lngIndex)
Set wshDestination = Worksheets(strDestination)
strDestination = wshList.Range("E" & lngIndex)
Set rngDestination = wshDestination.Range(strDestination).Offset(lngCount * lngRowCount, 0)
Set rngDestination = rngDestination.Resize(rngSource.Rows.Count, rngSource.Columns.Count)
strDestination = rngDestination.Address
rngDestination.Insert Shift:=xlDown
Set rngDestination = rngDestination.Worksheet.Range(strDestination)
' Copy
rngSource.Copy Destination:=rngDestination
rngSource.Copy
rngDestination.PasteSpecial Paste:=xlPasteColumnWidths
Call UpdateValues(Target, rngDestination, wshList.Range("F" & lngIndex))
Call CheckCol_E(Target)
Else
Target.Offset(0, 1) = ""
End If
ExitHandler:
mbNoEvent = False
Application.EnableEvents = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Code: Select all
Sub Clr_History()
Dim strPW As String
Dim strName As String
Dim wshDV As Worksheet
Set wshDV = Worksheets("DV")
strPW = InputBox("Enter password to clear history")
Select Case strPW
Case "75291", "75292", "75293"
strName = Application.VLookup(strPW, wshDV.Range("PasswordList"), 2, False)
Application.ScreenUpdating = False
With Worksheets("Change_History")
.Range("A3:G50000,F3:V50000").Clear
.Range("A3").Value = Now
.Range("C3").Value = strName
End With
Case Else
MsgBox "You don't have permission to clear history!", vbExclamation
End Select
End Sub
Code: Select all
Sub SetHistory(varUser, vPrevValue, vCurValue, sAddress)
Dim wshHist As Worksheet
Dim r As Long
Set wshHist = Worksheets("CHANGE_HISTORY")
r = wshHist.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
wshHist.Cells(r, 1).Value = Now
wshHist.Cells(r, 2).Value = varUser
wshHist.Cells(r, 4).Value = vPrevValue
wshHist.Cells(r, 5).Value = vCurValue
wshHist.Cells(r, 6).Value = sAddress
End Sub