如何在VBA中填写ActiveCell

时间:2019-02-06 06:37:45

标签: excel vba

有人可以帮助我吗?假设I4:I是我的Lastrow,G中的Activecell将填写公式。

Dim Lastrowzxc As Long
Lastrowzxc = Range("I4").CurrentRegion.Rows.Count

Range("G" & Rows.Count).End(xlUp).Offset(1).Select
  ActiveCell.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"
Selection.AutoFill Destination:=Range(ActiveCell.Address & Lastrowzxc)

4 个答案:

答案 0 :(得分:0)

自动填充的“范围”中的地址不正确。这可能有效,但这远非完美(仅仅是让您前进的起点):

Dim Lastrowzxc As Long
Lastrowzxc = Range("I4").CurrentRegion.Rows.Count

Range("G" & Rows.Count).End(xlUp).Offset(1).Select
  ActiveCell.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"
Selection.AutoFill Destination:=Range(Cells(2, 7), Cells(Lastrowzxc, ActiveCell.Column))

答案 1 :(得分:0)

假设I4:I20是要填充的范围,并且G4包含公式,这是您应该执行的操作:

Range("I4:I20").FormulaR1C1 = Range("G4").FormulaR1C1

答案 2 :(得分:0)

尝试一下对我有用:

Dim Lastrowzxc As Long
Lastrowzxc = ActiveSheet.Range("I4").currentRegion.Rows.Count

Range("G" & ActiveSheet.Rows.count).End(xlUp).Offset(1).Select

Selection.FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")"

Selection.AutoFill Destination:=Range(Range(ActiveCell.Address), Cells(Lastrowzxc, ActiveCell.Column))

答案 3 :(得分:0)

为什么要麻烦选择范围?这是不好的做法。

在选择col G中最后一个单元格下面的单元格时,我不太了解您的目标。我想您想从col G的最后一行开始,直到col I的最后一行?如果是这样..:

With Workbooks("REFERENCE").Sheets("REFERENCE")
LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
LastrowG = .Cells(.Rows.Count, "G").End(xlUp).Row

.Range("G" & LastrowG).FormulaR1C1 = "=IF(RC[2]<0,""40"",""50"")" 'change Range if necessary
.Range("G" & LastrowG).FormulaR1C1.AutoFill Destination:= .Range("G" & LastrowG & ":G" & LastrowI)

End With