使用FormulaR1C1锁定单元格

时间:2017-11-27 22:03:53

标签: excel-vba vba excel

我想知道是否可以使用vba锁定单元格引用。

这是我的代码

Sub Practice()
Dim myValue As Variant

'Get Input Value
myValue = InputBox("Input")

'Insert a Row
ActiveCell.Offset(0, 1).EntireColumn.Insert

'Select Blank Cell in New Column
ActiveCell.Offset(0, 1).Select

'Formula
ActiveCell.Value = myValue
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=.25*$R[-1]$C[0]*R[0]C[-1]"


End Sub

我希望单元格中的公式读取" = .25 * $ B $ 1 * A2" (假设B2是它被放入的单元格)

1 个答案:

答案 0 :(得分:1)

避免使用error: package com.company.module.pojo does not exist

另外,为了使相对单元格成为绝对单元,而不事先了解单元格,您只需将该单元格的地址传递给公式即可。

此外,您需要确保输入是数字而不是文本字符串:

.Select

如果您的单元格始终为B1,那么您可以使用Sub Practice() Dim myValue As Variant 'Get Input Value Do myValue = InputBox("Input") If Not IsNumeric(myValue) Then MsgBox "Input must ba a number" Loop Until IsNumeric(myValue) 'Insert a Row ActiveCell.Offset(0, 1).EntireColumn.Insert 'Select Blank Cell in New Column ActiveCell.Offset(0, 1).Value = myValue ActiveCell.Offset(1, 1).FormulaR1C1 = "=.25*" & ActiveCell.Offset(0, 1).Address(1, 1, xlR1C1) & "*R[0]C[-1]" End Sub 代替R1C2