Sort in Ascending order and Delete other columns using VBA

XLWiz

New Member
Joined
May 7, 2015
Messages
29
Hello everyone,

I'm new to VBA development and am having a problem getting the below code to work properly. I need for it to sort column Y in ascending order and delete other columns I don't need. Here is what I have so far:

Sub SortAndDeleteRows()
Range("Y2").CurrentRegion.Select
Selection.Sort Key1:=Range("Y2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
On Error GoTo 0
ActiveSheet.AutoFilterMode = False
Columns("B:F").EntireColumn.Delete
Columns("L:Q").EntireColumn.Delete
Columns("U:V").EntireColumn.Delete
Application.ScreenUpdating = True
End With Sub

Any help would be appreciated. Thank in advance.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi XLWiz,

you're having "a problem"? A bit more description would help a big deal solving it. On what line of code does your code crash and what's the error message you're getting? What I can see happening in your code straight away: if you're deleting column B:F, everything that is to the right of that will move left (column G will become B, etc), so your code probably won't delete the bits you want to delete.

Koen
 
Upvote 0
Hi XLWiz,

you're having "a problem"? A bit more description would help a big deal solving it. On what line of code does your code crash and what's the error message you're getting? What I can see happening in your code straight away: if you're deleting column B:F, everything that is to the right of that will move left (column G will become B, etc), so your code probably won't delete the bits you want to delete.

Koen

Koen,

I'm pretty certain I described my problem fairly clearly--A: I'm new to writing code and it isn't working properly and, B: how I need my code to function. If you cannot provide me with assistance, then you've become another part of the problem, and not the solution.

I'd appreciate if you didn't respond further unless you had an actual sample of corrected code, rather than criticism.
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,034
Members
449,061
Latest member
TheRealJoaquin

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