Deleting columns - faster vba code?

CarlosTapas

New Member
Joined
Sep 27, 2011
Messages
2
Hi (Long time reader first time poster)
As part of a Macro I export a range of cells that I calculate.
I export range A19:DW242 again and again so that it becomes a long dataset of about 70,000+ rows in a seperate sheet..
A then need to “clean” this data by deleting columns that are not required.
However this takes more time than I would like and would welcome some ideas on speeding this relatively simple task up.
The columns to be kept are identified in row 4 when the cell contains “report”
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Sub deletecolumns()
Dim ginger As Integer
Dim pizza As Integer
Application.ScreenUpdating = False
With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
<o:p></o:p>
Sheets("Output").Select
pizza = 1
For ginger = 1 To 160
If Cells(4, pizza).Value <> "Report" Then
Columns(pizza).Delete
Else
pizza = pizza + 1
End If
Next ginger
With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
<o:p></o:p>
Application.ScreenUpdating = True
End Sub


Thank you for you assistance!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
you name your counter variable pizza?? Weird :biggrin:

The rule of thumb is to work backwards when deleting rows or columns, in my experience that works faster, so change your loop to count backwards (use 'Step -1' at the end of the For line). Take care to not mess up the counter, often it's easier to work with a While/Wend.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top