VBA to copy a cell defined value down a colum a cell defined amount of times

Eean

New Member
Joined
May 26, 2010
Messages
44
Hope the title's not too confusing.
I'm trying to write a Macro that will take the value in B6 and copy it to a new sheet in cell A1 and copy it down the column based on the value in cell C6. Then do the dame for B7 /C7 and so on. I also need to add a set value into Column S for each line
E.g.
Sheet 1:
B6 = 20
C6 =5
B7 = 10
C7 = 2

Sheet 2 Should look like this:
A1=20 S1=SC01
A2=20 S2=SC01
A3=20 S2=SC01
A4=20 S2=SC01
A5=20 S2=SC01
A6=10 S2=SC01
A7=10 S2=SC01

Can you help?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I am guessing someone will come along and give you something more elegant, but this should work for you...

Its just a simple loop:

First we are taking the value in B6/B7 and assigning them as values...

Code:
For Each rngCell In Range("B6:B12") ' Set your Range here
    lngTotal = rngCell.Offset(0, 1).Value

Next we just need to find the last row, I threw an IF in to account for A1 and finding the last row and overwritting the info...

Code:
        If Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row > 1 Then
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
        Else
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
        End If

Next is another loop. So we are taking each of the values and going to copy it X (B7) number of times and paste it onto Sheet2 in the A column, as well as add your constant value. Then we just increase the last row calculation by 1 to keep it moving down.

Code:
    For i = 1 To lngTotal
        rngCell.Copy Destination:=Sheets("Sheet2").Range("A" & lngLstRow)
        Sheets("Sheet2").Range("B" & lngLstRow).Value = "SC01" ' Set your Value here
        lngLstRow = lngLstRow + 1
    Next i

And that is about it...

Hope this helps, and please back up your excel workbook before running this macro!

Code:
Sub MovingOnDown()


Dim rngCell As Range
Dim lngTotal As Long
Dim lngLstRow As Long


For Each rngCell In Range("B6:B12") ' Set your Range here
    lngTotal = rngCell.Offset(0, 1).Value
        If Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row > 1 Then
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
        Else
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
        End If
    For i = 1 To lngTotal
        rngCell.Copy Destination:=Sheets("Sheet2").Range("A" & lngLstRow)
        Sheets("Sheet2").Range("B" & lngLstRow).Value = "SC01" ' Set your Value here
        lngLstRow = lngLstRow + 1
    Next i
Next


End Sub
 
Upvote 0
I am guessing someone will come along and give you something more elegant, but this should work for you...

Its just a simple loop:

First we are taking the value in B6/B7 and assigning them as values...

Code:
For Each rngCell In Range("B6:B12") ' Set your Range here
    lngTotal = rngCell.Offset(0, 1).Value

Next we just need to find the last row, I threw an IF in to account for A1 and finding the last row and overwritting the info...

Code:
        If Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row > 1 Then
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
        Else
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
        End If

Next is another loop. So we are taking each of the values and going to copy it X (B7) number of times and paste it onto Sheet2 in the A column, as well as add your constant value. Then we just increase the last row calculation by 1 to keep it moving down.

Code:
    For i = 1 To lngTotal
        rngCell.Copy Destination:=Sheets("Sheet2").Range("A" & lngLstRow)
        Sheets("Sheet2").Range("B" & lngLstRow).Value = "SC01" ' Set your Value here
        lngLstRow = lngLstRow + 1
    Next i

And that is about it...

Hope this helps, and please back up your excel workbook before running this macro!

Code:
Sub MovingOnDown()


Dim rngCell As Range
Dim lngTotal As Long
Dim lngLstRow As Long


For Each rngCell In Range("B6:B12") ' Set your Range here
    lngTotal = rngCell.Offset(0, 1).Value
        If Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row > 1 Then
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
        Else
            lngLstRow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
        End If
    For i = 1 To lngTotal
        rngCell.Copy Destination:=Sheets("Sheet2").Range("A" & lngLstRow)
        Sheets("Sheet2").Range("B" & lngLstRow).Value = "SC01" ' Set your Value here
        lngLstRow = lngLstRow + 1
    Next i
Next


End Sub

That's brilliant!
I love how you broke that down - I'm learning as I go so seeing it in stages is really helpful. Great answer!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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