Multi Dim array printing specific value to range depending on value in Col A

RumSwift

New Member
Joined
Feb 21, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hiya,

I'm building a roster for my business and to save time, I'm attempting to automate it. this is due to multiple rosters rebuilds taking place throughout the rostered period & I would rather not copy and paste names hundreds of times.

The Roster on Sheet "Roster" looks like this:
Line/Rotation NumberDateEmployee
101/03/2023
102/03/2023
103/03/2023
204/03/2023
205/03/2023
206/03/2023
307/03/2023
308/03/2023
309/03/2023

I have a multi dim array that fills from a table of active staff. Each staff member has their own unique "line" attached to them. So for example, the array looks like this:

Item One
Joe Bloggs
1

Item Two
Anita Formula
2

Item Three
Chris Swanson
3

The Array has the employee's name in position 1 and their line/rotation number is position 2.

the roster is not in a table format, it's just a range. This is due to it essentially being a template to be uploaded to a different system and requires it to just be a regular range & won't work if it's in a table. Meaning I have to find the range by getting last row. so depending on the length of the current roster, it could be A1:D400 or A1:D100.

I essentially need something that loops through the range and fills the final col (this case will be D) with the emplyee name where the line number in col A is the number in position 2 of the array. So, the above would come out looking like this:

Line NumberDateEmployee
101/03/2023Joe Bloggs
102/03/2023Joe Bloggs
103/03/2023Joe Bloggs
204/03/2023Anita Formula
205/03/2023Anita Formula
206/03/2023Anita Formula
307/03/2023Chris Swanson
308/03/2023Chris Swanson
309/03/2023Chris Swanson

I've shortened the example above but the array I currently have has 32 staff in (so 32-line numbers). For some extra info, this can change each time I have to roll a new roster through. So I've already set everything up so far to be dynamically sized. f that makes a difference.

Any help is massively appreciated.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I have assumed the Employee Listing actually looks like the below.
Also I have used a sheet name of Employees for that sheet.
On that basis try the code below.

20230221 VBA Dict Assign Employees to Roster RumSwift.xlsm
A
1Joe Bloggs
21
3Anita Formula
42
5Chris Swanson
63
Employees


VBA Code:
Sub AllocateNamesToRoster()

    Dim shtRoster As Worksheet, shtEmpLst As Worksheet
    Dim rstrRng As Range, empRng As Range
    Dim rstrLastRow As Long, empLastRow As Long
    Dim rstrArr As Variant, empArr As Variant
    Dim i As Long
    
    Set shtRoster = Worksheets("Roster")
    Set shtEmpLst = Worksheets("Employees")         '<-- Change to actual sheet name
    
    With shtRoster
        rstrLastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set rstrRng = .Range(.Cells(2, "A"), .Cells(rstrLastRow, "C"))
        rstrArr = rstrRng
    End With

    With shtEmpLst
        empLastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set empRng = .Range(.Cells(1, "A"), .Cells(empLastRow, "C"))
        empArr = empRng
    End With
    
    Dim dictEmp As Object, dictKey As String
    
    ' Load employee list range into Dictionary
    Set dictEmp = CreateObject("Scripting.dictionary")
    For i = 1 To UBound(empArr) Step 2
        dictKey = empArr(i + 1, 1)              ' Rotation no on 2nd Row
        If Not dictEmp.exists(dictKey) Then
            dictEmp(dictKey) = empArr(i, 1)
        End If
    Next i

    ' Load names into Roster Array
    For i = 1 To UBound(rstrArr)
        dictKey = rstrArr(i, 1)
        If dictEmp.exists(dictKey) Then
            rstrArr(i, 3) = dictEmp(dictKey)
        End If
    Next i
    
    ' Write back Employee Names
    rstrRng.Columns(3).Value = Application.Index(rstrArr, 0, 3)

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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