PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

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

 October 28, 2022     do-loops, excel, is-empty, vba     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing