Forum Discussion

Lorenzo Kim's avatar
Lorenzo Kim
Bronze Contributor
Jun 10, 2018

VBA for not deleting entire row but just clear-contents and copying records below it one row up

Dim i As Long
For i = Cells(Rows.Count, "G").End(xlUp).Row To 2 Step -1
If Cells(i, "G") = 0 Then Rows(i).Delete
Next i

The above short code I found in the net was a powerful tool in deleting row of records which are incomplete (criteria say for column G with G=0).

it worked in one of my workbooks but I cannot use that in another because beyond column J - there are data which can not be touched.

I think I have the sequence of the codes (horribly I do not know the proper syntax).

1. determine the lastrow of column G

    Dim LastRow As Long
    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
    End With

2. code a loop until Lastrow to detect which row has G =0 then clear-contents from A-J columns only        for that row then copy 1 row up all the records below it.

    if a record is cleared Lastrow = Lastrow -1

3. repeat the loop until no G=0 record is found.

 

It sounds simple but for a little-programming-knowhow like me, this is a monstrous task!

How do I write the subroutine...

many thanks

 

1 Reply

  • Lorenzo Kim's avatar
    Lorenzo Kim
    Bronze Contributor

    sorry for posting this message, Mr. Man Fai Chan had already posted his solution to this problem.

    I only used the hi-lighting of incomplete record. I completely missed the delete program.

    so sorry..

    many thanks

Resources