VBA copy value multiple times based upon number of rows and loop

ruannpreger

New Member
Joined
Sep 25, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like to make the VBA below more efficient.
In my simplified example i have 5 rows populated.
Column D has 5 values; lets say
D1 = AAA
D2 = BAH
D3 = KLM
D4 = TRE
D5 = BHG

so vba counts the number of rows ( which is 5) and then copy value in D1 ( which is AAA) 5 times in col H, next it copies values D2 ( BAH) 4 times to col i.
by changing the vba manually, it works however in real i have over 100 values.

Now i would like to have this / everything only in col H underneath eachother and preferably more efficient.

VBA Code:
Sub test()
Dim MyCount As Long
MyCount = Range("A" & Rows.Count).End(xlUp).Row
Dim MyVal As String
If MyCount > 0 Then MyVal = Range("d1").Text
Range("h1:h" & MyCount) = MyVal

MyCount2 = Range("A" & Rows.Count).End(xlUp).Row - 1
Dim MyVal2 As String
If MyCount2 > 0 Then MyVal2 = Range("d2").Text
Range("i1:i" & MyCount2) = MyVal2

MyCount3 = Range("A" & Rows.Count).End(xlUp).Row - 2
Dim MyVal3 As String
If MyCount3 > 0 Then MyVal3 = Range("d3").Text
Range("j1:j" & MyCount3) = MyVal3
End sub

it should result in :
Col H

AAA
AAA
AAA
AAA
AAA
BAH
BAH
BAH
BAH
KLM
KLM
KLM

thanks.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about
VBA Code:
Sub ruannpreger()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, rr As Long, nr As Long
   
   Ary = Range("D1", Range("D" & Rows.Count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary) ^ 2, 1 To 1)
   For r = 1 To UBound(Ary)
      For rr = r To UBound(Ary)
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 1)
      Next rr
   Next r
   Range("H1").Resize(nr).Value = Nary
End Sub
 
Upvote 0
great! while testing it I noticed that the row count should be deducted with 1. Meaning that when there are 5 rows, it should multiply with 4 (instead of 5) and then with 3 and then with 2. Where do i put -1 in the code?
 
Upvote 0
If you do that what happens to the last value in the column?
 
Upvote 0
if you copy the 1st value 4 times, the 2nd 3times, the 3rd 2times, the 4th once. Where does that leave the 5th value?
 
Upvote 0
In that case try
VBA Code:
Sub ruannpreger()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, rr As Long, nr As Long
   
   Ary = Range("D1", Range("D" & Rows.Count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary) ^ 2, 1 To 1)
   For r = 1 To UBound(Ary)
      For rr = r To UBound(Ary) - 1
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 1)
      Next rr
   Next r
   Range("H1").Resize(nr).Value = Nary
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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