Insert formula with dynamic row

后端 未结 3 1032
旧时难觅i
旧时难觅i 2021-01-25 23:33

I have a formula that looks for a \"Date:\" Value in column A and pastes a formula in the adjacent B cell, but I can\'t figure out how to make the formula dynamic.

So if

3条回答
  •  悲&欢浪女
    2021-01-26 00:01

    Use the R1C1 style of formatting:

    Sub Check()
        Dim rng As Range
        Dim i As Long
        Dim cell As Range 'Remember to declare cell as a range.
    
        'Qualify your ranges with at least the sheet name.
        Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A8:A48")
    
        For Each cell In rng
            'test if cell is empty
            If cell.Value = "Date:" Then
                'write to adjacent cell
    
                'cell.Offset(0, 1).Formula = "=TEXT(T8,""mmm-dd-yyyy"")&"" | ""&V8&"" - ""&U8&"" | Dept: ""&W8"
                cell.Offset(0, 1).FormulaR1C1 = "=TEXT(RC20,""mmm-dd-yyyy"")&"" | ""&RC22&"" - ""&RC21&"" | Dept: ""&RC23"
    
            End If
        Next
    End Sub
    

    RC20 means this row, column 20. R1C20 means row 1, column 20. RC[-1] means this row, one column to the left.
    http://www.numeritas.co.uk/2013/09/the-%E2%80%98dark-art%E2%80%99-of-r1c1-notation/

提交回复
热议问题