Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 397
Default Deleting evey nth column

HI there,

A tough one I think...

I would like to keep column A,B,C.
Then delete Column D,E,F, keep column G
Then delete column h,i,j keep column K
Then delete column l,m,n keep column O, etc etc till I run out of
columns.

I was previously helped with a SIMILAR problem (the movement of the
number of columns was different and the VB below worked for me, however
I dont know how to change it to achieve what I want to achieve above.
Any ideas????

Sub go_delete()
Dim tmp As Integer
Dim tmp2 As Integer
Dim tmp_address As String, row_count As Single

With ActiveSheet.UsedRange
tmp = Fix((.Columns.Count - 5) / 4)
If (.Columns.Count - 5) Mod 4 0 Then
tmp = tmp + 1
End If
tmp_address = .Address(False, False)
row_count = .Rows.Count
End With
With ActiveSheet.Range(tmp_address)
With .Offset(0, 5).Resize(row_count, .Columns.Count - 5)
For tmp2 = tmp To 1 Step -1
With .Columns(4 * tmp2 - 3)
.Resize(row_count, 2).EntireColumn.Delete
End With
Next
End With
End With
End Sub




*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default Deleting evey nth column

Sub go_delete()
Dim tmp As Range
Dim i As Long

With ActiveSheet
Set tmp = .Range("D1:F1")
For i = 8 To .Columns.Count - 2
Set tmp = Union(.Cells(1, i).Resize(, 2), tmp)
Next i
End With

tmp.EntireColumn.Delete

End Sub



--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Darin Kramer" wrote in message
...
HI there,

A tough one I think...

I would like to keep column A,B,C.
Then delete Column D,E,F, keep column G
Then delete column h,i,j keep column K
Then delete column l,m,n keep column O, etc etc till I run out of
columns.

I was previously helped with a SIMILAR problem (the movement of the
number of columns was different and the VB below worked for me, however
I dont know how to change it to achieve what I want to achieve above.
Any ideas????

Sub go_delete()
Dim tmp As Integer
Dim tmp2 As Integer
Dim tmp_address As String, row_count As Single

With ActiveSheet.UsedRange
tmp = Fix((.Columns.Count - 5) / 4)
If (.Columns.Count - 5) Mod 4 0 Then
tmp = tmp + 1
End If
tmp_address = .Address(False, False)
row_count = .Rows.Count
End With
With ActiveSheet.Range(tmp_address)
With .Offset(0, 5).Resize(row_count, .Columns.Count - 5)
For tmp2 = tmp To 1 Step -1
With .Columns(4 * tmp2 - 3)
.Resize(row_count, 2).EntireColumn.Delete
End With
Next
End With
End With
End Sub




*** Sent via Developersdex http://www.developersdex.com ***



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 397
Default Deleting evey nth column

Thanks Bob - unfortunately it doesnt work...
I changed my range from d1:f1 to say d1:ax1, but all it did was delete
everything except for column a,b, and c... am I doing something wrong?
do I need to change the i...?

Regards

D



*** Sent via Developersdex http://www.developersdex.com ***
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default Deleting evey nth column

The D1:F1 was just to initialise the tmp range.

You should use

Sub go_delete()
Dim tmp As Range
Dim i As Long

With ActiveSheet
Set tmp = .Range("D1:F1")
For i = 8 To .Range("AX1").Column - 2
Set tmp = Union(.Cells(1, i).Resize(, 2), tmp)
Next i
End With

tmp.EntireColumn.Delete

End Sub



--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Darin Kramer" wrote in message
...
Thanks Bob - unfortunately it doesnt work...
I changed my range from d1:f1 to say d1:ax1, but all it did was delete
everything except for column a,b, and c... am I doing something wrong?
do I need to change the i...?

Regards

D



*** Sent via Developersdex http://www.developersdex.com ***



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 397
Default Deleting evey nth column

Bob I guess my explanation of the problem wasnt clear enought, because I
know you would easily be able to solve it :) (and have done so in the
past!)

Basically Macro is still not working it works for the first instance, ie
it correctly deletes columns D,E and F (and now G is next to C), but
thats all it does, from H to AI nothing happens. (Does it matter that
H,L,P, etc are blank..?)
I need to delete H,I,J
Keep K
Delete L,M,N
Keep O
Delete P,Q,r
Keep S
etc

Appreciate your time and effort


*** Sent via Developersdex http://www.developersdex.com ***


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Deleting evey nth column

Try this and let me know how you get on. It's flexible in that you can
alter the number of columns to delete and the number of columns to
skip. There is some redundancy in looping till 256 but it's still
quick and I am sure you could easily optimise it.

Sub Test()

DeleteNthColumns ThisWorkbook.Worksheets(1), 4, 3, 1

End Sub

Sub DeleteNthColumns(sht As Excel.Worksheet, lngFirstColumnToDelete As
Long, lngColumnsToDelete As Long, lngColumnsToKeep As Long)

Dim lngColCurrent As Long
Dim lngDeleteCounter As Long

'starting on the first column to delete
lngColCurrent = lngFirstColumnToDelete

Do

For lngDeleteCounter = 1 To lngColumnsToDelete

'delete as many columns as specified
sht.Columns(lngColCurrent).Delete

Next lngDeleteCounter

'then skip as many columns as specified
lngColCurrent = lngColCurrent + lngColumnsToKeep

Loop While lngColCurrent <= 256 'only 256 columns in an excel sheet

End Sub

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default Deleting evey nth column

No, my bad, you explained it fine

Try this alternative

Sub go_delete()
Dim tmp As Range
Dim i As Long

With ActiveSheet
Set tmp = .Range("D1:F1")
For i = 8 To .Range("AX1").Column - 2 Step 4
Set tmp = Union(.Cells(1, i).Resize(, 3), tmp)
Next i
End With

tmp.EntireColumn.Delete

End Sub



--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Darin Kramer" wrote in message
...
Bob I guess my explanation of the problem wasnt clear enought, because I
know you would easily be able to solve it :) (and have done so in the
past!)

