在VBA中相当于SQL“%”

VBA中是否有任何SQL等效的“%”符号? 我需要返回几个文件,只是中间有一些字符。

帮助真的很感激!

例如这里是我的代码:我需要从该网页下载名称为2013的所有文件,并保存并以不同的方式调用它们。 这个任务可能吗?

Sub Sample() Dim strURL As String Dim strPath As String Dim i As Integer strURL = "http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2013.pdf" strPath = "C:\Documents and Settings\ee28118\Desktop\178.pdf" Ret = URLDownloadToFile(0, strURL, strPath, 0, 0) If Ret = 0 Then MsgBox "File successfully downloaded" Else MsgBox "Unable to download the file" End If End Sub 

当您导航到上传文件夹时,您将获得其中所有文件的目录列表。 你可以遍历该列表上的超链接,并testing每个超链接是否符合你的标准,如果是的话,下载它。 您需要对MSXML和MSHTML的引用。 这是一个例子。

 Sub Sample() Dim sUrl As String Dim xHttp As MSXML2.XMLHTTP Dim hDoc As MSHTML.HTMLDocument Dim hAnchor As MSHTML.HTMLAnchorElement Dim Ret As Long Dim sPath As String Dim i As Long sPath = "C:\Documents and Settings\ee28118\Desktop\" sUrl = "http://cetatenie.just.ro/wp-content/uploads/" 'Get the directory listing Set xHttp = New MSXML2.XMLHTTP xHttp.Open "GET", sUrl xHttp.send 'Wait for the page to load Do Until xHttp.readyState = 4 DoEvents Loop 'Put the page in an HTML document Set hDoc = New MSHTML.HTMLDocument hDoc.body.innerHTML = xHttp.responseText 'Loop through the hyperlinks on the directory listing For i = 0 To hDoc.getElementsByTagName("a").Length - 1 Set hAnchor = hDoc.getElementsByTagName("a").Item(i) 'test the pathname to see if it matches your pattern If hAnchor.pathname Like "Ordin-*.2013.pdf" Then Ret = UrlDownloadToFile(0, sUrl & hAnchor.pathname, sPath, 0, 0) If Ret = 0 Then Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath Else Debug.Print sUrl & hAnchor.pathname & " not downloaded" End If End If Next i End Sub 

编辑

我认为URLDownloadToFile已经写好了。 我没有写一个,我只是用下面的函数来testing遍历文件的代码。 您可以使用它来确保上面的代码适用于您,但是您需要编写实际的代码以最终下载文件。 有了URLDownloadToFile的所有参数,我很惊讶它不存在。

 Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, lNum1 As Long, lNum2 As Long) As Long UrlDownloadToFile = 0 End Function 

您可以使用Like运算符。

模式中的字符匹配string

 ? Any single character. * Zero or more characters. # Any single digit (0–9). [charlist] Any single character in charlist. [!charlist] Any single character not in charlist 

例如:

 Dim MyCheck MyCheck = "aBBBa" Like "a*a" ' Returns True. MyCheck = "F" Like "[AZ]" ' Returns True. MyCheck = "F" Like "[!AZ]" ' Returns False. MyCheck = "a2a" Like "a#a" ' Returns True. MyCheck = "aM5b" Like "a[LP]#[!ce]" ' Returns True. MyCheck = "BAT123khg" Like "B?T*" ' Returns True. MyCheck = "CAT123khg" Like "B?T*" ' Returns False. 

尝试下面的代码:如果string中包含string2013 ,布尔函数将返回true。

 Sub Sample() Dim result As Boolean result = has2013("http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2013.pdf") Debug.Print result result = has2013("http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2014.pdf") Debug.Print result End Sub Function has2013(lnk As String) As Boolean has2013 = lnk Like "*2013*" End Function 

在VBA中使用带有通配符的LIKE函数:

这里是一个例子(从Ozgrid论坛复制)

 Dim sht As Worksheet For Each sht In ActiveWorkbook.Worksheets If sht.Name Like "FRI*" Then 'Add code for Friday sheets Else If sht.Name Like "MON*" Then 'Add code for Monday sheets End If End If Next 

乘法字符*代替零个或多个字符,而 代替1个字符, 代替1个数字。 还有其他更具体的字符。 匹配策略,如果你只想匹配某些字符。

所以你去!

另外,你可以看看Ozgrid论坛:在VBA中使用正则expression式

要获取服务器上的文件列表,请阅读Mr Excel中的 FTP(使用DIR) – 使用FTP列出文件