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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,301
Office Version
  1. 365
Platform
  1. Windows
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
 

XLWiz

New Member
Joined
May 7, 2015
Messages
29
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.
 

Forum statistics

Threads
1,136,260
Messages
5,674,682
Members
419,520
Latest member
Jennifer4Dillon

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
Top