vba VBA从左到右拖动公式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16300573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA drag formula from left to right
提问by Josephine Bautista
I am trying to drag a formula from a cell to another cell to the right (i.e. cellA1 [formula: A2 + A3] -> cellB1 [formula: B2+B3])
. Here is what I have:
我试图将一个公式从一个单元格拖到另一个单元格的右侧(即cellA1 [formula: A2 + A3] -> cellB1 [formula: B2+B3])
。这是我所拥有的:
Dim lastColumn As Long
lastColumn = Workbook2.Worksheets(7).Cells(1, Columns.Count).End(xlToLeft).Column
cellSource = Workbook2.Worksheets(7).Cells(3, lastColumn)
cellTarget = Workbook2.Worksheets(7).Cells(3, lastColumn + 1)
cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault
Error is "Object Required"
错误是“需要对象”
回答by Santosh
Try below code :
试试下面的代码:
Dim lastColumn As Long, cellSource As Range, cellTarget As Range
lastColumn = Workbook2.Worksheets(7).Cells(1, Columns.Count).End(xlToLeft).Column
Set cellSource = Workbook2.Worksheets(7).Range(Cells(3, lastColumn).Address)
Set cellTarget = Workbook2.Worksheets(7).Range(Cells(3, lastColumn), Cells(3, lastColumn + 1))
cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault
回答by Our Man in Bananas
ranges are objects, so you should use the setkeyword when assigning values to those variables:
范围是对象,因此在为这些变量赋值时应该使用set关键字:
try this:
尝试这个:
Dim lastColumn As Long
lastColumn = Workbook2.Worksheets(7).Cells(1, Columns.Count).End(xlToLeft).Column
set cellSource = Workbook2.Worksheets(7).Cells(3, lastColumn)
set cellTarget = Workbook2.Worksheets(7).Cells(3, lastColumn + 1)
cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault