Insert formula with dynamic row

后端 未结 3 1104
旧时难觅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-25 23:59

    Set a variable for the row value and convert it to a string for your formula insert like so

    Sub Check()
        Dim i As Integer
        Dim j As String
    
        'for each row 
        For i = 8 To 48
            If Cells(i, 1).Value = "Date:" Then
                'set the string for use in the formula
                j = Trim(Str(i))
                Cells(i, 2).Formula = "=TEXT(T8,""mmm-dd-yyyy"")& "" | "" &V" & j & "& "" - "" &U" & j & "&"" | Dept: ""&W" & j & ""
           End If
        Next
    End Sub
    

    If your range is really this limited, you would be better of setting a conditional formula directly in your sheet like this:

    =IF(A8="Date:",TEXT(T8,"mmm-dd-yyyy") & " | " & V8 & " - " & U8 & " | Dept: " & W8,"")
    

    This will only display the text on the condition that A8 = Date:. Dragging the formula down will increment the row number

提交回复
热议问题