Using Split for all array elements without a loop

mse330

Well-known Member
Joined
Oct 18, 2007
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hello all,

Is it possible to split all elements of an array at once without a loop ?

I have the below table where I have different staff name, their supervisors at the shift & number of shifts. I have written the below code to get the count of each unique staff/supervisor/shift which works fine but I was wondering if I could split my Keys by ";" through the whole array without a loop. In my situation below, I used text to column & it did the job but I was wondering if I could do it through VBA without a loop perhaps using Application.Index but I am getting an error

VBA Code:
Sub test()

Dim a, Txt As String
a = Cells(1).CurrentRegion

With CreateObject("scripting.dictionary")
   For x = 2 To UBound(a)
      Txt = Join(Array(a(x, 1), a(x, 2), a(x, 3)), ";")
      If Not .exists(Txt) Then .Add Txt, a(x, 4) Else .Item(Txt) = .Item(Txt) + a(x, 4)
   Next
   Range("F2").Resize(.Count) = Application.Transpose(.keys)
   Range("F2").Resize(.Count).TextToColumns [F2], 1, , , , 1
   Range("i2").Resize(.Count) = Application.Transpose(.items)
End With

End Sub

I tried something like below instead of the Text to Columns but I am getting type mismatch error
VBA Code:
Range("F2").Resize(.Count, 3) = Application.Index(Application.Transpose(Split(.keys, ";")), 0, Array(1, 2, 3))

Book1
ABCDEFGHI
1Staff NameSupervisorShiftNo of shiftStaff NameSupervisorShiftNo of shift
2DanielDavidNight1DanielDavidNight1
3WillianLukeMorning6WillianLukeMorning12
4JacobLukeNight3JacobLukeNight7
5JamesDavidMorning6JamesDavidMorning6
6JacobLukeMorning3JacobLukeMorning3
7JamesDavidNight6JamesDavidNight6
8DanielJohnMorning4DanielJohnMorning4
9JamesLukeNight2JamesLukeNight6
10JacobLukeNight4JacobDavidMorning1
11WillianLukeMorning6JamesJohnNight2
12JacobDavidMorning1DanielMatthewNight6
13JamesJohnNight2
14DanielMatthewNight6
15JamesLukeNight4
Sheet1
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Split requires a string, so cannot work on an array.
 
Upvote 0
Thanks Fluff ... So I either have to loop in VBA or just go with Text To Columns like I did
 
Upvote 0
That's right, I'd tend to do with the text to columns approach that you used.
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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