Search code examples
excelvbaconcatenation

Deleting Cells after Concatenation in VBA


I am trying to reformat a text file that has been imported into Excel.

I have done several minor reformatting points including adding rows, deleting page numbers, and combining headlines back into a single cell via the & function (the text file was delimited when importing).

After a concatenate, in which I took certain cells from columns A-Z and combined them in Column A, I tried to delete the now redundant information from Columns B-Z.

I tried selecting the cells and deleting, and also Range.Clear, but it does not delete the cells. I receive no errors.

This is what I have to take care of this step:

'Fix Duplicate Cells from Concatenate
For i = lastRow2 To 2 Step -1
    If IsEmpty(Range(i, 1).Offset(-1, 0)) = True Then
        ActiveSheet.Range(Cells(i, 2), Cells(i, 26)).Clear
    End If
Next

Ultimately, I would like to check if column A contains no information one row above the row where I would like to delete information from columns B-Z.

Full code:

Sub Format()
'This will delete page numbers
Dim lRow As Long
Dim iCntr As Long
lRow = 350
For iCntr = lRow To 1 Step -1
    If IsNumeric(Cells(iCntr, 1)) Then
        Rows(iCntr).Delete
    End If
Next

'Add Row above each row with Headings
Dim lRow2 As Long, iRow As Long

With Worksheets("Sheet1")
    lRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row in column A

    'loop backwards (bottom to top = Step -1) through all rows
    For iRow = lRow2 To 1 Step -1
        'check if column A of current row (iRow) is "DIM"
        If .Cells(iRow, "A").Value = "DIM" Then
            .Rows(iRow).Resize(RowSize:=1).Insert xlShiftDown
            'insert 1 row and move current (iRow) row down (xlShiftDown)
            'means: insert 1 row ABOVE current row (iRow)
        End If
    Next iRow
End With

'Combine Headings back to single Cell
Dim lngLastRow As String
Dim lastRow As Long
Dim lastcolumn As Long

lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count

For i = lastRow To 1 Step -1
    If Cells(i, 1).Value = "DIM" Then
        Cells(i, 1).Value = Cells(i, 1).Value & " " & Cells(i, 2).Value & " " & _
        Cells(i, 3).Value & " " & Cells(i, 4).Value & " " & Cells(i, 5).Value & " " & _
        Cells(i, 6).Value & " " & Cells(i, 7).Value & " " & Cells(i, 8).Value & " " & _
        Cells(i, 9).Value & " " & Cells(i, 10).Value & " " & Cells(i, 11).Value & " " & _
        Cells(i, 12).Value & " " & Cells(i, 13).Value & " " & Cells(i, 14).Value & " " & _
        Cells(i, 15).Value & " " & Cells(i, 16).Value & " " & Cells(i, 17).Value & " " & _
        Cells(i, 18).Value & " " & Cells(i, 19).Value & " " & Cells(i, 20).Value & " " & _
        Cells(i, 21).Value & " " & Cells(i, 22).Value & " " & Cells(i, 23).Value & " " & _
        Cells(i, 24).Value & " " & Cells(i, 25).Value & " " & Cells(i, 25).Value
    End If
Next

'Fix Duplicate Cells from Concatenate
For i = lastRow2 To 2 Step -1
    If IsEmpty(Range(i, 1).Offset(-1, 0)) = True Then
        ActiveSheet.Range(Cells(i, 2), Cells(i, 26)).Clear
    End If
Next

End Sub

The reason I have a condition set for the clearing of cells after concatenate is because I do not simply want to clear all cells in range B:Z, or even the specific rows in this range. I only want to clear this range in the instances where there is a blank line above it (headers to data). The reason being: I am trying to keep the spreadsheet as generic as possible in order to use it again if the specific layout of rows changes based on the input file.


Solution

  • First, the variable lastRow2 doesn't seem to be declared, and as you don't get any errors, you obviously don't use Option Explicit. Please do, because that will warn you about such errors.

    Secondly, I don't see that you in any way initialize lastRow2, which explains why the loop is never run. Did you run the code in the debugger to verify values of variables and progress of the execution? That is the first thing to do when you see unexpected results.

    Thirdly, I don't understand why you have the condition and why you use offset If IsEmpty(Range(i, 1).Offset(-1, 0)) = True. Just clear the cells explicitly

    Try this instead:

    lastColumn = 26
    
    For i = lastRow To 1 Step -1
        Range(Cells(i, 2), Cells(i, lastColumn)).Clear
    Next
    

    edit:

    I noticed you have the last column as 25 (as well as the previous one) in the part where you concatenate the values from the cells. The correct last column is 26.


    edit2:

    Based on your edit of your question and assuming you have declared and initialized lastRow2 the corrected function would look like this:

    For i = lastRow2 To 2 Step -1
        If IsEmpty(Range(Cells(i, 1), Cells(i, 1)).Offset(-1, 0)) = True Then
            ActiveSheet.Range(Cells(i, 2), Cells(i, 4)).Clear
        End If
    Next