Need help deleting empty columns not cells

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,079
Office Version
  1. 365
Platform
  1. Windows
Hi I need help deleting Columns that have empty data in whole column. Not if there is empty cells. example if I select Column C D and E because all the way down that line has no data. Then I want to delete them so it shifts to the left.

I tried Find & Select Co to special then blanks and delete to try to record a code, but it doesn't do what I need it to do.
 
That is initially thought. Based on whatever column references you enter into the array, it will search and if there is nothing anywhere in that column, it will delete it and shift to the left.
For some reason, the second code doesn't seem to be working, but the first one I gave you does. You just need to update the array to include what columns you want to look at.
Or, if you want it to check ALL columns dynamically/automatically, let me know, and we can update it to do that.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Here is VBA code that does all columns automatically:
Code:
Sub MyDeleteColumns()

    Dim lastCol As Long
    Dim myCol As Long
    
'   Find last column on sheet
    lastCol = Range("A1").SpecialCells(xlLastCell).Column
    
'   Loop through each column backwards
    For myCol = lastCol To 1 Step -1
'       Check value in first row of current column
        If Cells(1, myCol) = "" Then
'           Go down, and see if it at last row and it is empty
            myRow = Cells(1, myCol).End(xlDown).Row
            If myRow = Rows.Count And Cells(myRow, myCol) = "" Then
                Columns(myCol).Delete
            End If
        End If
    Next myCol
            
End Sub
 
Upvote 0
Yeah this is it thank guys. appreciate all the help :) . Next time ill try to explain it right.
 
Upvote 0
Just an alternative but if your cells are truly blank and not an empty string from a formula can you use the counta function?

Code:
Sub DelColumns()

    Dim LstCol As Long, i As Long

    LstCol = Cells.Find(what:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
    For i = LstCol To 1 Step -1
        If WorksheetFunction.CountA(Columns(i)) = 0 Then Columns(i).Delete
    Next i

End Sub
 
Upvote 0
The columns that are empty are blank. Nothing at all inside cells
 
Upvote 0
Just an alternative but if your cells are truly blank and not an empty string from a formula can you use the counta function?
Nice! I like it. Short and sweet (where were you about an hour ago?;))
That is like something I was trying to come up with my second attempt, which didn't work out so well.
 
Last edited:
Upvote 0
Ok now I just did. Works the sameway & your code is so short in length.
 
Upvote 0
hahahah Yes this works short and sweet thankg guys for all the help.:)(y)
 
Upvote 0

Forum statistics

Threads
1,215,320
Messages
6,124,238
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top