我已经写了一个代码,但是它只粘贴要粘贴的现有公式,而不是公式。
Sub lastRow()
Dim wsS1 As Worksheet 'Sheet1
Dim wsS2 As Worksheet 'sheet2
Dim lastR As Long, lastC As Long
Set wsS1 = Sheets("Instru Input")
Set wsS2 = Sheets("Process1")
With wsS1
lastR = .Range("A" & .Rows.Count).End(xlUp).Row - 4
End With
With wsS2
lastC = .Cells(3, Columns.Count).End(xlToLeft).Column
Range(.Cells(3, 1).Address, .Cells(3, lastC).Address).AutoFill
Destination:=Range(.Cells(3, 1).Address, .Cells(lastR, lastC).Address)
End With
End Sub
我是VBA的新手,我不知道如何将数据粘贴为值,我不确定要添加到此代码中的内容仅将其粘贴为值。
答案 0 :(得分:0)
首先,在每个Range()
和Cells()
之前加一个点,这样您就可以正确地引用With wsS2
。否则,它将引用ActiveSheet
或Worksheet
,其中代码为-VBA - Loop Through Multiple Worksheets and Apply Filter
关于该问题-一种快速解决此问题的方法,可以在末尾添加以下行:
.Range(.Cells(3, 1), .Cells(lastR, lastC)).Value2 =.Range(.Cells(3, 1), .Cells(lastR, lastC)).Value2
在End With
之前。它将很快将公式转换为值。或使用注释中的选项,在目标位置后添加`,Type:= xlFillValues:
答案 1 :(得分:0)
我想这可以解决问题。它不是高级代码...但是我认为它将提供预期的输出,您可以尝试执行此操作-
Sub lastRow()
Dim wsS1 As Worksheet 'Sheet1
Dim wsS2 As Worksheet 'sheet2
Dim lastR As Long, lastC As Long
Set wsS1 = Sheets("Instru Input")
Set wsS2 = Sheets("Process1")
With wsS1
lastR = .Range("A" & .Rows.Count).End(xlUp).Row - 4
End With
With wsS2
lastC = .Cells(3, Columns.Count).End(xlToLeft).Column
Range(.Cells(3, 1).Address, .Cells(3, lastC).Address).AutoFill
.Range(.Cells(3,1).Address, .Cells(lastR, lastC).Address)
End With
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub