Friday, October 28, 2022

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

Issue

I'm new to VBA and am trying to figure out how to do the following:

  • Loop through all rows in column K until A is empty
  • If the cell in column K is empty, fill it with the value in the same row in column E
  • If K is still empty, fill it with the value in the same row in column G
  • If K is still empty, fill it with the value in the same row in column 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.

     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

Solution

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


Answered By - tigeravatar
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.