Basically Macro is still not working it works for the first instance, ie
it correctly deletes columns D,E and F (and now G is next to C), but
thats all it does, from H to AI nothing happens. (Does it matter that
H,L,P, etc are blank..?)
I need to delete H,I,J
Keep K
Delete L,M,N
Keep O
Delete P,Q,r
Keep S
etc

Appreciate your time and effort


*** Sent via Developersdex http://www.developersdex.com ***



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Deleting evey nth column


If it's always those columns this should work
Sub go_delete(
Range("A1").Selec
Dim iColumn As Intege
iColumn =
D
Range(Cells(1, iColumn), Cells(1, iColumn
2).Address).EntireColumn.Delet
iColumn = iColumn +
Loop Until iColumn = 25
End Su

-Thanks Bob - unfortunately it doesnt work..
I changed my range from d1:f1 to say d1:ax1, but all it did was delet
everything except for column a,b, and c... am I doing something wrong
do I need to change the i...

Regard





--
Ikaabo
-----------------------------------------------------------------------
Ikaabod's Profile: http://www.excelforum.com/member.php...fo&userid=3337
View this thread: http://www.excelforum.com/showthread.php?threadid=54106

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 397
Default Deleting evey nth column



Thanks! This one works!!!

*** Sent via Developersdex http://www.developersdex.com ***
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Deleting evey nth column

Have you got it working then? It should just work

I created a sub "DeleteNthColumns" that takes in paremeters of the
worksheet you want to perform the action on, the first column number to
delete (D=4), the number of columns to delete and the number of columns
to keep. The "Test" sub calls that procedure on the first worksheet
starting at column d, deleting three columns and keeping one.



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 can make a macro to run evey one hour-automatically SAM SEBAIHI Excel Discussion (Misc queries) 2 January 30th 07 04:12 AM
Auto Refresh evey ?? in a linked list Graham Excel Discussion (Misc queries) 0 July 27th 06 12:06 PM
Deleting cells in a column ref a different column Daminc[_23_] Excel Programming 8 December 23rd 05 05:04 PM
Deleting a row by a column value DKY[_17_] Excel Programming 2 October 8th 04 01:33 AM
Deleting a row with 0 in column A Steven R. Berke Excel Programming 1 October 2nd 03 01:37 AM


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

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

About Us

"It's about Microsoft Excel"