Solved

Delete blank columns

Posted on 2012-04-10
9
238 Views
Last Modified: 2012-04-11
Need to delete all blank columns in a sheet..

I get a sheet routinely that has no more than 20 columns of data but there are often blank columns between populated columns and it's not always the same column that is blank. I'm already running a macro to change fonts, colors, etc. so I am looking for some code to add to the VB macro that would also delete those pesky blank columns.

Thanks in advance,

swjtx99
0
Comment
Question by:swjtx99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37827128
Sub DeleteEmptyCols()
    Dim LastCol As Long, i As Long
    LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Columns
    For i = LastCol To 1 Step -1
        If WorksheetFunction.CountA(Columns(i)) = 0 Then
            Columns(i).EntireColumn.Delete
        End If
    Next i
End Sub
0
 

Author Comment

by:swjtx99
ID: 37827216
Thanks for the fast reply.

There is an error. Attached example sheet with above macro inserted.

Thanks,

swjtx99
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37827276
where is the sheet attached?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:swjtx99
ID: 37827358
I'll try that again. I attached it but it didn't upload.
Delete-blank-columns.xlsm
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37827449
is your heading of the columns fixed.. i mean you will know the heading for the columns which are not empty?
0
 

Author Comment

by:swjtx99
ID: 37827474
Yes, the column headings will always be the same name but not always in the same place.
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 37828291
Change

ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Columns

to

ActiveSheet.UsedRange.Columns.Count
0
 

Author Closing Comment

by:swjtx99
ID: 37832194
Thanks. Works great. Also found this after some late night googling. Yours is less lines so not sure what the difference is but they both work.

   
Sub Step_21_DeltBlnkCol()

Sheets("Gage").Select

Range("A1").Select
C = ActiveCell.Address
D = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

'Keep going through all the columns that contain data

While ActiveCell.Column <= D

' Check to see if the column is empty

  If WorksheetFunction.CountA(Selection.EntireColumn) = 0 Then

'   If it is empty delete it

    Selection.EntireColumn.Delete

'   Decrement the number of columns that contain data as one was just deleted

    D = D - 1
  Else

'   If is is not empty go to the next column to test it

    ActiveCell.Offset(0, 1).Select
  End If
Wend
Ender:

'Go back to original starting place

ActiveSheet.Range(C).Select

'End Sub
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37832906
You're welcome but I think you should 'Request Attention' so that you can give Anuroopsundd at least half the points since all I did was to correct one line in his solution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel vba question 7 39
Excel Data Validation Help 9 42
How to always round a decimal up 5 19
Display Excel gridlines when called from Access 7 9
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question