简体   繁体   中英

Need to copy/paste row (x number of times) when cell value equals X

I'm trying to use Excel for a Jewelry Order Form.

In the order form (sheet1), a user may select from a cell that is formatted into a drop-down list, a number representing the number of stones in a piece of jewelry. For example, if there are 10 stones in a ring, then the user selects 10 from the drop-down list.

The details for each of the 10 stones needs to be captured in the order form (Sheet1). For example, each stone will have 4 data elements... a stone type, weight, color, cut... So I created the desired formatted row of data (in Sheet2) where each cell is a drop-down for a user to select from.

I want to create a control button to do the following actions:

  1. Delete rows 19:150 in Sheet1 This will clear out any prior stone details that may be displayed.

  2. Find the value in cell C13 in Sheet1 This value will be used to determine how many rows should be pasted/displayed

  3. Copy row, range A2: D2 in Sheet2 This is template row data where each cell in the row is its own drop-down list.

  4. Paste row in B19 in Sheet1 This is the template row pasted into an order form.

4a) Paste as many rows as the value in step (2) above.

For example, if the value in step 2 from above is "3", then the stone details row will need to be pasted 3 times in the order form.

The furthest I've been able to get is the creation of the control button, and the delete clause...

Private Sub CommandButton1_Click()
Sub deleteMultipleRows()
Rows("19:150").Delete
End Sub

For the delete statement, you should probably use Sheet1.Rows("19:150").Delete as that will ensure that excel knows which sheet to delete those rows from.


You can declare a variable and assign it a value like so:

Dim rowCount as Integer
rowCount = Sheet1.Range("C13")

If you try recording a macro for the copy and pasting, you should see some sample code.

Note:

If the result looks something like this:

Sheet1.Activate
Row(2).Select
Selection.Copy

You can combine such statements like so:

Sheet1.Row(2).Copy

Since most .Activate commands can be ignored, and .Select and Selection. commands can be combined (and should be in most situations). The Sheet1. added before the .Row(2) tells Excel to specifically use Row 2 from Sheet1. Without the qualifier, ie just Row(2) , Excel would use Row 2 from whichever sheet happens to be currently active.


You can then use a variable and the value from the sheet to loop through either pasting or copy/paste combo like so:

Dim counter as integer
For counter = 1 to Sheet1.Range("C13")
    'Add code for copy/paste here
Next counter

Or if you have the rowCount variable declared and assigned, you could use it here like this:

Dim counter as integer
For counter = 1 to rowCount
    'Add code for copy/paste here
Next counter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM