Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Newbee - deleting blank columns

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob
  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Newbee - deleting blank columns

Backup before testing. This will work on the activesheet, change if necessary.

Sub test()
Dim rCol As Range
Dim RngToDelete As Range

For Each rCol In ActiveSheet.UsedRange.Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub

"Rednosebob" wrote:

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Newbee - deleting blank columns

I want to pass a minimum and maximum column number to this subroutine
(MinCol, MaxCol). Is this possible? I'm not familar with ranges like I
should be. I appreciate your comments!

Bob

"JMB" wrote:

Backup before testing. This will work on the activesheet, change if necessary.

Sub test()
Dim rCol As Range
Dim RngToDelete As Range

For Each rCol In ActiveSheet.UsedRange.Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub

"Rednosebob" wrote:

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob

  #4   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Newbee - deleting blank columns

Pass Min and Max columns as parameters or user inputs? Also, the way it is
written, it will run on the active sheet, which can be selected prior to
running the code (to make it the active sheet) or the range references in the
subroutine will have to be fully qualified to identify specifically what
sheet it should run on (more on that at the end of the post). Also, I tested
it with both column numbers (1,2,3) and letters (A, B, C).

If as user inputs:

Sub test()
Dim rCol As Range
Dim MinCol As Variant
Dim MaxCol As Variant
Dim RngToDelete As Range

On Error Resume Next

MinCol = InputBox("Input Minimum Column")
MaxCol = InputBox("Input Maximum Column")

If IsNumeric(MinCol) Then
Set MinCol = Columns(CLng(MinCol))
Else: Set MinCol = Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Columns(CLng(MaxCol))
Else: Set MaxCol = Columns(MaxCol)
End If

If Not MinCol Is Nothing And _
Not MaxCol Is Nothing Then
For Each rCol In Range(MinCol.Cells(1, 1), _
MaxCol.Cells(Rows.Count, 1)).Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol
End If

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub


If parameters (not tested)

Sub test(MinCol As Variant, MaxCol As Variant)
Dim rCol As Range
Dim RngToDelete As Range

On Error Resume Next

'Delete these two lines
'MinCol = InputBox("Input Minimum Column")
'MaxCol = InputBox("Input Maximum Column")

Rest of code goes here......



If you need to qualify the worksheet then do it here - I used the codename
for the sheet (Sheet2), but you can also use the tab name with
Worksheets("YourSheetName") - I like using codename as it is less likely to
be changed, causing code to fail.

If IsNumeric(MinCol) Then
Set MinCol = Sheet2.Columns(CLng(MinCol))
Else: Set MinCol = Sheet2.Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Sheet2.Columns(CLng(MaxCol))
Else: Set MaxCol = Sheet2.Columns(MaxCol)
End If


"Rednosebob" wrote:

I want to pass a minimum and maximum column number to this subroutine
(MinCol, MaxCol). Is this possible? I'm not familar with ranges like I
should be. I appreciate your comments!

Bob

"JMB" wrote:

Backup before testing. This will work on the activesheet, change if necessary.

Sub test()
Dim rCol As Range
Dim RngToDelete As Range

For Each rCol In ActiveSheet.UsedRange.Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub

"Rednosebob" wrote:

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Newbee - deleting blank columns

I've used the later version where I can pass the parameters. It works
exactly like I wanted it to. Thanks for your help!

Bob

"JMB" wrote:

Pass Min and Max columns as parameters or user inputs? Also, the way it is
written, it will run on the active sheet, which can be selected prior to
running the code (to make it the active sheet) or the range references in the
subroutine will have to be fully qualified to identify specifically what
sheet it should run on (more on that at the end of the post). Also, I tested
it with both column numbers (1,2,3) and letters (A, B, C).

If as user inputs:

Sub test()
Dim rCol As Range
Dim MinCol As Variant
Dim MaxCol As Variant
Dim RngToDelete As Range

On Error Resume Next

MinCol = InputBox("Input Minimum Column")
MaxCol = InputBox("Input Maximum Column")

If IsNumeric(MinCol) Then
Set MinCol = Columns(CLng(MinCol))
Else: Set MinCol = Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Columns(CLng(MaxCol))
Else: Set MaxCol = Columns(MaxCol)
End If

If Not MinCol Is Nothing And _
Not MaxCol Is Nothing Then
For Each rCol In Range(MinCol.Cells(1, 1), _
MaxCol.Cells(Rows.Count, 1)).Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol
End If

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub


If parameters (not tested)

Sub test(MinCol As Variant, MaxCol As Variant)
Dim rCol As Range
Dim RngToDelete As Range

On Error Resume Next

'Delete these two lines
'MinCol = InputBox("Input Minimum Column")
'MaxCol = InputBox("Input Maximum Column")

Rest of code goes here......



