0

So I have the above code, I want to continue on this logic: if K4 is not empty then copy L3 to L4 and so on, until one cell is empty (for example K32 is not empty then copy L31 to L32, but if K33 is empty then stop) Im sorry if this is a basic question, I have just started working with VBA. Thank you in advance

   Sub kepletmasolos()

Sheets("XTR MSTR").Select

 If IsEmpty(Range("K3").Value) = False Then

      Range("L2").Copy Range("L3")

   End If

End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • 1
    There are numerous example how to loop through a given range, e.g. [Loop through each cell ...](https://stackoverflow.com/questions/3875415/loop-through-each-cell-in-a-range-of-cells-when-given-a-range-object). – T.M. Oct 06 '21 at 14:57
  • 1
    Does this answer your question? [Loop through each cell in a range of cells when given a Range object](https://stackoverflow.com/questions/3875415/loop-through-each-cell-in-a-range-of-cells-when-given-a-range-object) – nima Oct 08 '21 at 10:51

2 Answers2

0

Here is one way, using a Do loop:

Sub kepletmasolos()

Dim r As Long: r = 3

With Sheets("XTR MSTR")
    Do Until IsEmpty(.Cells(r, "K"))
        '.Cells(r, "L").Value = .Cells(r - 1, "L").Value
        .Cells(r - 1, "L").copy .Cells(r, "L")
        r = r + 1
    Loop
End With

End Sub
SJR
  • 22,986
  • 6
  • 18
  • 26
0

new to VBA myself, Just thought i would through my idea in?

row = where you would start, let me know what you think.

Sub kepletmasolos()

Dim Row As Long 
Row = 1

Start:
With Sheets("XTR MSTR")
    If Not isEmpty(.Cells(Row, "K")) Then
        .Cells(Row, "L").Copy .Cells(Row + 1, "L")
        Row = Row + 1
        GoTo Start
    Else
End If
End With

End Sub
Kieran
  • 1
  • 3
  • Hint: `GoTo` is widely considered as harmful to structured programming (originating in former BASIC times with excessive *Spaghetti programming*) - except its use in error handling; c.f. [GoTo](https://en.wikipedia.org/wiki/Goto) or [GoTo still considered harmful?](https://stackoverflow.com/questions/46586/goto-still-considered-harmful) – T.M. Oct 07 '21 at 14:21