delete columns vba - but ignore 1st row

John Caines

Well-known Member
Joined
Aug 28, 2006
Messages
1,155
Office Version
  1. 2019
Platform
  1. Windows
Hello All.
I'm trying to find a vba solution to delete all empty (selected range) columns in a spreadsheet,,,but with a twist.
The spreadsheet columns have a header in Row A,,,so technically the column isn't completely empty.

So it's trying to write some vba code to delete all empty columns that have a header (in row A),,, and if nothing is in the column below A, delete the row.
I did find a vba code on the www. here;

Code was;
Code:
Public Sub DeleteEmptyColumns()
    Dim SourceRange As Range
    Dim EntireColumn As Range

    On Error Resume Next

    Set SourceRange = Application.InputBox( _
        "Select a range:", "Delete Empty Columns", _
        Application.Selection.Address, Type:=8)

    If Not (SourceRange Is Nothing) Then
        Application.ScreenUpdating = False

        For i = SourceRange.Columns.Count To 1 Step -1
            Set EntireColumn = SourceRange.Cells(1, i).EntireColumn
            If Application.WorksheetFunction.CountA(EntireColumn) = 0 Then
                EntireColumn.Delete
            End If
        Next

        Application.ScreenUpdating = True
    End If
End Sub

But the above code doesn't take into account the headings in row A.

