首页 > 解决方案 > 如何循环遍历 K 列中的每一行,直到 A 列为空

问题描述

我是 VBA 新手,正在尝试弄清楚如何执行以下操作:

我可以毫不费力地让代码查看一个特定的行,但我不知道如何让它循环遍历 A 不为空的所有行。

     Sub FillEmptyCells()

     Dim Lastrow As Long

     Lastrow = Range("A" & Rows.Count).End(xlUp).Row

     Do Until IsEmpty("A1:A")

     If IsEmpty(Range("K1").Value) = True Then
       Range("K1") = Range("E1")
     End If

     If IsEmpty(Range("K1").Value) = True Then
       Range("K1") = Range("G1")
     End If

     If IsEmpty(Range("K1").Value) = True Then
       Range("K1") = Range("I1")
     End If

     Loop

     End Sub

标签: excelvbais-emptydo-loops

解决方案


这是一种方法:

Sub tgr()

    Dim ws As Worksheet
    Set ws = ActiveWorkbook.ActiveSheet

    With ws.Range("K1:K" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
        .Formula = "=IF(E" & .Row & "<>"""",E" & .Row & ",IF(G" & .Row & "<>"""",G" & .Row & ",I" & .Row & "))"
        .Value = .Value
    End With

End Sub

推荐阅读