【问题标题】:How to paste a column from one sheet below another sheet? Both columns are of equal but unknown length如何将一个工作表中的列粘贴到另一工作表下方?两列的长度相等但未知
【发布时间】:2019-08-13 01:57:22
【问题描述】:

我的成功之处:

我能够将“Sheet1”上从 F2 开始的列粘贴到“加载文件”工作表上,因为我知道该列将从 E2 开始。

我正在努力解决的问题:

我需要复制同一列,将列中的每个值加 28(值是日期),然后将结果粘贴到我刚刚粘贴的列的正下方

这是我尝试过的代码。第一点就像我提到的那样工作

'Developement Complete

Set ws = Application.Worksheets("Sheet1")
endRowSheet1 = ws.UsedRange.Rows.Count
Set wsOut = Application.Worksheets("Load File")

Set r = ws.Range(Cells(2, 1), Cells(endRowSheet1, 1))
    r.Copy
    wsOut.Range("A2").PasteSpecial xlPasteAll

Set r = ws.Range(Cells(2, 6), Cells(endRowSheet1, 6))
    r.Copy
    wsOut.Range("E2").PasteSpecial xlPasteAll

endRowDevelopment = wsOut.UsedRange.Rows.Count
'Compliance Regulatory
'need to add 28 to the value in each cell of copied column

Set r = ws.Range(Cells(2, 1), Cells(endRow, 1))
    r.Copy
    wsOut.Range(Cells(endRowDevelopment + 1, 1), Cells(endRowDevelopment * 2 + 1, 1)).PasteSpecial xlPasteAll

Set r = ws.Range(Cells(2, 6), Cells(endRow, 6))
    r.Copy
    wsOut.Range(Cells(endRowDevelopment + 1, 5), Cells(endRowDevelopment * 2 + 1, 5)).PasteSpecial xlPasteAll
End Sub

我收到运行时错误 1004,所以我怀疑我使用 range 函数的方式有问题。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    单元格指的是特定的工作表,需要用“.”限定。此外,不要使用 UsedRange 来计算行数。如果您删除目标工作表中的行,即使它们是空白的,它们也会保留在 UsedRange 中。只需计算工作表中的行数或要粘贴值的列中的行数。最后,如果您只想粘贴日期,可以稍微清理一下代码。

    Sub test()
    
    Set ws = Application.Worksheets("Sheet1")
    
    endRowSheet1 = ws.Cells(Rows.Count, 1).End(xlUp).Row
    
    Set wsOut = Application.Worksheets("Load File")
    
    With ws
    
    Set r1 = ws.Range(.Cells(2, 1), .Cells(endRowSheet1, 1))
        r1.Copy Destination:=wsOut.Range("A2")
    
    Set r2 = ws.Range(.Cells(2, 6), .Cells(endRowSheet1, 6))
        r2.Copy Destination:=wsOut.Range("E2")
    
    End With
    
    endRowDevelopment = wsOut.Cells(Rows.Count, 1).End(xlUp).Row
    
        r1.Copy Destination:=wsOut.Range("A" & endRowDevelopment + 1)
        r2.Copy Destination:=wsOut.Range("E" & endRowDevelopment + 1)
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      您需要使用工作表完全限定所有 RangeCells 引用(注意下面的点):

      With ws
          Set r = .Range(.Cells(2, 1), .Cells(endRowSheet1, 1))
          r.Copy
          wsOut.Range("A2").PasteSpecial xlPasteAll
          Set r = ws.Range(.Cells(2, 6), .Cells(endRowSheet1, 6))
          r.Copy
          wsOut.Range("E2").PasteSpecial xlPasteAll
          endRowDevelopment = wsOut.UsedRange.Rows.Count
          Set r = .Range(.Cells(2, 1), .Cells(endRow, 1))
          r.Copy
          wsOut.Range(wsOut.Cells(endRowDevelopment + 1, 1), wsOut.Cells(endRowDevelopment * 2 + 1, 1)).PasteSpecial xlPasteAll
          Set r = .Range(.Cells(2, 6), .Cells(endRow, 6))
          r.Copy
          wsOut.Range(wsOut.Cells(endRowDevelopment + 1, 5), wsOut.Cells(endRowDevelopment * 2 + 1, 5)).PasteSpecial xlPasteAll
      end with
      

      【讨论】:

      • 感谢您的帮助!当我运行此代码时,我收到运行时错误 91。经过一番谷歌搜索后,我认为这与 Set 语句有关。你知道怎么解决吗?
      • 在哪一行?
      • 我不确定如何检查?弹出错误信息后,代码中没有高亮一行
      • 尝试使用 F8 单步执行您的代码。是否定义了所有变量(给定值)?在最后 2 次粘贴中,您可能会尝试更改目标单元格,以便仅更改顶部单元格(而不是范围),但不要认为这会导致该错误。
      • 完成!没有意识到这是一个选择哈哈
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      相关资源
      最近更新 更多