Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default marco to delete empty columns

Hello. I need to make a macro that will delete any column in my excel
worksheet that is empty. I am not sure wich columns are empty so it will need
to check each one. I would also like it to ignore the first row because every
column has a header so the macro would not work if it checked the first row.
Does anyone have any ideas for a macro? I tryed to make one to do it today
but just ended up very frustrated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default marco to delete empty columns

Hi,

Right click your sheet tab, view code and paste this in and run it

Sub sonic()
lastcolumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For x = 1 To lastcolumn
If Cells(Rows.Count, x).End(xlUp).Row = 1 Then
Columns(x).EntireColumn.Delete
End If
Next

End Sub


Mike

" wrote:

Hello. I need to make a macro that will delete any column in my excel
worksheet that is empty. I am not sure wich columns are empty so it will need
to check each one. I would also like it to ignore the first row because every
column has a header so the macro would not work if it checked the first row.
Does anyone have any ideas for a macro? I tryed to make one to do it today
but just ended up very frustrated.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default marco to delete empty columns

OOPS,

My mistake you have to do it backwards

Sub sonic()
lastcolumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For x = lastcolumn To 1 Step -1
rr = Cells(Rows.Count, x).End(xlUp).Row
If Cells(Rows.Count, x).End(xlUp).Row = 1 Then
Columns(x).EntireColumn.Delete
End If
Next

End Sub

Mike

"Mike H" wrote:

Hi,

Right click your sheet tab, view code and paste this in and run it

Sub sonic()
lastcolumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For x = 1 To lastcolumn
If Cells(Rows.Count, x).End(xlUp).Row = 1 Then
Columns(x).EntireColumn.Delete
End If
Next

End Sub


Mike

" wrote:

Hello. I need to make a macro that will delete any column in my excel
worksheet that is empty. I am not sure wich columns are empty so it will need
to check each one. I would also like it to ignore the first row because every
column has a header so the macro would not work if it checked the first row.
Does anyone have any ideas for a macro? I tryed to make one to do it today
but just ended up very frustrated.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default marco to delete empty columns

Since you didn't use the rr variable anywhere, you can remove this
assignment line from the code...

rr = Cells(Rows.Count, x).End(xlUp).Row

--
Rick (MVP - Excel)


"Mike H" wrote in message
...
OOPS,

My mistake you have to do it backwards

Sub sonic()
lastcolumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For x = lastcolumn To 1 Step -1
rr = Cells(Rows.Count, x).End(xlUp).Row
If Cells(Rows.Count, x).End(xlUp).Row = 1 Then
Columns(x).EntireColumn.Delete
End If
Next

End Sub

Mike

"Mike H" wrote:

Hi,

Right click your sheet tab, view code and paste this in and run it

Sub sonic()
lastcolumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For x = 1 To lastcolumn
If Cells(Rows.Count, x).End(xlUp).Row = 1 Then
Columns(x).EntireColumn.Delete
End If
Next

End Sub


Mike

" wrote:

Hello. I need to make a macro that will delete any column in my excel
worksheet that is empty. I am not sure wich columns are empty so it
will need
to check each one. I would also like it to ignore the first row because
every
column has a header so the macro would not work if it checked the first
row.
Does anyone have any ideas for a macro? I tryed to make one to do it
today
but just ended up very frustrated.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
marco to delete empty columns Mike H Excel Programming 0 February 17th 09 09:37 PM
How to delete at once empty columns in a table? capxc Excel Discussion (Misc queries) 1 July 19th 08 08:28 PM
Delete rows that are empty across columns ALATL Excel Worksheet Functions 0 November 6th 06 04:09 AM
Macro to Delete empty columns Darth Cor Excel Programming 1 June 2nd 05 02:08 PM
Delete empty array columns RB Smissaert Excel Programming 18 August 22nd 04 08:41 PM


All times are GMT +1. The time now is 04:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"