【问题标题】:Copy values of Sheet1 to End of Column of Sheet2 using Array使用数组将 Sheet1 的值复制到 Sheet2 的列末尾
【发布时间】:2022-06-18 13:22:34
【问题描述】:

我在 Sheet1 中的 Range("D16:D19") 中有一个值列表,要复制到 Sheet2 的“B”列的最后一行。

我想使用一个数组。

这只会将 D16 的值复制到 B 列的最后一行。

Dim Datearray As Variant
N = Worksheet4.Cells(Rows.Count, "B").End(xlUp).Row + 1
Datearray = Worksheet3.Range("D16:D19")
Worksheet4.Cells(N, 2) = Datearray

【问题讨论】:

  • 我已经添加了我的原始代码。我不明白我应该调整什么。你能说得更具体点吗?
  • 请用Worksheet4.Cells(N, 2),Resize(Ubound(Datearray), Ubound(Datearray, 2)).Value = Datearray替换Worksheet4.Cells(N, 2) = Datearray
  • 我已根据您的新要求编辑了我的帖子。

标签: arrays excel vba


【解决方案1】:

复制范围

Option Explicit

Sub CopyRange()
    ' Source
    Const sName As String = "Sheet1" ' read from
    Const sRangeAddress As String = "D16:D19"
    ' Destination
    Const dName As String = "Sheet2" ' written to
    Const dCol As String = "B"
    ' Both
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Source
    Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
    Dim srg As Range: Set srg = sws.Range(sRangeAddress)

    ' Destination
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    Dim dfCell As Range
    Set dfCell = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Offset(1)
    Dim drg As Range: Set drg = dfCell.Resize(srg.Rows.Count, srg.Columns.Count)
    ' You can omit ', srg.Columns.Count' because your copying a one-column range.
    
    ' Copy (there is no need for an array, 'srg.Value' is already one).
    drg.Value = srg.Value

End Sub

编辑

Dim n As Long
n = Worksheet4.Cells(Worksheet4.Rows.Count, "B").End(xlUp).Row + 1
Dim DateArray As Variant: DateArray = Worksheet3.Range("D16:D19").Value
Worksheet4.Cells(n, "B").Resize(UBound(DateArray, 1)).Value = DateArray

' or without the array:
Dim n As Long
n = Worksheet4.Cells(Worksheet4.Rows.Count, "B").End(xlUp).Row + 1
With Worksheet3.Range("D16:D19")
    Worksheet4.Cells(n, "B").Resize(.Rows.Count).Value = .Value
End With

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2021-05-27
    • 2016-07-25
    相关资源
    最近更新 更多