Matthew Bershok
Matthew Bershok

Reputation: 21

Excel VBA moving contents of cell and deleting rows reading each line till end of data

I am currently trying to finish a macro for excel vba. I need to read each line. If the value in column A is blank or equal to 0 I need to move the contents of column D from that row up one row and right one column. Example, If cell A55 is blank it will read the value in D55 and move it up one row and right one column to cell E54. It needs to then delete the entire row with the blank value in A55, therefore removing row 55 completely. I have the following code. This code will read the code line by line and delete the rows I need it to; however I can not get it to move the contents in column D. Thank you in advance

Dim ws As Worksheet, iA&, lastRowA&, valueA$,
Set ws = ActiveWorkbook.ActiveSheet

lastRowA = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

For iA = lastRowA To 1 Step -1
    valueA = ws.Cells(iA, 1).value
    ' Check if it contains blank space in "A" column
    If (valueA = "") Then
        'CODE TO MOVE CONTENTS FROM CELL TO NEW LOCATION
        ws.Rows(iA).Delete
    End If
Next

Upvotes: 0

Views: 1098

Answers (1)

GSerg
GSerg

Reputation: 78200

For iA = lastRowA To 1 Step -1
    With ws.Rows(iA)
        valueA = .Cells(1).value

        If Len(valueA) = 0 Then
            .Cells(4).Offset(-1, 1).Value = .Cells(4).Value
            .Delete
        End If
    End With
Next

Upvotes: 1

Related Questions