range method run time error 1004

Kuldeep662

New Member
Joined
Apr 26, 2012
Messages
2
Hi guys, i have small query regarding this and I havent found answer for this yet. Ofcourse I 'm new to this and not sure what do I do

I have following code which works

Range("F2 : F179").Select
Selection.Sort Key1:=Range("F2:F179"), Order1:=xlAscending _
, Order2:=xlYes, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom


Since I want to handle no. of rows dynamically for sorting i wrote following code...

Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
Range("F2 : F & LastRow &").Select
Selection.Sort Key1:=Range("F2:F179"), Order1:=xlAscending _
, Order2:=xlYes, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom

Replacing 179 with LastRow variable doesnt work. When i print value of LastRow it prints correctly.

I get Method Range of Object _WorkSheet failed error. .

When i change above code to..

Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
Worksheets("OverallITrac").Range("F2 : F & LastRow & ").Select
Selection.Sort Key1:=Range("F2:F & LastRow &"), Order1:=xlAscending _
, Order2:=xlYes, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom

Now I get Application defined or object defined error. PLease help. Thanks in advance.

Thanks,
Kuldeep
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to the Board!

The issue is that you have your variable LastRow in between your text qualifiers (double quotes). When you do that, it is treated as literal text and not a variable.

I have updated your code (and used a little different way to find the last row with data in column F). This code should work for you:
Code:
    Dim LastRow As Integer 'This is the LAST Non Empty Row in column F
    
    LastRow = Cells(Rows.Count, "F").End(xlUp).Row
    Worksheets("OverallITrac").Range("F2:F" & LastRow).Select
    Selection.Sort Key1:=Range("F2:F" & LastRow), Order1:=xlAscending _
        , Order2:=xlYes, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,737
Members
449,050
Latest member
excelknuckles

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