
如何使用VBA进行公式自动填充?
我想通过VBA实现这个功能,目前A1-A10有数据,B1-B10有公式为B1=A1*2,C1-C10有公式为C1=B1*2,现在如何通过VBA实现,当A11输入数字时,B...
我想通过VBA实现这个功能,目前A1-A10有数据,B1-B10有公式为B1=A1*2,C1-C10有公式为C1=B1*2,现在如何通过VBA实现,当A11输入数字时,B11、C11自动填充公式。不需要把公式写入VBA中,也不需要在excel中开启扩展数据区域格式及公式。
展开
4个回答
展开全部
输入完代码后,关闭VB编辑器即可。
Private Sub Worksheet_Change(ByVal Target As Range)
x = Target.Row
y = Target.Column
If y = 1 Then
If Cells(x, y) = "" Then
Cells(x, y + 1).ClearContents
Cells(x, y + 2).ClearContents
ElseIf IsNumeric(Cells(x, y)) = True Then
Cells(x, y + 1) = Cells(x, y) * 2
Cells(x, y + 2) = Cells(x, y + 1) * 2
End If
End If
End Sub
公式不管写在哪里,总得写出来吧,不然表格怎么知道你要怎么算。改了一下代码,你试试是不是这个效果。
Private Sub Worksheet_Change(ByVal Target As Range)
x = Range("A" & Rows.Count).End(3).Row
y = Target.Column
If y = 1 Then
Dim i As Long
For i = 1 To x
If Cells(i, y) = "" Then
Cells(i, y + 1).ClearContents
Cells(i, y + 2).ClearContents
ElseIf IsNumeric(Cells(i, y)) = True Then
Cells(i, y + 1) = Cells(i, y) * 2
Cells(i, y + 2) = Cells(i, y + 1) * 2
End If
Next
End If
End Sub
嗯试试再说
展开全部
A表数据输入后通过VBA过入到B表,一次有N行,B表中C、E列有公式,公式太复杂,无法通过VBA直接输入,只能填充,如何通过VBA选中B表C-E列的M至M+N行,如何填充公式,因为目前无法确定会使用多少行数据,所以不想预先拉很多行公式。谢谢
Sheets("交易记录列表").Activate
Range(Cells(XE + 1, 9), Cells(XE + H + 1, 17)).Select
Selection.FillDown
在Range前加表格名称会导致错误,所以需先激活目标表格
Sheets("交易记录列表").Activate
Range(Cells(XE + 1, 9), Cells(XE + H + 1, 17)).Select
Selection.FillDown
在Range前加表格名称会导致错误,所以需先激活目标表格
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
alt+f11打开VBE
双击你的工作表,在右边贴入
Private Sub Worksheet_Change(ByVal Target As Range)
Dim n
n = Target.Row
If Target.Column = 1 Then
If VBA.IsNumeric(Target.Value) Then
Range("B" & n).Formula = Replace(Range("B" & n - 1).Formula, "A" & n - 1, "A" & n)
Range("C" & n).Formula = Replace(Range("C" & n - 1).Formula, "B" & n - 1, "B" & n)
Else
MsgBox "输入的不是数字,请重新输入!"
End If
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询