VBA Code for copying rows based on cell value

anonymous1234

New Member
Joined
Nov 20, 2023
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I am new to VBA coding.

Please can someone help me on how to code for copying rows based on cell value:

For example,

If cell value E14 = 3


E18: Happy
E19: Birthday
E20: Mate!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,

I am new to VBA coding.

Please can someone help me on how to code for copying rows based on cell value:

For example,

If cell value E14 = 3


E18: Happy
E19: Birthday
E20: Mate!
accidentally uploaded without finishing:

then based on cell value E14 = 3, the data will be displayed:

Happy
Birthday
Mate!

Happy
Birthday
Mate!

Happy
Birthday
Mate!
 
Upvote 0
Here is a UDF that will do that, not sure if it will work for your needs/ excel version:
VBA Code:
Function rpt(r As Long, rng As Range) As Variant
    Dim x As Long, y As Long, z As Long
    Dim var As Variant
    Dim oVar() As Variant
    
    var = rng.Value
    ReDim oVar((UBound(var) + 1) * r - 2)
    
    For y = 1 To r
        For x = 1 To UBound(var)
            oVar(z) = var(x, 1): z = z + 1
        Next x
        If z <= UBound(oVar) Then
            oVar(z) = "": z = z + 1
        End If
    Next y
    rpt = Application.Transpose(oVar)
End Function

Used as below:
Book1
EFGH
143Happy
15Birthday
16Mate!
17
18HappyHappy
19BirthdayBirthday
20Mate!Mate!
21
22Happy
23Birthday
24Mate!
Sheet1
Cell Formulas
RangeFormula
H14:H24H14=rpt(E14,E18:E20)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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