Make small variant array from big array based on Value

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
I have a variant array that I would would like to break up into individual arrays and I'm realizing I haven't figured out how to do that yet.
Mini-sheet example below.

Goal: In vba (not in sheet) array would like to either
A) Make smaller arrays for each department based on the department name from a bigger array
or
B) perhaps preferably, make arrays directly based on the department name and avoid the bigger array step


Mini-sheet
Book4
ABC
1DeptWk 1Wk 2
2Manufacturing17154
3Manufacturing18616
4Manufacturing33114
5Manufacturing8571
6Manufacturing112120
7Manufacturing13023
8Manufacturing185114
9Manufacturing2486
10Manufacturing139107
11Manufacturing8349
12Manufacturing3933
13Sales99146
14Sales11287
15Sales185195
16Sales186135
17Sales12986
18Sales5490
19Sales5221
20Sales188187
21Sales18823
22Marketing159192
23Marketing141177
24Marketing7080
25Marketing8072
26Marketing2079
27HR3585
28HR10894
29HR122128
30HR29132
Sheet1


My thinking up to a point...but then I realized I'm not sure what the best way to do this would.

VBA Code:
Sub Test()
Dim a, aManu, aSales, aMkt, aHR, aAll As Variant
Dim lr, lc As Long

Dim iSh As Worksheet
   
Set iSh = Worksheets("Sheet1")
   
    With iSh
        lr = .Cells(Rows.Count, 1).End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
       
        aAll = .Range(.Cells(2, 2), .Cells(lr, lc)).Value
       
            '### For Loops here to get individual arrays??
            '### how to size the small array before knowing how many department entries?
            '### aManu, aSales, aMkt, aHR
       
    End With
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
Sub Test()

    Dim addrAll As String, addrDept As String, Depts As Variant
    Dim lr, lc As Long, i As Long, ubDepts As Long
    
    Dim iSh As Worksheet
       
    Set iSh = Worksheets("Sheet1")
    
    Depts = Array("", "Manufacturing", "Sales", "Marketing", "HR")
    ubDepts = UBound(Depts)
    ReDim a(1 To ubDepts) As Variant
   
    With iSh
        lr = .Cells(Rows.Count, 1).End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        addrAll = .Range(.Cells(2, 2), .Cells(lr, lc)).Address
        addrDept = .Range(.Cells(2, 1), .Cells(lr, 1)).Address
    End With
    
    For i = 1 To ubDepts
        a(i) = Evaluate("=Filter(" & addrAll & ", " & addrDept & "= """ & Depts(i) & """)")
    Next
    ' a(1) <=> aManu, a(2) <=> aSales, a(3) <=> aMkt, a(4) <=> aHR

End Sub
 
Upvote 0
Solution
VBA Code:
Sub Test()

    Dim addrAll As String, addrDept As String, Depts As Variant
    Dim lr, lc As Long, i As Long, ubDepts As Long
   
    Dim iSh As Worksheet
      
    Set iSh = Worksheets("Sheet1")
   
    Depts = Array("", "Manufacturing", "Sales", "Marketing", "HR")
    ubDepts = UBound(Depts)
    ReDim a(1 To ubDepts) As Variant
  
    With iSh
        lr = .Cells(Rows.Count, 1).End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        addrAll = .Range(.Cells(2, 2), .Cells(lr, lc)).Address
        addrDept = .Range(.Cells(2, 1), .Cells(lr, 1)).Address
    End With
   
    For i = 1 To ubDepts
        a(i) = Evaluate("=Filter(" & addrAll & ", " & addrDept & "= """ & Depts(i) & """)")
    Next
    ' a(1) <=> aManu, a(2) <=> aSales, a(3) <=> aMkt, a(4) <=> aHR

End Sub
Thank you JGordon11 this works nicely.
I'm not that knowledgeable on Evaluate, but reading on the internet, I think it makes sense the way that you've constructed the code.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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