Compile Error: Expected End With

aggiemarine07

New Member
Joined
Nov 5, 2013
Messages
46
Howdy! I am definitetly a novice at VBA and I am hoping someone with more VBA skills than myself can spot the error below. I keep getting a "Compile Error: Expected End With" error. This code is supposed to auto sort data in a few columns as information gets added. Here is the code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Range("P5:Y156").Select
ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Add Key:=Range( _
        "P5:P156"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Add Key:=Range( _
        "R5:R156"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("COMBINED").Sort
        .SetRange Range("P4:Y156")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
End Sub

Any help would be greatly appreciated. Thanks!
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Range("P5:Y156").Select
ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Add Key:=Range( _
"P5:P156"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("COMBINED").Sort.SortFields.Add Key:=Range( _
"R5:R156"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("COMBINED").Sort
    .SetRange Range("P4:Y156")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub
 
Upvote 0
Whenever you have a "With" statement like "With ActiveWorkbook.Worksheets("COMBINED").Sort" you need to pair it with an "End With" The "End With" should be right after the ".Apply"

The With statement is a shortcut and you need to tell the compiler when you are done using that shortcut.
 
Upvote 0

Forum statistics

Threads
1,215,515
Messages
6,125,279
Members
449,220
Latest member
Excel Master

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