VBA to update previous entered data

Skrej

Board Regular
Joined
May 31, 2013
Messages
159
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet of employees.

Through a userform, I enter various updates on performance, status, etc. Each update is entered on a new line. An employee might be assigned to different departments.

What I'd like is some guidance on VBA that will retroactively update all prior entries for a given person in the 'current?' column ONLY for the given department once I enter one of several various keywords for the 'review' column, such as 'dismissed', 'promoted', or 'resigned'. Any other keywords such as 'good, OK, fair", etc. would have no effect.

So, John Doe's entry might look something like this before the code, with the red entries (color only for post clarity) being the latest update in a dynamic list. Other macros automatically sort and update the list after a new entry to give something like this.

ABCDE
1NameIDDepartmentReviewCurrent?
2Doe, John123VMOKYes
3Doe, John123VMPoorYes
4Doe, John
123VMDismissedNo
5Doe, John123OPOKYes
6Doe, John123OPPoorYes
7Doe, John123OPOKYes
8Doe, John
123OPfairYes

<tbody>
</tbody>

So, after running the code, it would retroactively update his 'current' status to 'No' for any department where certain reviews are entered such as "Dismissed", "Promoted", or "Left", but if the review were something else, it wouldn't change, and would only update entries for the same department where I enter the review status.

ABCDE
1NameIDDepartmentReviewCurrent?
2Doe, John123VMOKNo
3Doe, John123VMPoorNo
4Doe, John
123
VM
Dismissed
No
5Doe, John123OPOKyes
6Doe, John123OPPooryes
7Doe, John123OPOKyes
8Doe, John
123
OP
fair
Yes

<tbody>
</tbody>


This is very simplified snapshot, but if somebody could suggest the way through VBA to check and update, I can adapt and fit into my other code.

I worked out how to update 'current' column for the actual review line where I make the entry, I'm just not sure how to go back and update the others retroactively.
Code:
  [B]Dim wks As Worksheet[/B]
  [B]Dim lRow as Long[/B]
  [B]Dim nr As Long[/B]
  
  [B]Set wks = Sheet2[/B]
  [B]lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row[/B]
  
[B]For y = 7 To lRow[/B]
  [B]
Select Case wks.Cells(nr, 3).Value[/B]
[B]Case "VM", “OP”
[/B]
[B]Select Case wks.Cells(nr, 4).Value[/B]
[B]Case "Terminated", "Left", "Promoted", "Dismissed"[/B]
[B]wks.Cells(y, 5).Value = "No"
Case Else
[B] wks.Cells(y, 5).Value = "Yes"
[/B][/B]
[B]End Select[/B]
[B]End Select
[/B]
[B]nr = nr + 1
[/B]
[B]Next y[/B]
Any suggestions are appreciated.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Re: Help with VBA to update previous entered data

You could try this:

Sub UpdateCurrent()


Dim wks As Worksheet
Dim lrow As Long
Dim y As Long
Dim y2 As Long
Dim Name As String
Dim ID As String
Dim Dep As String


Set wks = ActiveWorkbook.Sheets("Sheet2")
lrow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row


Application.ScreenUpdating = False


For y = 1 To lrow
If wks.Range("D" & y) = "Dismissed" Or wks.Range("D" & y) = "Promoted" Or wks.Range("D" & y) = "Left" Or wks.Range("D" & y) = "Terminated" Then
Name = wks.Range("A" & y)
ID = wks.Range("B" & y)
Dep = wks.Range("C" & y)
For y2 = y To 1 Step -1
If wks.Range("A" & y2) = Name And wks.Range("B" & y2) = ID And wks.Range("C" & y2) = Dep Then
wks.Range("E" & y2) = "No"
End If
Next y2
Else
wks.Range("E" & y) = "Yes"
End If
Next y


Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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