简体   繁体   English

在AHK中运行VBA的字符串

[英]Run String of VBA within AHK

I am creating an AHK script to manipulate an excel spreadsheet which necessitates the use of VBA. 我正在创建一个AHK脚本来操作excel电子表格,这需要使用VBA。 I know how to use single lines of VBA within AHK by converting them to COM calls (like Xl.Worksheets(1).ShowAllData ), but what I need to do now is run a series of commands. 我知道如何通过将它们转换为COM调用(如Xl.Worksheets(1).ShowAllData )在AHK中使用单行VBA,但我现在需要做的是运行一系列命令。

Worksheets(1).ShowAllData
With Intersect(Columns("G"), ActiveSheet.UsedRange)
 .Replace "0*", "#N/A", xlPart
 .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
End With

I tried simply placing Xl (my COM object handle) in front of various sections of this code, and it didn't help. 我试过简单地将Xl(我的COM对象句柄)放在此代码的各个部分之前,但它没有帮助。 Like this: 像这样:

#j::
 Xl.Worksheets(1).ShowAllData
 Xl.With Intersect(Columns("G"), ActiveSheet.UsedRange)
  Xl.Replace "0*", "#N/A", xlPart
  Xl.SpecialCells(xlConstants, xlErrors).EntireRow.Delete
 Xl.End With
Return

AHK seems to have troubles with more complicated VBA commands. AHK似乎有更复杂的VBA命令的麻烦。 For instance, I can create a new sheet, with Xl.Worksheets.Add , but if I try to add a sheet and name it, like this: Xl.Worksheets.Add().Name = "MySheet" it freaks out. 例如,我可以使用Xl.Worksheets.Add创建一个新工作表,但是如果我尝试添加工作表并将其命名,就像这样: Xl.Worksheets.Add().Name = "MySheet"Xl.Worksheets.Add().Name = "MySheet"了。

What am I missing? 我错过了什么? Thanks!! 谢谢!! Paul 保罗

With is a compiling statement. With是一个编译语句。 Using it or not using it doesn't change what your program does, though using it makes it a bit faster. 使用它或不使用它不会改变程序的功能,但使用它会使它更快一些。

Intersect(Columns("G"), ActiveSheet.UsedRange).Replace "0*", "#N/A", xlPart
Intersect(Columns("G"), ActiveSheet.UsedRange).SpecialCells(xlConstants, xlErrors).EntireRow.Delete

Above is same code as yours without using With . 以上是与您相同的代码而不使用With

Saying you are missing the constants is a stupid thing to say. 说你缺少常数是一个愚蠢的说法。 You do not get a special version of Excel different to everyone else. 您没有获得与其他人不同的特殊版本的Excel。 Cause they are there. 因为他们在那里。 Use SEARCH in Object Browser. 在对象浏览器中使用SEARCH

PS: With is part of VBA. PS: With是VBA的一部分。 Everything else you are doing has nothing to do with VBA but with Excel's Object. 你正在做的其他事情与VBA无关,而是与Excel的对象无关。 The two are quite separate. 这两者是完全分开的。 You are NOT writing VBA code but using Excel via COM in your language. 您不是在编写VBA代码,而是使用您的语言通过COM使用Excel。 VBA has nothing to do with it which is why with won't work. VBA无关,用它做这就是为什么with是行不通的。

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

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