Optimise code and include a variable.

imcfcl

New Member
Joined
Sep 23, 2011
Messages
18
Afternoon All,

Hope all is well - I've got some code that I've made with macro recorder (still not comfortable enough to write my own) but it isn't fit for use. It clearly needs a variable but I dont know how.

Code:
    Range("F17").Select
    Selection.Copy
    Range("D17").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("D17:D114")
    Range("D17:D114").Select
    Range("C17:D17").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Add Key:=Range("D17:D114") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Home").Sort
        .SetRange Range("C17:D114")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
End Sub

In col C, starting at C17, I have a list such with entries like 'Door 12' 'Door 45' 'Door 198' 'Door 1' and I need to sort this data smallest to biggest - obviously excel can't sort this type of list properly - So I've written this formula to trim the numbers off so that It can

=IF(LEN($C17)=6,RIGHT($C17,1),IF(LEN($C17)=7,RIGHT($C17,2),RIGHT($C17,3))) and it works and I've stored it in Cell F17.

I want to paste this formula next to every entry in my list, and then sort by column D. My list could go from C17 to C100, or it could go from C17 to D999 ... it will keep growing and thats why I can't use my recorded macro.

Help appreciated - and a pint for anyone who ever visits a little county called Cheshire.
 

Excel Facts

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

Code:
Sub Test()
    Dim LastRow As Long
    LastRow = Range("C" & Rows.Count).End(xlUp).Row
    Range("F17").Copy Range("D17")
    Application.CutCopyMode = False
    Range("D17").AutoFill Destination:=Range("D17:D" & LastRow)
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Home").Sort.SortFields.Add Key:=Range("D17:D" & LastRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Home").Sort
        .SetRange Range("C17:D" & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

I removed the unnecessary Selects.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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