我正在尝试将一系列单元格从一个数据文件(sh1作为工作表)复制到另一个(作为工作表)。单元格应粘贴在现有数据下方。由于要复制的单元格数量和现有数据量不同。我创建了这段代码:
Dim sh1 As Worksheet, sho As Worksheet, lr As Long, rng1 As Range
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng1 = sh1.Range("A3:B" & lr)
rng1.Copy sho.Cells(Rows.Count, 1).End(xlUp)(2)
这完全正常 - 但它将Worksheet sh1中的函数复制到工作表sho而不是值。我知道这个代码是“.PasteSpecial”但我无法在我的代码中匹配它,而不会破坏其他先决条件。
答案 0 :(得分:0)
而不是:
rng1.Copy sho.Cells(Rows.Count, 1).End(xlUp)(2)
直接分配值。它避免了clipbaord,因此它更快:
sho.Cells(sho.Rows.Count, 1).End(xlUp).Offset(2).Resize(rng1.rows,rng1.columns).Value = rng1.Value
答案 1 :(得分:0)
您可以使用此代码将A1的值复制到B1(如果A1是公式)
Range("B1").Formula = Range("A1")
答案 2 :(得分:0)
如果您有Sheet1
和Sheet2
,请尝试以下操作:
Option Explicit
Sub test()
Dim sh1 As Worksheet, sho As Worksheet, lr As Long, rng1 As Range
Set sh1 = Sheet1
Set sho = Sheet2
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng1 = sh1.Range("A3:B" & lr)
rng1.Copy
sho.Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues
End Sub