If you need to qualify the worksheet then do it here - I used the codename
for the sheet (Sheet2), but you can also use the tab name with
Worksheets("YourSheetName") - I like using codename as it is less likely to
be changed, causing code to fail.

If IsNumeric(MinCol) Then
Set MinCol = Sheet2.Columns(CLng(MinCol))
Else: Set MinCol = Sheet2.Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Sheet2.Columns(CLng(MaxCol))
Else: Set MaxCol = Sheet2.Columns(MaxCol)
End If


"Rednosebob" wrote:

I want to pass a minimum and maximum column number to this subroutine
(MinCol, MaxCol). Is this possible? I'm not familar with ranges like I
should be. I appreciate your comments!

Bob

"JMB" wrote:

Backup before testing. This will work on the activesheet, change if necessary.

Sub test()
Dim rCol As Range
Dim RngToDelete As Range

For Each rCol In ActiveSheet.UsedRange.Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub

"Rednosebob" wrote:

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob



  #6   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Newbee - deleting blank columns

I'm glad that worked for you. Thanks for the feedback.


"Rednosebob" wrote:

I've used the later version where I can pass the parameters. It works
exactly like I wanted it to. Thanks for your help!

Bob

"JMB" wrote:

Pass Min and Max columns as parameters or user inputs? Also, the way it is
written, it will run on the active sheet, which can be selected prior to
running the code (to make it the active sheet) or the range references in the
subroutine will have to be fully qualified to identify specifically what
sheet it should run on (more on that at the end of the post). Also, I tested
it with both column numbers (1,2,3) and letters (A, B, C).

If as user inputs:

Sub test()
Dim rCol As Range
Dim MinCol As Variant
Dim MaxCol As Variant
Dim RngToDelete As Range

On Error Resume Next

MinCol = InputBox("Input Minimum Column")
MaxCol = InputBox("Input Maximum Column")

If IsNumeric(MinCol) Then
Set MinCol = Columns(CLng(MinCol))
Else: Set MinCol = Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Columns(CLng(MaxCol))
Else: Set MaxCol = Columns(MaxCol)
End If

If Not MinCol Is Nothing And _
Not MaxCol Is Nothing Then
For Each rCol In Range(MinCol.Cells(1, 1), _
MaxCol.Cells(Rows.Count, 1)).Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol
End If

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub


If parameters (not tested)

Sub test(MinCol As Variant, MaxCol As Variant)
Dim rCol As Range
Dim RngToDelete As Range

On Error Resume Next

'Delete these two lines
'MinCol = InputBox("Input Minimum Column")
'MaxCol = InputBox("Input Maximum Column")

Rest of code goes here......



If you need to qualify the worksheet then do it here - I used the codename
for the sheet (Sheet2), but you can also use the tab name with
Worksheets("YourSheetName") - I like using codename as it is less likely to
be changed, causing code to fail.

If IsNumeric(MinCol) Then
Set MinCol = Sheet2.Columns(CLng(MinCol))
Else: Set MinCol = Sheet2.Columns(MinCol)
End If

If IsNumeric(MaxCol) Then
Set MaxCol = Sheet2.Columns(CLng(MaxCol))
Else: Set MaxCol = Sheet2.Columns(MaxCol)
End If


"Rednosebob" wrote:

I want to pass a minimum and maximum column number to this subroutine
(MinCol, MaxCol). Is this possible? I'm not familar with ranges like I
should be. I appreciate your comments!

Bob

"JMB" wrote:

Backup before testing. This will work on the activesheet, change if necessary.

Sub test()
Dim rCol As Range
Dim RngToDelete As Range

For Each rCol In ActiveSheet.UsedRange.Columns
If Application.CountA(rCol) = 0 Then
If RngToDelete Is Nothing Then
Set RngToDelete = rCol
Else: Set RngToDelete = Union(RngToDelete, rCol)
End If
End If
Next rCol

If Not RngToDelete Is Nothing Then _
RngToDelete.EntireColumn.Delete

End Sub

"Rednosebob" wrote:

Excel 2000 - I have a spreadsheet that is laid out to have enough columns
formated for the maxium possible uaeage for this application. The number of
columns actually used will vary. I know the maximum columns used, but I need
a macro that will delete all blank columns.

Thanks in advance for your help!

Bob

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
reduce excel file size by deleting blank rows and columns?? Delta007bhd Excel Discussion (Misc queries) 5 April 4th 23 12:48 PM
Newbee made a big mistake [email protected] Excel Discussion (Misc queries) 2 July 12th 06 08:20 AM
Newbee needs HELP-Small Macro tbobo Excel Discussion (Misc queries) 4 March 8th 06 07:17 AM
newbee got a problem climax Excel Discussion (Misc queries) 2 January 30th 06 01:21 PM
Newbee help Goran[_2_] Excel Programming 2 April 16th 04 01:29 PM


All times are GMT +1. The time now is 03:00 PM.

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"