2

我试图掌握在*编码时何时可以和不能使用泛化。

我的意思的一个例子是xfile.*扩展名是否无关紧要。另一个是*.xls如果我想引用任何和所有 excel 文件。

我不只是对文件感兴趣。我想使用类似的东西Washington*Oregon*如果我想要工作簿中的所有工作表,无论之后发生什么,无论是This MonthThis Year等等。

另一个级别是电子表格单元格中的值。

我特别要求其中的每一个,因为*在每种情况下似乎都受到不同的对待。

谢谢您的帮助。


编辑:

我刚刚遇到的一个问题的一个很好的例子是这段代码:

If ActiveSheet.Name <> "City*" Then
    code
End If

工作表名称的选项是City MTDCity YTDCountry MTDCountry YTD(本月至今和年初至今,仅供参考)

我正在使用的工作表是,City MTD但我的程序仍然进入 If 语句。这使我相信它*没有被视为通配符,而是字符串中的字面星号。

4

2 回答 2

1

以下是*访问名称符合特定模式的所有工作表的方法:

Sub test()
    Dim ws As Worksheet
    Dim count As Long

    For Each ws In ActiveWorkbook.Sheets
        If LCase(ws.Name) Like "*data*" Then count = count + 1
    Next ws
    Debug.Print "There are " & count & " sheets with 'data' in their name"

End Sub

当我在一个工作簿上运行它时,该工作簿有一张名为“原始数据”和另一个“已处理数据”的工作表(以及其他一些在其中任何地方不包含“数据”的工作表),我得到:

There are 2 sheets with 'data' in their name

*可用于许多目的,但有些有限。对于更复杂的问题,建议使用 VBScript 的正则表达式对象——它也可以在 VBA 中使用(如果你在项目中添加了正确的引用)。

于 2015-08-18T17:23:47.470 回答
0

列出为* 通配符找到的方法

.

Range .Replace Method: UsedRange.Replace "test*", "NewValue"

Range .AutoFilter: Range("A:A").AutoFilter Field:=1, Criteria1:="test*"

Like Operator (compares strings): If Range("A1") Like "test*" Then

Files and Folders Methods:
    Copy
    CopyFile
    CopyFolder
    MoveFile
    MoveFolder
    DeleteFolder
    DeleteFile
    Dir Function (searches for files or folders)
    ChDir Statement (changes current folder)
    Kill Statement (deletes files from disk)

Application Methods
    .GetSaveAsFilename (used for file extension only)
    .GetOpenFilename (used for file extension only)
    .Match "test*", Range("A:A"), 0 '(If match_type is 0 and lookup_value is text)

WorksheetFunction Methods:
    .AverageIf and .AverageIfs
    .CountIf and .CountIfs
    .Find and .FindB (Range("A1").Find "*")
    .Match
    .Search and .SearchB (locate one text string within a 2nd string)
    .SumIf and .SumIfs
    .VLookup and .HLookup

FileDialog Object - .InitialFileName Property

VBScript.RegExp (or reference to "Microsoft VBScript Regular Expressions *")

Scripting.FileSystemObject CopyFile and DeleteFile Methods ("Microsoft Scripting Runtime")

.

它可以用波浪号 (~) 字符转义Range("A1").Find "~*"finds * ( .Find "~~"finds ~)

.

于 2015-08-18T17:24:00.027 回答