Any way to make it better?

hitbid

Board Regular
Joined
Jan 21, 2016
Messages
114
Just a quick question,

Since I've been learning arrays I've tried to interject them into places where I can.

For example here, I am deleting certain columns based on their column number, pre-set in an array.

Code:
Dim cols As Variant
Dim counter As Double
Dim Top As Double


cols = Array(8, 5, 3)
Top = UBound(cols)


For counter = 0 To Top
    Columns(cols(counter)).Delete
Next counter

How could this code be even simpler or easier? I just want to learn how to fry a fish differently.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can try this:
Code:
Sub Columns_Delete()
'Modified  7/15/2018  9:03:32 PM  EDT
Range("C1,E1,H1").EntireColumn.Delete
End Sub
 
Upvote 0
maybe:
Code:
Sub Easier()
Range("C:C,E:E,H:H").Delete
End Sub
 
Upvote 0
Thx. I think I've gone loop crazy.
But, if you did want to use a loop, this is a way to simplify the code you posted earlier...
Code:
Dim V As Variant
For Each V In Array(8, 5, 3)
  Columns(V).Delete
Next
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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