我希望我的公式跟随目标细胞

时间:2017-08-02 09:09:23

标签: excel vba excel-vba

美好的一天,

即使我在第20行之前插入了多行,我希望此代码遵循目标单元格(G20)

=LEFT(G20,3)&TEXT(COUNTIF(G20:G20,LEFT(G20,1)&"*"),"000")

谢谢,

是否也可以为Cell D20设置INDRECT代码?

Private Sub CommandButton1_Click()
Dim x As String, rcell As Range, y As String, z As Long

x = "RFP" & Format(Now, "mmddyyhhmmss")
y = Left(x, 10)
z = Right(x, 2)

For Each rcell In Range("D20:D" & Range("D" & Rows.Count).End(3).Row)
If Cells(rcell.Row, 1) = "" Then Cells(rcell.Row, 1) = y & z
z = z + 1
Next rcell

End Sub

3 个答案:

答案 0 :(得分:2)

尝试用2美元符号围绕G20。像这样:

=LEFT($G$20,3)&TEXT(COUNTIF($G$20:$G$20,LEFT($G$20,1)&"*"),"000")

在这里阅读更多有关美元符号的信息:

https://blogs.office.com/en-us/2011/08/17/making-sense-of-dollar-signs-in-excel/ 一般来说,它们确实很有用。

答案 1 :(得分:1)

如果您总是希望公式在上面插入行之后引用Cell G20,请使用INDIRECT函数

=LEFT(INDIRECT("G20"),3)&TEXT(COUNTIF(INDIRECT("G20:G20"),LEFT(INDIRECT("G20"),1)&"*"),"000")

注意: INDIRECT是一个易失性函数。有关详细信息,请参阅this

答案 2 :(得分:0)

试试这个

=LEFT($G$20,3)&TEXT(COUNTIF($G$20:$G$20,LEFT($G$20,1)&"*"),"000")