Solved

VBA Excel ?  - How do i identify empty columns

Posted on 2000-04-20
4
149 Views
Last Modified: 2011-09-20
I need to figure out what the first column is in spreadsheets that has no data. I think I can remember how to find the last active cell, but what I need is the first column which has no data in any cell in that column.

What is the least brute force method to do this?
0
Comment
Question by:quantz
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
calacuccia earned 50 total points
ID: 2736694
Hi Quantz,

Closely related to your question in Win95 area..

What about the code I posted in that question ?

Sub FindEmptyColumns()
Dim EmptyRange As Range, Col As Range
Set EmptyRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks)
For Each Col In ActiveSheet.UsedRange.Columns
If Intersect(Col, EmptyRange).Rows.Count = Col.Rows.Count Then
MsgBox ("Column " & Col.Column & " is empty !")
End If
Next Col
End Sub

To get stop code at first empty column, change to:
Sub FindEmptyColumns()
Dim EmptyRange As Range, Col As Range
Set EmptyRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks)
For Each Col In ActiveSheet.UsedRange.Columns
If Intersect(Col, EmptyRange).Rows.Count = Col.Rows.Count Then
MsgBox ("Column " & Col.Column & " is the first empty column in the used range!")
Exit Sub
End If
Next Col
End Sub

Calacuccia
0
 

Author Comment

by:quantz
ID: 2738024
This looked like it would work as is.  But it failed when the text file I "imported" to format had no empty cells in it before the end of the file. It gave me an error message "No empty cells".  So I got rid of the "special cells" part of the code, just selecting "Used Range" and put an integer as a counter in the "For each Col in " loop.  Works fine.
0
 

Author Comment

by:quantz
ID: 2738030
Calacuccia:
Hmm, so thats what happened to my first posting.... ended up in Win5...
Thanks
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2738134
Here's the URL for your win95 question. I suppose you'll want to delete that one.

http://www1.experts-exchange.com/Computers/Operating_Systems/Windows/Win95/Q_10331770.html

Regarding your problem, you could also use this little modified code (traps the error message 'No empty cells found', displays a little message box and ends the procedure)

Sub FindEmptyColumns()
Dim EmptyRange As Range, Col As Range
On Error GoTo ErrorHandler
Set EmptyRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
For Each Col In ActiveSheet.UsedRange.Columns
If Intersect(Col, EmptyRange).Rows.Count = Col.Rows.Count Then
MsgBox ("Column " & Col.Column & " is empty !")
End If
Next Col
ErrorHandler:
MsgBox ("No empty columns in the used range of your spreadsheet !")
End Sub

Cheers
Calacuccia
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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