为每一行循环 Excel 代码,直到找到空白单元格

Loop Excel code for each line until blank cell is found

我有以下代码需要循环 ~100 行。有没有办法在此处添加循环功能以重复直到一行为空白,而不是为每一行写出来?我无法弄清楚 Do While Loop 功能并将其合并到下面的代码中。谢谢!

 Sub Excel_INDIRECT_Function()

 'declare a variable
    Dim ws As Worksheet

 Set ws = Worksheets("TOC")

    'apply the Excel INDIRECT function

ws.Range("$F8").Formula = "=INDIRECT($W8&""Q24"")"
ws.Range("$G8").Formula = "=INDIRECT($W8&""Q30"")"
ws.Range("$I8").Formula = "=INDIRECT($W8&""I56"")"
ws.Range("$J8").Formula = "=INDIRECT($W8&""Q34"")"
ws.Range("$K8").Formula = "=INDIRECT($W8&""D7"")"
ws.Range("$L8").Formula = "=INDIRECT($W8&""L56"")"
ws.Range("$M8").Formula = "=INDIRECT($W8&""M56"")"
ws.Range("$N8").Formula = "=INDIRECT($W8&""N56"")"
ws.Range("$O8").Formula = "=INDIRECT($W8&""O56"")"
ws.Range("$R8").Formula = "=INDIRECT($W8&""D6"")"

End Sub

我希望在不编写此代码约 100 次的情况下为多行完成此操作。非常感谢。

我不确定你是否需要循环。

此代码会将 F、G 等列中的公式从第 8 行向下放置到 W 列中的最后一行数据。

Sub Excel_INDIRECT_Function()
'declare a variable
Dim ws As Worksheet
Dim lngLastRow As Long

    Set ws = Worksheets("TOC")
    
    lngLastRow = ws.Range("W" & Rows.Count).End(xlUp).Row
    'apply the Excel INDIRECT function

    ws.Range("F8:F" & lngLastRow).Formula = "=INDIRECT($W8&""Q24"")"
    ws.Range("G8:G" & lngLastRow).Formula = "=INDIRECT($W8&""Q30"")"
    ws.Range("I8:I" & lngLastRow).Formula = "=INDIRECT($W8&""I56"")"
    ws.Range("J8:J" & lngLastRow).Formula = "=INDIRECT($W8&""Q34"")"
    ws.Range("K8:K" & lngLastRow).Formula = "=INDIRECT($W8&""D7"")"
    ws.Range("L8:L" & lngLastRow).Formula = "=INDIRECT($W8&""L56"")"
    ws.Range("M8:M" & lngLastRow).Formula = "=INDIRECT($W8&""M56"")"
    ws.Range("N8:N" & lngLastRow).Formula = "=INDIRECT($W8&""N56"")"
    ws.Range("O8:O" & lngLastRow).Formula = "=INDIRECT($W8&""O56"")"
    ws.Range("R8:R" & lngLastRow).Formula = "=INDIRECT($W8&""D6"")"

End Sub