简体   繁体   中英

Copy and paste down a column based on the value of another cell

I am trying to copy and paste a number down a column ( that number will vary throughout column "A") and I need the number of Instances that a cell is filled down the column to equal the value of another cell in the data set. That value will vary throughout as well. Any help would be appreciated. if the value was 3751022 and the value in the other column was 11

    The image shows the set Im working on. It need to copy down 3751022 down column A 1-11 times because the first entry will already be done. once the 10 are pasted then it needs to wait for the next entry to past another value down x amount of time                                                

I don't completely understand where you will get the value you want repeated but you could build a function like

Public Function Copy2()
Dim r1, r2, r3 As Range
Dim ct


Set r1 = Range("a1") 'set pointer
Set r2 = Range("b1") 'set pointer
Set r3 = Range("c1") 'set pointer

Do ' outer loop going through each number in column A until it comes up to a blank cell
ct = 0

  Do ' Inner loop printing the # of times a number is requested from coresponding colum B data
   ct = ct + 1

' prints column 3 info
    r3.Value = r1.Value
    Set r3 = r3.Offset(1, 0)

  Loop Until ct = r2.Value

Set r2 = r2.Offset(1, 0)
Set r1 = r1.Offset(1, 0)

Loop Until r1.Value = ""

End Function

Then you can call it from a button where you would assign its code like

Private Sub CommandButton2_Click()
Copy2
End Sub

this will go down column A and starting populating a new column C with the number of occurrences found in its respective column B

example result

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