ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with VBA macro running inside VB (https://www.excelbanter.com/excel-programming/361792-problem-vba-macro-running-inside-vbulletin.html)

David Travers

Problem with VBA macro running inside VB
 
I have created a VB6 program which uses some Excel automation to generate an
Excel formatted document from a CSV file.

Inside Excel VBA running in Excel 2003, I have the following

Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Key2:=Range("F2") _
, Order2:=xlAscending, Key3:=Range("A2"), Order3:=xlAscending,
Header:=xlYes, OrderCustom:=1, MatchCase:=False _
, Orientation:=xlTopToBottom

however in the VB applciation i have

Dim xl as Excel.Application
Set xl = New Excel.Application

.....

xl.Selection.Sort xl.Range("D2"), xlAscending, xl.Range("F2"), xlAscending,
xl.Range("A2"), xlAscending, xlYes, 1, False, xlTopToBottom

but this doesn't work (get Sort Method of Range class failed)

Anyone got any ideas why not.

It's the only line of Excel code within the program that does not work.




Bob Phillips[_14_]

Problem with VBA macro running inside VB
 
Range is not a property of the Excel application, but of a worksheet. You
will need to step all the way down the object model

xl.workbook_object.worksheet_object.Range("D2")

for example, or better setup object variables for each part and build an
xlRange1 object that you can use, etc.

--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"David Travers" wrote in
message ...
I have created a VB6 program which uses some Excel automation to generate

an
Excel formatted document from a CSV file.

Inside Excel VBA running in Excel 2003, I have the following

Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Key2:=Range("F2") _
, Order2:=xlAscending, Key3:=Range("A2"), Order3:=xlAscending,
Header:=xlYes, OrderCustom:=1, MatchCase:=False _
, Orientation:=xlTopToBottom

however in the VB applciation i have

Dim xl as Excel.Application
Set xl = New Excel.Application

....

xl.Selection.Sort xl.Range("D2"), xlAscending, xl.Range("F2"),

xlAscending,
xl.Range("A2"), xlAscending, xlYes, 1, False, xlTopToBottom

but this doesn't work (get Sort Method of Range class failed)

Anyone got any ideas why not.

It's the only line of Excel code within the program that does not work.







All times are GMT +1. The time now is 02:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com