macro to paste text for desired number of times in a cloumns

genetist

Board Regular
Joined
Mar 29, 2013
Messages
75
Office Version
  1. 2016
Platform
  1. Windows
Hi to all,
i have data like this
A B
MB1 MB1-1
MB2
MB3
MB4
MB5

i want to paste text for MB1 in column B like from MB1-1 to MB1-20 and MB 2-1 to MB 2-20. for MB2 in cloumn B like MB2 1-2 to MB2 1-20 and MB2 2-1 and MB2 2-20.

I am expecting like this
A B
MB1 MB1 1-1
.
.
.
MB1 1-20
MB1 2-1
.
.
.
MB1 2-20

MB2 MB2 1-1
.
.
.
MB2 1-20
MB2 2-1
.
.
.
MB2 2-20
and i would like to continue this for the rest of text in column A and i want this 20 in 1-1 or 2-20 should be changable because this 20 texts are not contstant numbers for always.
Here i tried with commond button simple and now i am in position to paste text in a given range but not the way i am expecting.
here is my code
Private Sub CommandButton1_click()
Dim Text As String
Text = "MB1 1-1"
Range ("B1:B20").value =Text
End Sub
I need to sovle this problem or needs editing to this VBA code to reach my objective. any help would be appreciated
Thanking you
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try:

Code:
Private Sub CommandButton1_Click()
    Dim Lastrow As Long
    Dim Cell As Range
    Dim i As Long
    Dim r As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("A1:A" & Lastrow)
        For i = 1 To 20
            r = r + 1
            Range("B" & r).Value = Cell.Value & " 1-" & Format(i, "00")
        Next i
        For i = 1 To 20
            r = r + 1
            Range("B" & r).Value = Cell.Value & " 2-" & Format(i, "00")
        Next i
    Next Cell
End Sub

Note that I padded then last number to 2 digits in case you want to sort.
 
Upvote 0
Thank you very much Dear Andrew poulsom for helping me in solving this problem and spending your valueable time for me.
with kind regards,
Genetist
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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