在假设将分配不必要的大量内存的情况下,我总是害怕将事物声明为变体.
最近我正在努力提高电子表格的性能,但却产生了相反的印象(见下面的编辑):Dim myarray() as Variant
相比之下,性能有所改善Dim myarray() as String
这两个声明的主要区别和后果是什么?
在这里找不到明确的指导:https://msdn.microsoft.com/en-us/library/aa711948.aspx
编辑:受控性能测试
我运行了受控性能测试(获取dim myarray() as Variant
版本,制作副本并更改两个变量Dim myarray() as String
)
正如你在下面看到我错了,性能差异并不显着.
Dim myarray() as Variant VERSION
Start 4:05:47 PM
FXLoaded 4:05:47 PM 00:00
TDLoaded 4:06:38 PM 00:51
LisofPCTD 4:06:57 PM 00:19
YDLoaded 4:07:47 PM 00:50
LisofPCYD 4:08:14 PM 00:27
PrintCoBTD 4:08:46 PM 00:32
PrintCoBYD 4:09:18 PM 00:32
Total 03:31 03:31
Dim myarray() as String VERSION
Start 4:25:53 PM
FXLoaded 4:25:53 PM 00:00 …
批量文件如下:
@echo off
set filelocation=C:\Users\myself\Documents\This&That
cd %filelocation%
echo %filelocation%
pause
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
'That' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
C:\Users\myself\Documents\This
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
考虑到我无法更改文件夹名称,我该如何处理"&"
下面的代码不考虑分组形状。有没有解决的办法?
Sub LoopThruShapes()
Dim sh As Shape
i=1
For Each sh In ActiveSheet.Shapes
Cells(i, 1).value = sh.name
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
来源:http : //www.java2s.com/Code/VBA-Excel-Access-Word/Excel/LoopingthroughaCollectionofShapes.htm
我正在尝试import
在 VBA 中输入 Python 代码。
下面的代码有效,但需要管理员权限。有没有办法绕过 win register 需求(假设我只是没有管理员权限)但保持“延迟投标”行为(每次我编译新东西时都不想工具>>参考)
class ProofOfConcept(object):
def __init__(self):
self.output = []
def GetData(self):
with open('C:\Users\MyPath\Documents\COMs\SourceData.txt') as FileObj:
for line in FileObj:
self.output.append(line)
return self.output
class COMProofOfConcept(object):
_reg_clsid_ = "{D25A5B2A-9544-4C07-8077-DB3611BE63E7}"
_reg_progid_= 'RiskTools.ProofOfConcept'
_public_methods_ = ['GetData']
def __init__(self):
self.__ProofOfConcept = ProofOfConcept()
def GetData(self):
return self.__ProofOfConcept.GetData()
if __name__=='__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(COMProofOfConcept)
Run Code Online (Sandbox Code Playgroud)
调用它的 VBA 代码:
Sub TestProofOfConcept()
Set PoF = CreateObject("RiskTools.ProofOfConcept")
x = PoF.GetData()
MsgBox x(0)
End Sub
Run Code Online (Sandbox Code Playgroud)