I am just learning the abc on vba
I have been working with this code, to auto-populate a list based on 2 others. I have a client list, each client has a consultant, I needed to populate the team each client belonged to, and managed to do this using VLookUp application to relate consultant-manager-team.
So when we have a new consultant on board and has not been added to the "consultant-manager" list, obviously, I get an error. I tried fixing it with "On Error Resume Next", but it just populated the cell with the error using the last valid team name and went on. So I inserted a handler (see code below), for it to leave the cell blank and move on, it works fine when there is one error, but when there were 2 continuous errors:
- The macro would leave the first one in blank but populated the second cell with an error using the last valid team name.
- It keeps populating even after the list ended, ignoring the condition I gave it for the loop (NumRows in code below).
Could any one of you gods guide me on how to redact the error handling process?
Sub populateteam()
Dim wbFollowUp As Workbook
Dim wbList As Workbook
Set wbFollowUp = ThisWorkbook
Set wbList = Workbooks.Open("C:\<folders>\CSTeams.xlsx")
Dim wsAkasaka As Worksheet
Dim wsList As Worksheet
Set wsAkasaka = wbFollowUp.Worksheets("Akasaka")
Set wsList = wbList.Worksheets("All Japan")
wbFollowUp.Activate
Dim consultant As String
Dim manager As String
Dim team As String
Dim x As Integer
Application.ScreenUpdating = False
NumRows = Range("b2", Range("b2").End(xlDown)).Rows.Count
For x = 1 To NumRows
consultant = wsAkasaka.Range("b" & (ActiveCell.Row)).Value
On Error GoTo handler:
manager = Application.VLookup(consultant, wsList.Range("a13:c250"), 3, False)
team = Application.VLookup(manager, wsList.Range("e2:F11"), 2, False)
'The name of the manager in the consultant list and in the team list should be exactly the same,
'including spaces before and after
If IsEmpty(ActiveCell.Value) Then
ActiveCell.Value = team
ActiveCell.Offset(1, 0).Select
End If
Next
Application.ScreenUpdating = True
handler:
ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Resume Next
End Sub