Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default Delete Columns For A Range

I'm trying to delete columns (not rows) within Row 1, that contain a blank
cell. I have examples of looping through the sheet, but not a specific row.
The below deletes rows within a range. Can someone modify it for columns?


Sub DeleteEmptyColumnRange()
Dim DelRange As Range
Dim c As Range
For Each c In ActiveSheet.Range("A1:AK1").Cells
If c.Value = 0 Then
If DelRange Is Nothing Then
Set DelRange = c.EntireRow
Else
Set DelRange = Union(DelRange, c.EntireRow)
End If
End If
Next c
'turn on error handling in case no range is assigned

On Error Resume Next
DelRange.Delete
On Error GoTo 0

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Delete Columns For A Range

Sub DeleteEmptyColumnRange()
Dim DelRange As Range
Dim c As Range
For Each c In ActiveSheet.Range("A1:AK1").Cells
If isempty(c.Value) Then
If DelRange Is Nothing Then
Set DelRange = c.EntireColumn
Else
Set DelRange = Union(DelRange, c.EntireColumn)
End If
End If
Next c
'turn on error handling in case no range is assigned

On Error Resume Next
DelRange.Delete
On Error GoTo 0

End Sub

or

Sub DeleteColumnForBlankCellRow1()
dim rng as Range
On Error Resume Next
set rng = ActiveSheet.Range("A1:AK1").SpecialCells(xlBlanks)
On Error goto 0
if not rng is nothing then
rng.Entirecolumn.Delete
End if
End sub

--
Regards,
Tom Ogilvy


"scott" wrote in message
...
I'm trying to delete columns (not rows) within Row 1, that contain a blank
cell. I have examples of looping through the sheet, but not a specific

row.
The below deletes rows within a range. Can someone modify it for columns?


Sub DeleteEmptyColumnRange()
Dim DelRange As Range
Dim c As Range
For Each c In ActiveSheet.Range("A1:AK1").Cells
If c.Value = 0 Then
If DelRange Is Nothing Then
Set DelRange = c.EntireRow
Else
Set DelRange = Union(DelRange, c.EntireRow)
End If
End If
Next c
'turn on error handling in case no range is assigned

On Error Resume Next
DelRange.Delete
On Error GoTo 0

End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Delete Columns For A Range

Hi Scott,
This worked for me:

Sub DelColumn()
r = 1 ' r is row number
For i = 37 To 1 Step -1 ' column numbers, work from right to left
If Cells(r, i) = "" Then Columns(i).Delete Shift:=xlToLeft
Next i

End Sub

HTH

Andrew Bourke



scott wrote:
I'm trying to delete columns (not rows) within Row 1, that contain a blank
cell. I have examples of looping through the sheet, but not a specific row.
The below deletes rows within a range. Can someone modify it for columns?


Sub DeleteEmptyColumnRange()
Dim DelRange As Range
Dim c As Range
For Each c In ActiveSheet.Range("A1:AK1").Cells
If c.Value = 0 Then
If DelRange Is Nothing Then
Set DelRange = c.EntireRow
Else
Set DelRange = Union(DelRange, c.EntireRow)
End If
End If
Next c
'turn on error handling in case no range is assigned

On Error Resume Next
DelRange.Delete
On Error GoTo 0

End Sub


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
How to Delete blanks between a range and populate only the names inthe given range Yuvraj Excel Discussion (Misc queries) 2 November 4th 09 08:32 PM
Delete columns of a cell range Brettjg Excel Discussion (Misc queries) 2 April 25th 07 11:26 AM
merge text from 2 columns into 1 then delete the old 2 columns sleepindogg Excel Worksheet Functions 4 March 30th 06 07:25 PM
How to Delete a Range in Closed Workbook (to Replace Delete Query) [email protected] Excel Discussion (Misc queries) 1 March 8th 06 10:10 AM
Delete columns Elsie Excel Programming 1 February 28th 04 03:08 AM


All times are GMT +1. The time now is 02:23 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"