VBA技巧(一)

1.获取当前路径

ActiveWorkbook.Path

 

2.创建文件

Set FS = CreateObject("Scripting.FileSystemObject")
Set table = FS.CreateTextFile(ActiveWorkbook.Path + "/CreateTables.sql", True)

 

table.writeline("SET ANSI_NULLS ON")

 

3.转义符

MsgBox Chr$(34) & "123" & Chr$(34) _
         & Chr$(13) & Chr$(39) & "123" & Chr$(39)
       
MsgBox "<?xml version=""1.0"" encoding=""utf-8"" ?>"
       
'Chr$(13) 换行
'Chr$(34) ""
'Chr$(39) "

 

4.类型定义

Dim a, b As Integer

以上定义变量a被定义为variant型,b则被定义为integer型

 

当在把a当作integer型使用时,就会报“ByRef 参数类型不符”的错误

 

在vba中定义变量时如变量名后无 as 类型,则统统被定义为variant型

 

正确的命名方法是

dim a as integer,b as integer

dim a as integer

dim b as integer

你可能感兴趣的:(VBA技巧(一))