Solved

VBA Excel ?  - How do i identify empty columns

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now