How to populate a value based on the number of instance in the other table

mart_mrexcel

Active Member
Joined
Aug 23, 2008
Messages
310
Office Version
  1. 365
Platform
  1. Windows
I have a "Company" table and Grade Table, how can I dynamicaly populate the Co. Name based on the number of instances in the grade table? See Example in column E & F.

ABCDEF
1Expected Output
2Co. NameGradeCo. NameGrade
3Company1Gr1Company1Gr1
4Company2Gr2Company1Gr2
Company2Gr1
Company2Gr2
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello,

Here is a VBA solution:
VBA Code:
Sub test()
  Dim companies As Variant, grades As Variant, results As Variant, i As Long, j As Long
  With Application
  companies = .Transpose(Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)) 'actual data
  grades = .Transpose(Range("D3:D" & Cells(Rows.Count, "D").End(xlUp).Row)) 'actual data
 
  ReDim results(1 To 2, 1 To 2)
  results(1, 1) = Range("B2").Value 'header info
  results(2, 1) = Range("D2").Value 'header info
 
  For i = 1 To UBound(companies)
    For j = 1 To UBound(grades)
      results(1, UBound(results, 2)) = companies(i)
      results(2, UBound(results, 2)) = grades(j)
      ReDim Preserve results(1 To 2, 1 To UBound(results, 2) + 1)
    Next
  Next
  ReDim Preserve results(1 To 2, 1 To UBound(results, 2) - 1)
 
  Range("E3").Resize(UBound(results, 2), UBound(results, 1)).Value = .Transpose(results)
  End With
End Sub
 
Upvote 1
With a formula
Fluff.xlsm
ABCDE
1Co. NameGradeCo. NameGrade
2Company1Gr1Company1Gr1
3Company2Gr2Company1Gr2
4Company3Company2Gr1
5Company2Gr2
6Company3Gr1
7Company3Gr2
8
Sheet6
Cell Formulas
RangeFormula
D2:E7D2=LET(c,FILTER(A2:A100,A2:A100<>""),g,FILTER(C2:C100,C2:C100<>""),HSTACK(TOCOL(IF(SEQUENCE(,ROWS(g)),c)),TOCOL(IF(SEQUENCE(,ROWS(c)),g),,1)))
Dynamic array formulas.
 
Upvote 1
Solution
Hello,

Here is a VBA solution:
VBA Code:
Sub test()
  Dim companies As Variant, grades As Variant, results As Variant, i As Long, j As Long
  With Application
  companies = .Transpose(Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)) 'actual data
  grades = .Transpose(Range("D3:D" & Cells(Rows.Count, "D").End(xlUp).Row)) 'actual data
 
  ReDim results(1 To 2, 1 To 2)
  results(1, 1) = Range("B2").Value 'header info
  results(2, 1) = Range("D2").Value 'header info
 
  For i = 1 To UBound(companies)
    For j = 1 To UBound(grades)
      results(1, UBound(results, 2)) = companies(i)
      results(2, UBound(results, 2)) = grades(j)
      ReDim Preserve results(1 To 2, 1 To UBound(results, 2) + 1)
    Next
  Next
  ReDim Preserve results(1 To 2, 1 To UBound(results, 2) - 1)
 
  Range("E3").Resize(UBound(results, 2), UBound(results, 1)).Value = .Transpose(results)
  End With
End Sub
Thank you
 
Upvote 0
With a formula
Fluff.xlsm
ABCDE
1Co. NameGradeCo. NameGrade
2Company1Gr1Company1Gr1
3Company2Gr2Company1Gr2
4Company3Company2Gr1
5Company2Gr2
6Company3Gr1
7Company3Gr2
8
Sheet6
Cell Formulas
RangeFormula
D2:E7D2=LET(c,FILTER(A2:A100,A2:A100<>""),g,FILTER(C2:C100,C2:C100<>""),HSTACK(TOCOL(IF(SEQUENCE(,ROWS(g)),c)),TOCOL(IF(SEQUENCE(,ROWS(c)),g),,1)))
Dynamic array formulas.
Thank you fluff. thats work perfect in terms of formula
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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