Solved

VBA Excel ?  - How do i identify empty columns

Posted on 2000-04-20
4
148 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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