Paste Data as per Count

6 views
Skip to first unread message

Vibhuti Shrotriya

unread,
Jul 5, 2016, 6:54:41 AM7/5/16
to Excel VBA Lab - An Excel VBA Macro help Group
Hi,

I have data in sheet 1 like below. I want to paste names in Sheet 2 as per the Count mentioned in Sheet 1.

Name        Count
Delhi               2
Goa             4
Bihar  1
Assam 7

Output Required in Sheet 2.

Output
Delhi
Delhi
Goa
Goa
Goa
Goa
Bihar
Assam
Assam
Assam
Assam
Assam
Assam
Assam

Kindly help.

Thanks
Vibhuti

Ashish Bhalara

unread,
Jul 5, 2016, 7:50:43 AM7/5/16
to Excel VBA Lab - An Excel VBA Macro help Group
Hi Vibhuti,

As per your query, press Alt+F11 from your sheet to open Macro, press Alt+I+M to insert new module in your sheet and Paste below code in module and close Macro by pressing Alt+Q, Press Alt+F8 to Run Macro. You will get exact require output in Sheet2. You can change Range or Sheet name in coding as per your requirement.

Option Explicit
Sub PasteDataasperCount()
Dim Rng As Range, R As Range
Dim I As Integer, J As Integer
Dim Ws1 As Worksheet, Ws2 As Worksheet
Set Ws1 = Worksheets("Sheet1")
Set Ws2 = Worksheets("Sheet2")
Set Rng = Ws1.Range("A2", Ws1.Range("A2").End(xlDown))
J = 2
For Each R In Rng
    For I = 1 To R.Offset(0, 1)
        Ws2.Range("A" & J).Value = R.Value
        J = J + 1
    Next I
Next R
End Sub

Regards
Ashish Bhalara

Vibhuti Shrotriya

unread,
Jul 8, 2016, 7:12:26 AM7/8/16
to Excel VBA Lab - An Excel VBA Macro help Group
Hi Ashish Bhalara,

It's working good. Thanks for the same.


Thanks & Regards
Vibhuti S.
Reply all
Reply to author
Forward
0 new messages