简体   繁体   English

复制行并粘贴X次,具体取决于单元格值

[英]Copy Row and paste X amount of times dependent on cell value

I have a macro that needs to copy a row and paste it (values, not formula) x times dependent on value in column A (quantity). 我有一个宏需要复制一行并粘贴它(值,而不是公式)x次取决于列A(数量)中的值。 This needs to be repeated for an 'infinite' amount of rows. 这需要针对“无限”行重复。 Once this is done column A needs to be deleted. 完成此操作后,需要删除A列。

There are similar questions to this but none seem to work for me, my macro also needs to delete two sheets and save the file as a CSV with a given name. 有类似的问题,但似乎没有一个对我有用,我的宏也需要删除两张纸并将文件保存为具有给定名称的CSV。 I have the save and give name based on cell values just not the copy and paste. 我有基于单元格值的保存和给出名称,而不是复制和粘贴。


So I have only used VBA for about two weeks so i am struggling: I have tried this and that and i can get the odd code to work by itself but never with the rest of the code. 所以我只用了大约两个星期的VBA,所以我很挣扎:我已经尝试了这个,而且我可以让奇怪的代码单独工作,但从不与其余代码一起工作。


Private Sub CommandButton1_Click()

Dim Path As String
Dim Filename1 As String
Dim Filename2 As String
Path = "C:\Users\BergA2\Desktop\CSV Usage Upload\ "

Filename1 = Range("B1")
Filename2 = Range("D1")

I imagine the code would go in here: values for quantity are taken from sheet1 and moved into sheet3 using a simple formula 

Sheets("Sheet2").Delete
Sheets("Sheet1").Delete

ActiveWorkbook.SaveAs Filename:=Path & Filename1 & "-" & Filename2 & ".csv", FileFormat:=xlCSV


End Sub

Input ( more then just two columns) 输入(多于两列)

Quantity User   ... 
1        A      ...
3        B      ...
0        C      ...

Output: 输出:

User    ...
A       ...
B       ...
B       ...
B       ...

Supposing you have your headers in the first row. 假设您在第一行中有标题。

Public Function ReturnCol(ByVal searchString As String) As Integer
Dim rg As Range
Application.ScreenUpdating = False
ActiveWorkbook.ActiveSheet.Rows("1:1").Select
Set rg = Selection.Find(What:=searchString, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=True, SearchFormat:=False)

If rg Is Nothing Then
    ReturnCol = 0
    Exit Function
Else
    ReturnCol = rg.Column
End If

End Function
Sub collect()
Dim col_pos As Integer
Dim usedrows As Long

Dim i As Integer
Dim user As String
Dim quant As Integer

Dim counter As Long
' Assuming headers are in the first row

' Calculate until which point to loop
usedrows = Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row

' Find the positions of the headers (column numbers)
quant_col_pos = ReturnCol("Quantity")
user_col_pos = ReturnCol("User")


counter = 0
If quant_col_pos <> 0 Then
    For i = 2 To usedrows
        user = Cells(i, user_col_pos).Value
        quant = Cells(i, quant_col_pos).Value
        Sheets("Sheet2").Activate

        If i = 2 Then
            Cells(1, 1).Value = "User"
        End If
        ' starting filling values from the 2nd row but somewhere need to remember
        'which one was the last row used therefore counter is used

        For x = 2 + counter To 1 + quant + counter
            ' first column
            Cells(x, 1).Value = user
        Next x
        counter = quant + counter
        Sheets("Sheet1").Activate

    Next i

End If
End Sub

Here you go: 干得好:

Option Explicit

Sub copyBasedOnColumnA()
    Dim numberCopies As Long
    Dim currentRow As Long
    Dim j As Long

    Dim sht as Worksheet
    Set sht = Sheets("add sheet name")

    currentRow = 2

    Do While Not IsEmpty(sht.Cells(currentRow, 1))
        'determine how often to copy the current row
        numberCopies = sht.Cells(currentRow, 1)

        'copy that many times
        For j = 2 To numberCopies
            sht.Rows(currentRow).copy
            sht.Rows(currentRow).Insert Shift:=xlDown

            'increment the counter, otherwise you also copy all the rows just input
            currentRow = currentRow + 1
        Next j

        'move to next row
        currentRow = currentRow + 1
    Loop

    Application.CutCopyMode = False

    sht.Columns(1).Delete
End Sub

I'm assuming you don't want to copy the header. 我假设你不想复制标题。 You can define in which row you want to start by setting the currentRow variable. 您可以通过设置currentRow变量来定义要在哪一行开始。 Also I assumed that you don't have anything import stored in column A. 此外,我假设您没有在A列中存储任何导入内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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