Active cell as input to formula

谁说胖子不能爱 提交于 2019-12-03 09:12:59

You don't need VBA (although you may prefer it).

W1: =INDEX($F$8:$AA$29,1,MAX(COLUMN(INDIRECT(CELL("address")))-(COLUMN(F8)-1),1))
W2: =INDEX($F$8:$AA$29,MAX(ROW(INDIRECT(CELL("address")))-(ROW(F8)-1),1),1)
W3: =J4-(G4+H4)

The CELL function with the address argument returns the address for whichever cell is active. I use INDIRECT to convert that address (just a string) to a cell reference. Then I use

=INDEX(Range, 1, Column of Reference)

to get the w1 value - the value in the first row and the same column as the active cell. The formula doesn't care what cell you make active, so I stuck a MAX in there so it would return a zero if you're out of the range.

Note that simply selecting a cell won't trigger the change. Once you select a cell, press F9 to calculate the sheet to get the proper results.

You need to use VBA. Following your example place this code in your Sheet object

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Row > 8 And Target.Column > 6 Then
        Range("G4").Value = Cells(8, Target.Column).Value
        Range("H4").Value = Cells(Target.Row, 6).Value
        Range("L4").Value = Cells(Target.Row, Target.Column).Value
    End If
End Sub

Cell L4 shows your selected cell value which can be used in other formulas.

EDIT

To place your code you go to VBA window and double click on the Sheet object where you have your data. (Marked with an arrow in the picture) Then paste yout code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!