简体   繁体   中英

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. 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).

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

The results in the table are calculated using the following formula "=R2C1*RC[-1]" in vba
使用的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). 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

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.

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

R[1]C points to a cell one row down from the current cell and in the same column as the current cell.

Google for "R1C1 reference". You will find many articles, for eg https://smurfonspreadsheets.wordpress.com/2007/11/12/r1c1-notation/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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