Vba 如何在粘贴数组之前插入行

Vba 如何在粘贴数组之前插入行,vba,excel,Vba,Excel,我目前有一个数组,我使用宏填充并粘贴到一个名为“T1”的工作表中。我的当前宏使用rowcount函数确定使用的行,并从下一个可用行粘贴数组 我遇到的问题是,当我多次粘贴此数组时,需要将这些数组隔开一行,以便区分不同的提交。这就是我目前所拥有的,我希望有人能帮助我: Sub CopyData() Dim Truearray() As String Dim cell As Excel.Range Dim RowCount1 As Integer Dim i

我目前有一个数组,我使用宏填充并粘贴到一个名为“T1”的工作表中。我的当前宏使用
rowcount
函数确定使用的行,并从下一个可用行粘贴数组

我遇到的问题是,当我多次粘贴此数组时,需要将这些数组隔开一行,以便区分不同的提交。这就是我目前所拥有的,我希望有人能帮助我:

Sub CopyData()

     Dim Truearray() As String
     Dim cell As Excel.Range
     Dim RowCount1 As Integer
     Dim i As Integer
     Dim ii As Integer
     Dim col As Range
     Dim col2 As Range
     i = 0
     ii = 2

     RowCount1 = DHRSheet.UsedRange.Rows.Count
     Set col = DHRSheet.Range("I1:I" & RowCount1)

     For Each cell In col

         If cell.Value = "True" Then

             Dim ValueCell As Range
             Set ValueCell = Cells(cell.Row, 3)
             ReDim Preserve Truearray(i)
             Truearray(i) = ValueCell.Value

             Dim siblingCell As Range
             Set siblingCell = Cells(cell.Row, 2)
             Dim Siblingarray() As String

             ReDim Preserve Siblingarray(i)
             Siblingarray(i) = DHRSheet.Name & "$" & siblingCell.Value

             i = i + 1

         End If

     Next

     Dim RowCount2 As Integer

     RowCount2 = DataSheet.UsedRange.Rows.Count + 1

     For ii = 2 To UBound(Truearray)
         DataSheet.Cells(RowCount2 + ii, 2).Value = Truearray(ii)
     Next

     For ii = 2 To UBound(Siblingarray)
         DataSheet.Cells(RowCount2 + ii, 1).Value = Siblingarray(ii)
     Next

     DataSheet.Columns("A:B").AutoFit

     MsgBox ("Data entered has been successfully validated & logged")

 End Sub 

如果从底部单元格偏移两行,将留下一行空白分隔符。您还应该考虑将整个数组填充为基1,并将其一次写入数据表。

Sub CopyData2()

    Dim rCell As Range
    Dim aTrues() As Variant
    Dim rRng As Range
    Dim lCnt As Long

    'Define the range to search
    With DHRSheet
        Set rRng = .Range(.Cells(1, 9), .Cells(.Rows.Count, 9).End(xlUp))
    End With

    'resize array to hold all the 'trues'
    ReDim aTrues(1 To Application.WorksheetFunction.CountIf(rRng, "True"), 1 To 2)

    For Each rCell In rRng.Cells
        If rCell.Value = "True" Then
            lCnt = lCnt + 1
            'store the string from column 2
            aTrues(lCnt, 1) = DHRSheet.Name & "$" & rCell.Offset(0, -7).Value
            'store the value from column 3
            aTrues(lCnt, 2) = rCell.Offset(0, -6).Value
        End If
    Next rCell

    'offset 2 from the bottom row to leave a row of separation
    With DataSheet.Cells(DataSheet.Rows.Count, 1).End(xlUp).Offset(2, 0)
        'write the stored information at one time
        .Resize(UBound(aTrues, 1), UBound(aTrues, 2)).Value = aTrues
    End With

End Sub

不要使用USEDRANGE。使用此方法查找最后一行,然后只需向其中添加1。我真的不理解这个问题,因为代码似乎已经完成了它应该做的事情。当执行以下行:Count2=DataSheet.UsedRange.Rows.Count+1时,在开始填写TrueArray和SiblingArray之前创建一个空行。如果要多次粘贴数组(例如在循环中),可以在每次粘贴数组后重新定义usedRange。这难道不能解决你的问题吗?-或者你可以重新定义最后一个单元格。像往常一样,很好的一个-好的评论,这将有助于操作