VBA中变量名后的可能后缀是什么?

时间:2011-02-10 14:17:27

标签: vba

正如我已经想到的那样,至少有六个:!@#$%&

这是剪辑:

Dim A!, B@, C#, D$, E%, F&
Debug.Print "A! - " & TypeName(A)
Debug.Print "B@ - " & TypeName(B)
Debug.Print "C# - " & TypeName(C)
Debug.Print "D$ - " & TypeName(D)
Debug.Print "E% - " & TypeName(E)
Debug.Print "F& - " & TypeName(F)

输出

A! - Single
B@ - Currency
C# - Double
D$ - String
E% - Integer
F& - Long

这种语法糖的文档在哪里?

还有哪些其他可能的后缀?

日期有吗?

3 个答案:

答案 0 :(得分:5)

这些后缀是类型提示,并且已接受答案中的链接已过时。

Dim someInteger% '% Equivalent to "As Integer"
Dim someLong&    '& Equivalent to "As Long"
Dim someDecimal@ '@ Equivalent to "As Currency"
Dim someSingle!  '! Equivalent to "As Single"
Dim someDouble#  '# Equivalent to "As Double"
Dim someString$  '$ Equivalent to "As String"

Dim someLongLong^  '^ Equivalent to "As LongLong" in 64-bit VBA hosts

所以除了^ LongLong之外,你已经拥有了它们,在VBA7中为64位主机应用程序引入了As。正是为什么微软为新的值类型引入了一个新类型提示的原因已经超出了我的范围。

它比语法糖更多语法中毒,并且在1000 REM INIT VARIABLES 1010 LET FIZZ$ = "FIZZ" 1011 LET BUZZ$ = "BUZZ" 1020 LET FIZZ% = 3 1021 LET BUZZ% = 5 1030 LET MIN% = 1 1031 LET MAX% = 15 1100 PRINT FIZZ$ + ":" + STR$(FIZZ%) 1101 PRINT BUZZ$ + ":" + STR$(BUZZ%) 1102 PRINT FIZZ$ + BUZZ$ + ":" + STR$(FIZZ%*BUZZ%) 1105 PRINT 子句成为事物之前,从祖先的恐龙版BASIC一直追溯到日期,例如在这个Commodore 64 BASIC 2.0 fizzbuzz代码中:

Rem

正如您所看到的,键入提示并不是VBA支持的唯一古代代码:行号,Let注释和显式Debug.Print TypeName(32&) 'prints Long Debug.Print TypeName(CLng(32)) 'prints Long 值分配在1982年也是一件事。避免它们不惜一切代价。

在文字中,更喜欢显式转换而不是类型提示:

# My main idea is to copy the file which is not in right order,
# and rename it, then delete it.

import os, re, shutil


# Arguments: folder, prefix
def fillGap(folder,prefix):


    # Create regex with prefix + number + extension.
    fileRegex = re.compile(r'(%s)((\d)(\d)(\d))\.txt' % prefix)

    # Makee sure the path is absolute.
    folder = os.path.abspath(folder)

    # When I commented the following one line, the program outptu is
    # FileNotFoundError: [Errno 2] No such file or directory:   'spam004.txt'
    os.chdir(folder)  # This line is to avoid the FileNotFoundError.

    # Make a list to contain the file with prefix.
    fileNames = list()
    for filename in os.listdir(folder):
        if re.search(fileRegex, filename):
            fileNames.append(filename)
    # Make sure the fileName in the list have a right order.

    fileNames.sort()
    print(fileNames)
    # Find the gap through incremting loops
    for i in range(len(fileNames)):
        mo = re.search(fileRegex, fileNames[i])
        if int(mo.group(2)) == i + 1:
            continue
        # The group(2) has three digits, so it need to 3 Ifs.
        # Copy the old file and rename it then delete the old one.
        if i + 1 < 10:
            shutil.copy
            newFileName =prefix + '00' + str(i + 1) + '.txt'
            shutil.copy(fileNames[i], newFileName)
            os.unlink(fileNames[i])
        elif i + 1 < 100:
            shutil.copy(fileNames[i], prefix + '0' + str(i+1) + '.txt')
            os.unlink(fileNames[i])
        else:
            shutil.copy(fileNames[i], prefix + str(i+1) + '.txt')
            os.unlink(fileNames[i])

folder = '/home/jianjun/spam/'
prefix = 'spam'

fillGap(folder, prefix)
  

问自己不是能否,问问自己应该 - 未知

答案 1 :(得分:4)

完整(?)列表位于变量和函数名称前缀下的http://support.microsoft.com/kb/110264。正如Remou所说 - 他们不被推荐(文章说使用是“气馁”)。我相信你在列表中都覆盖了它们。日期在技术上是一种变体(存储为浮点),因此没有该快捷方式。

答案 2 :(得分:3)

这些很久以来一直被弃用,仅用于向后兼容AFAIK。将变量声明为所需类型。您也可以强制变量进行输入。