我是Macros / VBA的新手;我对不断更新的电子表格进行定期分析,并希望为非常重复的过程练习开发宏。我已经记录了一个宏,我得到了粘贴方法错误。 VBA代码如下。我正在复制另一份文件中的公式;我可以直接在VBA代码中嵌入公式,以避免粘贴错误吗?
Sub SLA()
'
' SLA Macro
'
'
Range("Table_Tracker[[#Headers],[Docs]]").Select
Selection.Copy
Range("Table_Tracker[#All]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("N:R").Select
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"
Columns("S:S").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("S1").Select
ActiveCell.FormulaR1C1 = "1 Day SLA"
Columns("T:T").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("T1").Select
ActiveCell.FormulaR1C1 = "3 Day SLA"
Columns("U:U").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.FormulaR1C1 = "Prepare EDD"
Columns("V:V").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.FormulaR1C1 = "Analyze EDD"
Columns("W:W").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("W1").Select
ActiveCell.FormulaR1C1 = "Case Completion"
Range("A1").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$AK$1030"), , xlYes).Name _
= "Table6"
Range("Table6[#All]").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 9
Range("S2").Select
ActiveSheet.Paste
Range("T2").Select
ActiveSheet.Paste
Range("U2").Select
ActiveSheet.Paste
Range("V2").Select
ActiveSheet.Paste
Range("W2").Select
ActiveSheet.Paste
End SubSub SLA()
错误出现在Range(“S2”)
之后的行中答案 0 :(得分:0)
是的,您可以使用解释here的Range.Formula
属性在VBA中嵌入公式。
例如:
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Formula = "=SUM(A2:A101)"
字符串可以是任何公式,并且可以包含各种部分以包含VBA变量。
例如:
Sub writeFormula()
Dim i As Long
With ThisWorkbook.Worksheets("Sheet1")
For i = 1 To 100
.Cells(i, 1).Formula = "=SUM(" & .Range(.Cells(i, 2), .Cells(i, 20)).Address & ")"
Next
End With
End Sub
这会将公式=SUM($B$1:$T$1)
写入单元格A1,将=SUM($B$2:$T$2)
写入A2,依此类推,直至单元格A100中的=SUM($B$100:$T$100)
。