简体   繁体   English

使用 VBA 从另一个程序运行 Excel

[英]Running Excel using VBA from another program

I am using vba in a CAD program to export data, sort the data, and add data.我在 CAD 程序中使用 vba 来导出数据、对数据进行排序和添加数据。 The following macro is exactly what I want excel to do.下面的宏正是我想要 excel 做的。 However I believe I am limited to having the CAD program tell Excel what to do through VBA.但是我相信我仅限于让 CAD 程序通过 VBA 告诉 Excel 做什么。 This macro copyies a formula and pastes it to all the populated cells below it in the column.此宏复制一个公式并将其粘贴到列中它下方的所有填充单元格中。

MACRO CODE:宏代码:

Range("B1").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste

iLogic Version of Code:代码的 iLogic 版本:

oBook.WorkSheets(1).Name = "Order List"
oBook.WorkSheets(2).Name = "Cut List"
wSheet1 = oBook.WorkSheets("Order List")
wSheet2 = oBook.WorkSheets("Cut List")

wSheet2.Activate
wSheet2.Range("B1").Select
wSheet2.Selection.Copy
wSheet2.Range(Selection, Selection.End(xlDown)).Select
wSheet2.Selection.Paste

Unfortunately I seem to be missing something to translate between Inventor and Excel, but I don't know enough to even know if that's the issue.不幸的是,我似乎缺少在 Inventor 和 Excel 之间进行翻译的内容,但我什至不知道这是否是问题所在。 Any advice is very much appreciated as I am still very new to VBA.非常感谢任何建议,因为我对 VBA 还是很陌生。

Ok.好的。 I had some code that I had copied off of a forum but didn't understand the functions going on.我有一些从论坛上复制的代码,但不了解正在运行的功能。 I believe this is what you are referring to "Application"?我相信这就是您所说的“应用程序”?

wSheet2.Columns("G:G").select()
oExcelApp.Selection.cut()
wSheet2.Columns("B:B").Select()
oExcelApp.Selection.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)

Although I don't understand what the "(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)" refers to.虽然我不明白“(Microsoft.Office.Interop.Excel.XlDirection.xlToRight)”指的是什么。 I understand the part about the direction and where it inserts just not whats before it.我了解有关方向及其插入位置的部分,而不是它之前的内容。

I do agree with @DisGruntledDraftsman about not using Select and Activate.我同意@DisGruntledDraftsman 关于不使用 Select 和激活的观点。 However, if you are just trying to get some working code that gets the job done, some simple tweaks can be done.但是,如果您只是想获得一些可以完成工作的工作代码,则可以进行一些简单的调整。 Of course this isn't ideal but, it should work though.当然,这并不理想,但它应该可以工作。

row_Count = wSheet2.Range("B1048576").End(xlUp).Row
wSheet2.Activate
wSheet2.Range("B1").Select
Selection.AutoFill Destination:=wSheet2.Range("B1:B" & row_Count), Type:=xlFillDefault

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM