Excel Macro Trouble

dmonney

Registered User.
Local time
Today, 16:05
Joined
Dec 3, 2007
Messages
31
I am having trouble with a macro code that is run from access onto a worksheet in Excel. The part I'm having trouble with is when I try to apply a sort. I get a runtime 91 error. SRange does work to select the right area. the error comes on the sort

PHP:
Set xlapp = CreateObject("Excel.Application")
    xlapp.Application.Visible = True
   
    Set xlwkb = xlapp.Workbooks.Open(sfile)

*****************lots of Macro code that already works is here*******
 
xlapp.ActiveSheet.Range(SRange).Select
 
xlapp.Selection.Sort Key1:=ActiveSheet.Range("H1"), _ 
Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

*****************more Macro code that already works is here*******
 
Just use:

Code:
SRange.Sort Key1:=ActiveSheet.Range("H1"), _  
Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _ 
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

If you want to keep selecting things (this is completely unnecessary) you would use:

Code:
xlapp.ActiveSheet.SRange.Select

At some point you will have something along the lines of:

Code:
set SRange = thisworkbook.worksheets("Sheet1").range("A1:A4")

So with your original code:

Code:
xlapp.ActiveSheet.Range(SRange).Select

This translates to:

Code:
xlapp.activesheet.range(thisworkbook.worksheets("Sheet1").range("A1:A4"))

And thats why it didnt work.
 
What did you have SRange declared as? I had it as a string
ex: SRange = "A1:P151"
 
I figured it out. SRange is declared as a range
PHP:
    Set SortRange = ActiveSheet.Range("A1", "O773")

      SortRange.Sort Key1:=ActiveSheet.Range("H1"), _
        Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
        MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Thanks a bunch
 
I would have declared srange as a "range".

To get this working for you you need:

Code:
SRange = """A1:P151"""
 

Users who are viewing this thread

Back
Top Bottom