Is there anyway to modify this code above so I can delete empty columns on a range I select (taking into account the selected range, you'd be ignoring the Row A

Hope the above makes sense and someone can help me out here.

Best regards
John C
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Change the zero to 1

VBA Code:
            If Application.WorksheetFunction.CountA(EntireColumn) = 1 Then
 
Upvote 0
Hello JohnnyL!
Many thanks for your reply.
I just tried the above formula with your suggestion, but it didn't work :-(
The first ROW (Row 1) that has the headings has comments in, but this shouldn't affect it from running correctly.
mr-excl-example-sheet.xlsx
ABCDEFGHI
1Country – DNS : HostCountry ISO Code – DNS : HostContinent – DNS : HostLinking C Blocks – Moz : Root DomainLinks – Moz : Root DomainExternal Links – Moz : Root DomainExternal Equity-Passing Links – Moz : Root DomainRoot Domains Linking – Moz : Root DomainTime Last Crawled – Moz : URL
2United StatesUSNorth America10/12/2018 1:00
3United StatesUSNorth America10/15/2018 1:00
4United StatesUSNorth America10/10/2018 1:00
5IndiaINAsia10/12/2018 1:00
6GermanyDEEurope11/7/2018 0:00
7IndiaINAsia10/11/2018 1:00
812/19/2018 0:00
9FranceFREurope12/12/2018 0:00
10United StatesUSNorth America10/1/2018 1:00
11United StatesUSNorth America10/10/2018 1:00
12GermanyDEEurope10/15/2018 1:00
13CanadaCANorth America10/3/2018 1:00
14United StatesUSNorth America10/6/2018 1:00
15United StatesUSNorth America8/4/2017 1:00
16United StatesUSNorth America8/24/2018 1:00
17CyprusCYEurope11/15/2018 0:00
18SingaporeSGAsia10/5/2018 1:00
19United StatesUSNorth America10/14/2018 1:00
20United StatesUSNorth America10/13/2018 1:00
21United StatesUSNorth America10/5/2018 1:00
22BulgariaBGEurope11/2/2018 0:00
23IndiaINAsia10/22/2018 1:00
24CanadaCANorth America10/8/2018 1:00
25United StatesUSNorth America10/17/2018 1:00
26United KingdomGBEurope11/3/2018 0:00
27United StatesUSNorth America10/9/2018 1:00
289/22/2018 1:00
29United StatesUSNorth America10/2/2018 1:00
30United StatesUSNorth America10/15/2018 1:00
31FranceFREurope1/15/2017 0:00
32SingaporeSGAsia8/10/2018 1:00
33BahrainBHAsia11/24/2018 0:00
34United StatesUSNorth America11/17/2018 0:00
35IndiaINAsia12/12/2018 0:00
36United KingdomGBEurope7/2/2018 1:00
37United StatesUSNorth America10/10/2018 1:00
38GermanyDEEurope10/13/2018 1:00
3910/17/2018 1:00
40United StatesUSNorth America10/21/2018 1:00
41United StatesUSNorth America12/17/2018 0:00
42United StatesUSNorth America10/27/2018 1:00
43United StatesUSNorth America11/12/2018 0:00
44NetherlandsNLEurope11/7/2018 0:00
45United StatesUSNorth America8/6/2018 1:00
46United StatesUSNorth America10/9/2018 1:00
47United StatesUSNorth America11/30/2017 0:00
48United StatesUSNorth America10/4/2018 1:00
4910/22/2018 1:00
50United StatesUSNorth America12/3/2018 0:00
51United StatesUSNorth America11/1/2018 0:00
529/27/2018 1:00
53United StatesUSNorth America12/11/2018 0:00
545/28/2018 1:00
55United StatesUSNorth America10/27/2018 1:00
56United StatesUSNorth America6/19/2017 1:00
57SingaporeSGAsia9/30/2018 1:00
Sheet1


I've just used Xl2bb to show you a sample,, where the macro/vba should delete columns D,E,F,G,H, but it doesn't JohnnyL
:-(
Any ideas why not?
Love to get this working.
Many thanks again for your reply

Best regards

John C
 
Upvote 0
Just tried it on your sample & it worked for me.

VBA Code:
Public Sub DeleteEmptyColumns()
    Dim SourceRange As Range
    Dim EntireColumn As Range

    On Error Resume Next

    Set SourceRange = Application.InputBox( _
        "Select a range:", "Delete Empty Columns", _
        Application.Selection.Address, Type:=8)

    If Not (SourceRange Is Nothing) Then
        Application.ScreenUpdating = False

        For i = SourceRange.Columns.Count To 1 Step -1
            Set EntireColumn = SourceRange.Cells(1, i).EntireColumn
            If Application.WorksheetFunction.CountA(EntireColumn) = 1 Then
                EntireColumn.Delete
            End If
        Next

        Application.ScreenUpdating = True
    End If
End Sub
 
Upvote 0
Hi JohnnyL

This is driving me mad!
I've just created a short screencam of what I have and it not working for me here
(It's a mp4 video I've just uploaded to my dropbox JohnnyL
video-for-johnnyl

If you please could just see if I'm doing something stupid here JohnnyL, I just can't get it to delete columns :(
Hope you can advise

Best regards
A very confused...
John C
 
Upvote 0
When you run the script and it asks for the range, you are selecting the entire range.

Just select the columns you want Example A1:H1 for example.
 
Upvote 0
Hi JohnnyL,
It's still not working for me! :-(
I include image JohnnyL.

Even though this is a sample sheet,,, the actual sheet may have like 100 columns, all with headers in row 1,,,I just want to be able to click a button and it deletes all the columns with no data.
I'm really not sure why this isn't working JohnnyL
here's another video, just to show you.
still-not-working-video-for-johnnyl

I really don't know what the issue is here Johnny.
Am I still doing it wrong in this video?
I can't be??

Cheers JohnnyL
Has to work soon! LOL
John C
 

Attachments

  • range.jpg
    range.jpg
    122.7 KB · Views: 30
Upvote 0
You did the same thing, effectively, as what you did in the last video. Perhaps that was because of some poor wording in my last response.

Let me try again.

Don't select the column letters. You should be selecting cells on Row 1. Example A1:H1
 
Upvote 0
I really don't know what the issue is here
I believe that it relates to your data and the fact that columns you think have nothing in them other than a header actually do have something else. At one point in your post #7 video you selected column D by clicking its heading label.
At that point I paused your video and although it is very hard to read, I believe that at the right of the status bar it says something like Count=37 indicating there is more than just a heading in that column.

Here is another code to try. See what happens with it.

VBA Code:
Sub Del_Cols()
  Dim c As Long, rw As Long
 
  Application.ScreenUpdating = False
  For c = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1
    rw = Columns(c).Find(What:="?*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    If rw = 1 Then Columns(c).Delete
  Next c
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi JohnnyL & Peter_SSs!
Many many thanks for both your replies.

1st of all, I'll upload my macro enabled workbook with the example data in (I have used in screencams below, )
example-workbook
Sorry JohnnyL, I just can't get it to work at all.
If you say exactly what cell range to select I'll try it again (IE A1:I1,,, or if it's $A1:$I1,,, whatever you say it is, I'll try it again,,), but I've tried a million combinations now! (a bit of an exaggeration, but it feels like it!) :-)... but maybe still not the right 1!

I think Peter_SSs might be onto something, as if I select for example (highlight) COL D, it shows a count in lower left of workbook of 57, even though nothing is in COL D below Row 1,, very strange?
Image attached.
Really not sure what is wrong here.

But moving on,,,many thanks again Peter_Sss for taking a look also and uploading your code.
I just tested it, and it worked!
It deleted the columns in sample sheet D:H

I just tried it in a bigger sheet with data in Columns A:FX
and it worked again! Brilliant! :)
This is a huge huge timesaver for me Peter_SSs

Is there anyway to tweak it just slightly?
It would be great for it to say after it has run something like,

Starting Column Count: 180
Deleted Columns: 49
Finishing Column Count: 131

This would be handy to know.
But I really grateful Peter_SSs for this, as I said, it worked 1st time, brilliant.

Again, sorry I couldn't get the 1st VBA code to run JohnnyL, I just don't know why it won't work for me.

I appreciate very much your replies.

Have a great day
Best regards
John C
 

Attachments

  • count-57.jpg
    count-57.jpg
    249.8 KB · Views: 24
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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