简体   繁体   English

需要Excel VBA代码粘贴到列A中的下一个空白单元格中

[英]Need Excel VBA Code to paste into next blank cell in Column A

The code that I have currently pastes the data in Column A of worksheet "Projects" into the next blank row on worksheet "Assignment". 我当前已将代码粘贴到工作表“项目”的下一个空白行中的工作表“项目”的列A中。 I would like it to paste into the first blank cell on worksheet "Assignment" instead. 我希望它粘贴到工作表“ Assignment”的第一个空白单元格中。

Option Explicit

Sub CopyPasteX()
    'Declare variables
    Dim projName As String
    Dim projCount As Integer, lRow As Integer, lRow2 As Integer, i As Integer, j As Integer

    'Find last row
    lRow = Sheets("Projects").Range("A" & Rows.Count).End(xlUp).Row

    'Begin loop - CHANGE BELOW FROM 2 TO 1 IF SPREADSHEET DOES NOT INCLUDE HEADDERS
    For i = 2 To lRow

        'Set project names and the project count
        projName = Sheets("Projects").Range("A" & i)
        projCount = Sheets("Projects").Range("B" & i)

        'Second loop for pasting in project
        For j = 1 To projCount

            'Find last row on sheet 2
            lRow2 = Sheets("Assignment").Range("A" & Rows.Count).End(xlUp).Row


            'Paste in the project name on sheet2
            Sheets("Assignment").Range("A" & lRow2 + 1).Value = projName

        'Loop to continue copying based on the project count
        Next j

    'Loop to next project
    Next i

End Sub

Edit : I amended the lRow2 definition and refactored the whole code to take advantage of With ... End With sintax and reference proper sheet 编辑 :我修改了lRow2的定义,并重构整个代码以利用With ... End With sintax和参考正确的工作表

Sub CopyPasteX()
    'Declare variables

    Dim lRow2 As Integer, j As Long

    Dim cell As Range
    With Sheets("Projects") 'reference wanted sheet
        'loop through referenced sheet column A cells from row 1 down to last not empty one
        'Begin loop - CHANGE BELOW FROM "A2" TO "A1" IF SPREADSHEET DOES NOT INCLUDE HEADDERS
        For Each cell In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))

            'Second loop for pasting in project, taking current cell adjacent one as the ending value
            For j = 1 To cell.Offset(, 1)

                'Find firts empty cell on sheet Assignment
                With Sheets("Assignment")
                    Select Case True
                        Case IsEmpty(.Range("A1"))
                            lRow2 = 0
                        Case WorksheetFunction.CountA(.Range("A1", .Cells(.Rows.Count, "A").End(xlUp))) = 1
                            lRow2 = 1
                        Case Else
                            lRow2 = .Range("A1").End(xlDown).row
                    End Select
                    .Range("A" & lRow2 + 1).Value = cell.Value 'Paste current cell value (i.e. project name) in referenced sheet column A at row lRow
                End With

            'Loop to continue copying based on the project count
            Next

        'Loop to next project
        Next
    End With

End Sub
            'Find last row on sheet 2
           lRow2 = Sheets("Assignment").[A1].End(xlDown).Row

I found that this works exactly how I need it to. 我发现这完全符合我的需要。

Edit: This does not work as noted in the reply. 编辑:这不起作用,如答复中所述。

No need for inner loop. 不需要内循环。 Try this code 试试这个代码

Sub CopyPasteX()
Dim projName    As String
Dim projCount   As Integer
Dim lRow        As Integer
Dim lRow2       As Integer
Dim i           As Integer

lRow = Sheets("Projects").Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lRow
    projName = Sheets("Projects").Range("A" & i)
    projCount = Sheets("Projects").Range("B" & i)

    lRow2 = Sheets("Assignment").Range("A" & Rows.Count).End(xlUp).Row
    lRow2 = IIf(lRow2 = 1, 1, lRow2 + 1)

    Sheets("Assignment").Range("A" & lRow2).Resize(projCount).Value = projName
Next i
End Sub

Another code (using arrays) 另一个代码(使用数组)

Sub Test()
Dim arr         As Variant
Dim temp()      As String
Dim i           As Long
Dim j           As Long
Dim k           As Long

arr = Sheets("Projects").Range("A2:B" & Sheets("Projects").Cells(Rows.Count, 1).End(xlUp).Row).Value

j = 1: k = 1

For i = 1 To UBound(arr, 1)
    k = k + arr(i, 2)
    ReDim Preserve temp(1 To k)
    For j = j To k
        temp(j) = arr(i, 1)
    Next j
    j = k
Next i

With Sheets("Assignment").Range("A1")
    .Resize(k - 1, 1).Value = Application.Transpose(temp)
End With
End Sub

暂无
暂无

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

相关问题 excel vba:如何将组合框值粘贴到下一个列单元格 - excel vba: How to paste the combobox value to the next column cell Excel 公式(无 VBA)- Select 从指定单元格到列中的下一个空白 - Excel formula (no VBA) - Select from specified cell to next blank in column Excel:选择性粘贴>使用VBA添加“空白”单元格 - Excel: Paste Special > Adding a “blank” cell with VBA VBA Excel粘贴到下一行,如果复制了空白单元格,则无信息可转移 - VBA Excel Paste on next row without information to shift if there is a blank cell copied Excel VBA - 循环到下一个可用的空白单元格 - Excel VBA - Loop to next available blank cell Excel VBA:代码应检查单元格是否符合条件,然后粘贴到下一个活动单元格,但不起作用 - Excel VBA: Code should check whether cell falls into criteria then paste to next active cell but not working Excel vba 复制粘贴到下一个空列 - Excel vba copy paste to next empty column Excel VBA:如何将文本添加到特定列中的空白单元格,然后循环到下一个空白单元格并添加文本? - Excel VBA: How do I add text to a blank cell in a specific column then loop to the next blank cell and add text? Excel VBA-基于列中下一个非空白单元格的搜索/选择/删除范围 - Excel VBA - Search/Select/Delete Range Based on Next Non-Blank Cell in Column VBA - 复制单元格并粘贴列中的空白行 - VBA - Copy Cell and Paste Down Blank Rows in Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM