Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sort Excel Rows in Alphabetical Order

26 REPLIES 26
SOLVED
Reply
Message 1 of 27
C_Haines_ENG
957 Views, 26 Replies

Sort Excel Rows in Alphabetical Order

Hello all!

 

I cant seem to get this one to work even with the hyper lengthly documentation online. It seems it may not be possible to do this as the sorting method requires a rather random formatting to get the items to sort.

 

Range("A1:A12").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo

 

You can see beside the the Range.Sort there is a space, and the a key for what row to sort items by. I dont believe Inventors version of VBA supports this. Is there a way to get around this and has anyone successfully done this? Even previous posts of this topic were never anwsered. 

 

Thanks!

26 REPLIES 26
Message 2 of 27

Afternoon,

 

I did a little digging and here is the solution i found.

 

first ensure this reference file is checked. Located under the Tools menu bar in the VBA Editor See attached Image1_SortExcelRows & Image2_ExcelSortList_References_ScreenShot.

 

This will enable you to access the object library from Excel.

 

You where on the right track, please see the code below and let me know what you think. if this solves your issue please like the post and mark as a solution.

 

As always please reach out with further questions or concerns.

 

Sub SortExcelRange()

'set ref to workbook

Dim oWorkbook As Workbook
Set oWorkbook = GetObject("YOUR FILE PATH HERE WITH FILE NAME AND EXTENSION")

 

'set ref to active sheet

Dim oActiveSheet As WorkSheet
Set oActiveSheet = oWorkbook.Sheets.Item(1)

 

'adjust range as needed, i used b2 as the column to sort

oActiveSheet.Range("a1:c12").Sort Key1:=oActiveSheet.Range("b2"), order1:=xlAscending


End Sub

 

 

 

Message 3 of 27

Are you editing the Inventor VBA code in the VBA editor and not in the built in one? Would you be able to paste the "Imports" code so I can add it to my inventor? Doesnt work without that it seems. 

Message 4 of 27

yes i'm running it in the Inventor VBA, do you have the Microsoft Excel Reference file checked? you should not need to import anything.

Message 5 of 27

AMHumiston_0-1721074471711.png

Ensure this box is checked, it might be at the top of your list.

 

it is found here:

AMHumiston_1-1721074504456.png

 

 

Message 6 of 27

Okay so I did find it, but it still doesnt work. "Method arguments must be enclosed in parentheses." and "'Range' is a type and cannot be used as an expression."

Message 7 of 27

is that error coming from the sort line?

Message 8 of 27

Here are the reference files i have selected:

AMHumiston_0-1721128865025.png

 

Message 9 of 27

The method argument one yes. 

Message 10 of 27

please ensure your references match mine. that might be an issue.

Message 11 of 27

Do I have to run the script in the VBA Editor? Can these references not be imported into the default editor?

Message 12 of 27

It is setup to run in a VBA editor.

 

are  you using ilogic?

Message 13 of 27

Yes. Is iLogic.

Message 14 of 27

Is this problem not possible in iLogic? I was under the impression that iLogic and VBA were nearly identical minus some formatting. 

Message 15 of 27

i have it working in VBA, its a matter of getting access to the same properties in ilogic. i typically use VBA for this as its easier to format inside vba.

 

let me toy around with ilogic and see what i can come up with.

Message 16 of 27

i have it working in VBA, its a matter of getting access to the same properties in ilogic. i typically use VBA for this as its easier to format inside vba.

 

let

Message 17 of 27

Any luck with converting it to iLogic? I fear lack of formatting options in iLogic wont allow you to specify the parameters for how the sort should be done. 

Message 18 of 27

that is what i am running into. we use a lot of excel sheets to drive our cad models and certain features i haven't figured out how to get access from ilogic.

Message 19 of 27

I can get you started, this is why I was mentioning imports earlier.

 

Imports Microsoft.Office.Interop 'To use excel 
Imports Microsoft.Office.Interop.Excel 'To use excel
AddReference "Microsoft.Office.Interop.Excel" 'To use excel 
BOMPath = "C:\BOM Calculator\PART_BOM.xlsx" 'FINAL DESTINATION OF BOM RESULTS

'[ CREATE EXCEL APPLICATION, INSERT HEADER, AND CLOSE PREVIOUS INSTANCE IF STILL OPEN]

Try : ExcelAPP = GetObject(BOMPath) : ExcelAPP.Save : ExcelAPP.Application.Quit : My.Computer.FileSystem.DeleteFile(BOMPath) : Catch : End Try

ExcelAPP = CreateObject("Excel.Application") : ExcelAPP.Visible = True: ExcelAPP.DisplayAlerts = False

'AppActivate(ExcelAPP)
ExcelWB = ExcelAPP.Workbooks.Add
ExcelWB.SaveAs(BOMPath)

With ExcelAPP
	
	.Range("A1") = "TEST"
	
End With

ExcelAPP = Nothing
	
Message 20 of 27

let me tinker with this!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report