deselecting cells

WayneAli

New Member
Joined
Jun 9, 2015
Messages
11
Hi all,
As you're possibly aware, I'm fairly new to VBA but learning every day.

I have a couple of forms, one searches and clears a record/row and another that adds a record/row as lastrow + 1, once either of these have executed I have a few lines of code (below) that sort the sheet, firstly by custom order on column B, then alphabetically on column C.

My problem is that the Sub is ending with rows 3 to 22 selected when I click submit on both forms (both forms have these same lines of code at the end). I have tried various methods of selecting Cell A1 before EndSub but all are resulting in an error. I'm sure the issue is to do with the first line of code below - Rows("3:22").Select as the entirety of rows 3 to 22 are remaining selected.

Here is my code, hopefully someone more knowledgeable that me can throw in a line that deselects rows 3 to 22 and either results in (preferably) nothing being selected or cell A1 being selected.

Many thanks
VBA Code:
   Rows("3:22").Select
    ActiveWorkbook.Worksheets("DataSheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("DataSheet1").Sort.SortFields.Add2 Key:=Range( _
        "B3").End(xlDown), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
        "WM,CM,FF", DataOption:=xlSortNormal

    ActiveWorkbook.Worksheets("DataSheet1").Sort.SortFields.Add2 Key:=Range( _
        "C3").End(xlDown), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("DataSheet1").Sort
        .SetRange Range("A3:BA22")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Unload Me
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try....
VBA Code:
With Worksheets("DataSheet1")
  .Activate
  .Cells(1, 1).Select
End With
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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