简体   繁体   English

如何遍历K列中的每一行,直到A列为空

[英]How to loop through each row in column K until column A is empty

I'm new to VBA and am trying to figure out how to do the following: 我是VBA的新手,正在尝试找出如何执行以下操作:

  • Loop through all rows in column K until A is empty 循环遍历K列中的所有行,直到A为空
  • If the cell in column K is empty, fill it with the value in the same row in column E 如果K列中的单元格为空,请在E列的同一行中填充该值
  • If K is still empty, fill it with the value in the same row in column G 如果K仍然为空,则用G列同一行中的值填充
  • If K is still empty, fill it with the value in the same row in column I 如果K仍然为空,请在I列的同一行中填充该值

I have no trouble getting the code to look at one particular row, but I can't figure out how to get it to loop through all rows where A is not empty. 我可以很轻松地让代码查看某一特定行,但是我无法弄清楚如何使它遍历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

Here's one way to do it: 这是一种实现方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM