Replicate names based on count

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a table of names with their count in column B.

How can I get the modified results in column C whether formula or VBA?

bibles.xlsm
ABC
1BookCountRevised
2John7John
3Paul4John
4Adam6John
5Susan5John
6John
7John
8John
9Paul
10Paul
11Paul
12Paul
13Adam
14Adam
15Adam
16Adam
17Adam
18Adam
19Susan
20Susan
21Susan
22Susan
23Susan
Sheet1
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try:

VBA Code:
Sub addnames()

Dim rownum As Long
Dim rownum2 As Long
Dim counter As Long

rownum = 2
rownum2 = 2

Do Until Cells(rownum, 1) = ""
counter = 1
    Do Until counter = Cells(rownum, 2) + 1
    Cells(rownum2, 3) = Cells(rownum, 1)
    counter = counter + 1
    rownum2 = rownum2 + 1
    Loop
rownum = rownum + 1
Loop

End Sub
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Lists = Table.AddColumn(Source, "Custom", each {1..[Count]}),
    Expand = Table.ExpandListColumn(Lists, "Custom"),
    TSC = Table.SelectColumns(Expand,{"Book"}),
    Ren = Table.RenameColumns(TSC,{{"Book", "Revised"}})
in
    Ren
BookCountRevised
John7John
Paul4John
Adam6John
Susan5John
John
John
John
Paul
Paul
Paul
Paul
Adam
Adam
Adam
Adam
Adam
Adam
Susan
Susan
Susan
Susan
Susan
 
Upvote 0
Solution
  • Copy the code into a standard module.
  • Adjust the constants to fit your needs.
VBA Code:
Option Explicit

Sub writeInstances()
    
    Const FirstRow As Long = 2
    Const srcValue As Variant = "A"
    Const srcCount As Variant = "B"
    Const tgtValue As Variant = "C"
    
    ' Define Value Processing Range.
    Dim rng As Range
    Set rng = Range(Cells(FirstRow, srcValue), _
                    Cells(Rows.Count, srcValue))
    ' Define Value Last Cell Range.
    Dim cel As Range
    Set cel = rng.Find(What:="*", _
                       LookIn:=xlFormulas, _
                       SearchDirection:=xlPrevious)
    ' Validate Value Last Cell Range.
    If cel Is Nothing Then
        GoTo ProcExit
    End If
    ' Define Value Column Range.
    Set rng = Range(rng.Cells(1), cel)
    
    ' Write values from Value and Count Column Ranges to arrays of Source Array.
    Dim OneCell As Variant
    ReDim OneCell(1 To 1, 1 To 1)
    Dim Source As Variant
    ReDim Source(1 To 2)
    If rng.Rows.Count > 1 Then
        Source(1) = rng.Value
        Source(2) = rng.Offset(, Columns(srcCount).Column _
                               - Columns(srcValue).Column).Value
    Else
        Source(1) = OneCell
        Source(1)(1, 1) = rng.Value
        Source(2)(1, 1) = rng.Offset(, Columns(srcCount).Column _
                                     - Columns(srcValue).Column).Value
    End If
        
    ' Write values from Source Array to Target Array.
    Dim Target As Variant
    Dim CurrentValue As Variant
    Dim CountValue As Variant
    ReDim Target(1 To WorksheetFunction.Sum(Source(2)), 1 To 1)
    Dim i As Long
    Dim j As Long
    Dim k As Long
    For i = 1 To UBound(Source(1))
        CountValue = Source(2)(i, 1)
        If VarType(CountValue) = vbDouble Then
            CountValue = CLng(CountValue)
            If CountValue > 0 Then
                CurrentValue = Source(1)(i, 1)
                For j = 1 To CountValue
                    k = k + 1
                    Target(k, 1) = CurrentValue
                Next j
            End If
        End If
    Next i
    If k = 0 Then
        GoTo ProcExit
    End If
    
    ' Copy values from Target Array to Target Column Range.
    Set cel = rng.Cells(1).Offset(, Columns(tgtValue).Column _
                                  - Columns(srcValue).Column)
    cel.Resize(Rows.Count - cel.Row + 1).Clear
    cel.Resize(k).Value = Target
    
ProcExit:
End Sub
 
Upvote 0
Thank you all. @sandy, I can't figure out how you make the custom column for the second step Lists? Is there some extra information you can provide here or a website to view?
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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