VBA:何时可用?

我试图得到一个什么时候能够把握,不能使用*在编码时进行概括。

我的意思是xfile.*一个例子xfile.*如果扩展名不重要。 另一个将是*.xls如果我想引用任何和所有的Excel文件。

我不只是对文件感兴趣。 如果我想在工作簿中使用所有工作表,不pipe后来是什么,比如This MonthThis Year等,我想使用Washington*Oregon*这样的东西。

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

我特别要求每一种情况,因为*在每种情况下似乎都被区别对待。

谢谢您的帮助。


编辑:

我刚刚碰到的一个问题的一个很好的例子是在这个代码中:

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

工作表名称的选项是City MTDCity YTDCountry MTDCountry YTD (月份到date和年份Country YTD ,fyi)

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

以下是如何使用*来访问名称符合特定模式的所有表单:

 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 

*对于许多目的是有用的,但是有一定的限制。 对于更复杂的问题,build议使用VBScript的正则expression式对象 – 这也可以在VBA中使用(如果将正确的引用添加到项目中)。

列出为通配符find的方法

 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 *( Range("A1").Find "~*" .Find "~~" finds〜)

Interesting Posts