简体   繁体   English

如何在VBA中的公式中使用固定单元格

[英]How to use a fixed cell in a formula in vba

I have an issue with using a formula that contains a fixed cell in VBA. 我在使用VBA中包含固定单元格的公式时遇到问题。 The issue comes when the row number of the variable in the new data changes. 当新数据中变量的行号更改时,就会出现问题。 The issue is explained using a simple example as follow. 使用以下简单示例说明了该问题。 I hope you find it understandable. 希望您能理解。 Let's say I have a column of numbers (Time) and I want to multiply them by a variable in a cell (The cell below Variable in the following table, $A$2). 假设我有一列数字(时间),我想将它们乘以一个单元格中的变量(下表中变量下方的单元格,$ A $ 2)。

First result from first raw data: 来自第一个原始数据的第一个结果:
来自第一原始数据的第一结果

The results in the table are calculated using the following formula "=R2C1*RC[-1]" in vba 表中的结果是使用vba中的以下公式"=R2C1*RC[-1]"
使用的VBA公式 Now in the next calculation, the row number and variable change and the part of the formula which is using a fixed cell cause problem. 现在在下一次计算中,行号和变量会发生变化,并且使用固定单元格的公式部分会引起问题。
Second raw data to be processed 要处理的第二个原始数据

要处理的第二个原始数据
Because it does not update the row number and use the old row number. 因为它不会更新行号并使用旧的行号。 I want it to find its location like the second part of the formula (B2 changes to B7). 我希望它像公式的第二部分一样找到它的位置(B2更改为B7)。 Thank you for your help! 谢谢您的帮助! Cheers, Aryan 干杯,雅利安

you should reference the found cell row in your formula 您应该在公式中引用找到的单元格行

ActiveCell.FormulaR1C1 = "=R" & ActiveCell.Row + 1 & "C1*RC[-1]"

but you should also avoid the Activate/ActiveXXX/Select/Selection pattern since is prone to have you quickly lose control over the actually active thing 但您还应避免使用Activate / ActiveXXX / Select / Selection模式,因为它容易使您很快失去对实际活动对象的控制

finally you an use a loop to find all "Time" occurrences (see Here for more info about the pattern) 最后,您可以使用循环查找所有“时间”事件(有关此模式的更多信息,请参见此处

Option Explicit

Sub main()
    Dim f As Range, firstCell As Range
    With Worksheets("myWorksheetName") ' reference your worksheet (change myWorksheetName to your actual sheet name)
        With .Range("B1", .Cells(.Rows.Count, "B").End(xlUp)) 'reference its column B cells from row 1 down to last not empty one
            Set f = .Find("Time", LookIn:=xlValues, lookat:=xlWhole) 'search referenced range for first occurrence of "time"
            If Not f Is Nothing Then ' if found...
                Set firstCell = f ' store first occurrence cell
                Do
                    f.Offset(1, 1).Resize(4).FormulaR1C1 = "=R" & f.Row + 1 & "C1*RC[-1]" ' populate the range one column to the right of found cell and 4 rows wide with the formula containg the reference of found cell row +1
                    Set f = .FindNext(f) ' serach for the next "Time" occurrence
                Loop While f.Row <> firstCell.Row ' loop till you wrap back to initial occurrence
            End If
        End With
    End With
End Sub

The notation R2C1 is an absolute reference to row 2, column 1. 符号R2C1是对第2行,第1列的绝对引用。

If you want a reference that is relative to the current cell, you need to use relative reference notation. 如果要使用相对于当前单元格的引用,则需要使用相对引用表示法。

RC[-1] points to a cell in the current row and one column to the left RC[-1]指向当前行中的一个单元格,向左指向一列

R[1]C points to a cell one row down from the current cell and in the same column as the current cell. R[1]C指向比当前单元格低一排且与当前单元格位于同一列的单元格。

Google for "R1C1 reference". Google提供“ R1C1参考”。 You will find many articles, for eg https://smurfonspreadsheets.wordpress.com/2007/11/12/r1c1-notation/ 您会发现许多文章,例如https://smurfonspreadsheets.wordpress.com/2007/11/12/r1c1-notation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM