VBA If cell contains a number X. Copy Row Range and paste X times

menor59

Well-known Member
Joined
Oct 3, 2008
Messages
574
Office Version
  1. 2021
Platform
  1. Windows
hello all.

I think this is a Simple Request...

On a sheet i have in C10 a Value default 0

Lets say i put 6 there
On C10's Value of 6 copy F3:Y3 below it 5 times...

If C10 is lets say 10, On C10's Value of 10 and copy 9 times below F3:Y3

IF C10 is 0 Clear Everything below F3:Y3

(F3:Y3) is the template row Basically...there is some conditional formating, Formulas, and Data Validation there also that needs to populate also with the value in C10

Thoughts or comments?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Assuming that we can tell how many rows of data in columns F:Y from column F then you could try this Worksheet_Change event code. Post back if you need instructions to implement it.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim myR As Range
  
  Set myR = Range("C10")
  If Not Intersect(Target, myR) Is Nothing Then
    Application.EnableEvents = False
    Range("F3:Y" & Range("F" & Rows.Count).End(xlUp).Row).Offset(1).Clear
    On Error Resume Next
    If myR.Value >= 1 Then Range("F3:Y3").Copy Destination:=Range("F3:Y3").Resize(myR.Value)
    On Error GoTo 0
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
OK, That Cleared all of F3:Y3 when i put a number in C10
 
Upvote 0
Fixed it...Thank you!!!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim myR As Range
  
  Set myR = Range("C10")
  If Not Intersect(Target, myR) Is Nothing Then
    Application.EnableEvents = False
    Range("F4:Y" & Range("F" & Rows.Count).End(xlUp).Row).Offset(2).Clear
    On Error Resume Next
    If myR.Value >= 1 Then Range("F3:Y3").Copy Destination:=Range("F3:Y3").Resize(myR.Value)
    On Error GoTo 0
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
I dont know why it fixed it thou...I changed...

VBA Code:
Range("F4:Y" & Range("F" & Rows.Count).End(xlUp).Row).Offset(2).Clear
 
Upvote 0
I dont know why it fixed it thou...
What was in F3 before you ran the code?

I had assumed that there was some value or formula in F3 before the code was run.

I don't know if it matters, but with your code if you enter, say 8, then you will have something in rows 3:10. If you then enter 2 in C10 you will have data in rows 3:5, not 3:4

Should any old data be cleared from rows 4, 5, 6, ... before row 3 is copied again when a new number is entered in C10?
 
Upvote 0
Hello Peter and Folks...The above Code works Perfectly...but...theres a but...

VBA Code:
 Dim myR As Range
  
  Set myR = Range("C10")
  If Not Intersect(Target, myR) Is Nothing Then
    Application.EnableEvents = False
    Range("F4:Y4" & Range("F" & Rows.Count).End(xlUp).Row).Offset(0).Clear
    On Error Resume Next
    If myR.Value >= 1 Then Range("F3:Y3").Copy Destination:=Range("F3:Y3").Resize(myR.Value)
    On Error GoTo 0
    Application.EnableEvents = True
  End If

say the Value in C10 is 3...

And It copies whats in F3:Y3 (Blank) Correctly... Into F4:Y4 3 times Blanks....

Now...Lets say i put data Into those cells...

I come back later and i need to have C10 a different number than 3....Lets say 10....

Its taking the data thats in F3:Y3 and copying it down into F4:Y4 10 times...

what i want to do is...

If C10 was Changed from 3...to 10....

Give me empty Cells (without text from F3:Y3) 7 times below the the last cell range that containes data...Does that make sense?
 
Upvote 0
From this:
3.jpg





To this:

10.jpg


Keeping the Data Above...But taking the Formating and pulldowns from The First Range..
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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