简体   繁体   中英

VBA: How do I apply a formula to cells adjacent to non-empty cells?

FORMULA = INDEX(Sheet2!$A$2:$A$1000,MATCH(A2,Sheet2!$G$2:$G$1000,0))

A2 is a cell in active sheet, not sure if I need to be specific and say Sheet1!A2. The output would then be on B2.

ActiveCell.FormulaR1C1 = _
    "=INDEX(Sheet2!R3C1:R1000C1,MATCH(Sheet1!RC[-4],Sheet2!R3C7:R1000C7,0))"

is what I got from record macro but not sure what the RC things are.

Column A Formula Column

A
B
C
D
E
F
G
H
I

I think I need a for loop to evaluate all cells adjacent to cells that have a value to the left. I'm not sure how to input the formula with referencing cells in another sheet as well as moving down the column.

Figured it out. This ended up working for me.

'`Sub ApplyFormula()
'
' ApplyFormula Macro
'
Dim i&

For i = 2 To ActiveSheet.UsedRange.Rows.Count
    If Cells(i, 1).Value <> "" Then Cells(i, 6).FormulaR1C1 = "=INDEX(Sheet2!R2C1:R1000C1,MATCH(Sheet1!RC[-4],Sheet2!R2C7:R1000C7,0))"

Next
End Sub

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