0

enter image description here

I'm trying to do the following:

  1. check if the cell from column A is blank,
  2. if yes, cut vale from column C (same row)
  3. Paste cut value to column D (1 row above)
  4. Delete blank row (row3)
  5. Loop until row 1302

scenario:

  1. Check row A1, empty = false.
  2. Check row A2, empty = false.
  3. Check row A3, empty = true.
  4. Cut C3. Paste to D2.
  5. Check row A4, empty = false. Check row A5, empty = true.
  6. Cut C5. Paste to D4.
  7. Delete row 3 (for being blank)
  8. Loop until row 1302.

So far

Sub Macro5()
      Range("A2").Select
      Do Until IsEmpty(ActiveCell)
         ActiveCell.Offset(1, 0).Select
      Loop

End Sub

TIA

Gab G
  • 1
  • 2
  • You might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). • What exactly is your question? You didn't ask one. Where did you get stuck or errors? What did you stop from continuing? Did you try the Macro Recorder? – Pᴇʜ Sep 08 '20 at 09:01
  • By cut do you mean to delete that cell in column c and shift the data up? If so your data will come out of line the first blank instance. Or do you want to delete the whole row after copying column C to D? This would probably need to be looped in reverse if you do want to delete. Otherwise if you have a blank in A3, then when you delete that row, A4 will become A3 but your next loop will go to A4 meaning you skip what was A4 because it's now A3. – Simon Sep 08 '20 at 09:08
  • I guess to avoid skipping, I can focus on just cutting C3 and pasting it to D2.Then run another macro to clean-up the blank rows. – Gab G Sep 08 '20 at 10:24
  • There's no need for two macros. The one just needs to run from the bottom up. Again are you deleting (where a row in "A" has a blank) just Column C or the entire row or A:C ? – Simon Sep 08 '20 at 10:58
  • After copying C3 to D2, I'm deleting the blank rows. Row 3 will be blank after cutting C3. The whole Row 3 will be deleted and Row 4 will move up to become Row 3. – Gab G Sep 08 '20 at 11:05

3 Answers3

0

Since you are going to delete rows, loop from bottom to top

For rw = 1302 to 1 step -1

Test a Cell using the Values property

    If IsEmpty(Cells(re, 1).Value) Then

Assuming you don't need to copy Formats, just copy the value

        Cells(rw - 1, 4).Value = Cells(rw, 3).Value

Delete the row

        Rows(rw).Delete 

Move on

   End If
Next
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
0

Here you go. Change the couple things that may need to be changed. I've set it to run between row 2 to row 1302. It loops from the bottom up which is needed when deleting rows.

Sub CutC()

Dim i As Long, ws As Worksheet

Set ws = Sheets("Sheet1") 'set your sheet name

For i = 1302 To 2 Step -1 'This is your range to work from. Going from bottom row (1302) to top row (2)
    If ws.Range("A" & i) = "" Then
        ws.Range("D" & i - 1) = ws.Range("C" & i)
        ws.Range("A" & i).EntireRow.Delete
    End If
Next i

End Sub
Simon
  • 1,384
  • 2
  • 10
  • 19
-1

Enter this formula to D2 and fill down: =if(A3='', C3)

Copy column D and paste as value.

Filter blank cells in column A, clear content or delete entire rows is up to you.

xwhitelight
  • 1,569
  • 1
  • 10
  • 19