Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL";%&引用;VBA中的等价物_Vba_Excel - Fatal编程技术网

SQL";%&引用;VBA中的等价物

SQL";%&引用;VBA中的等价物,vba,excel,Vba,Excel,VBA中是否有“%”符号的SQL等价物? 我需要返回一些文件,只是中间有一些字符。 非常感谢你的帮助 例如,这里是我的代码:我需要从该网页下载名称为2013的所有文件,并保存它们并以不同的方式调用它们。这个任务可能吗 Sub Sample() Dim strURL As String Dim strPath As String Dim i As Integer strURL = "http://cetatenie.just.ro/wp-content/uploa

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

您可以使用Like操作符

模式中的字符与字符串中的字符匹配

? 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 "[A-Z]"    ' Returns True.
MyCheck = "F" Like "[!A-Z]"    ' Returns False.
MyCheck = "a2a" Like "a#a"    ' Returns True.
MyCheck = "aM5b" Like "a[L-P]#[!c-e]"    ' Returns True.
MyCheck = "BAT123khg" Like "B?T*"    ' Returns True.
MyCheck = "CAT123khg" Like "B?T*"    ' Returns False.

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

下面是一个示例(复制自)

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

好了

另外,你可以看看


要获取服务器上的文件列表,请在FTP(使用DIR)上读取以下代码:如果字符串中包含字符串2013,则布尔函数将返回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

当您导航到uploads文件夹时,您将获得其中所有文件的目录列表。您可以循环浏览该列表上的超链接,并测试每个超链接是否符合您的标准,如果符合,请下载。您需要对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已经编写好了。我没有写一个,我只是使用下面的函数来测试遍历文件的代码。您可以使用它来确保上面的代码适合您,但最终需要编写实际的代码来下载文件。对于URLDownloadToFile的所有参数,我很惊讶它还不存在

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

    UrlDownloadToFile = 0

End Function

我可以用它从一个网页上下载更多的文件吗?使用这种方法,这些文件的名称中只有日期不同?例如,strURL=“*********.2013.pdf”有疑问,我不知道你是否可以在FTP服务器上进行通配符文件搜索?也许你还可以得到关于我上面添加的代码的提示。谢谢!我怀疑这会起作用,您需要使用通配符搜索服务器/FTP服务器上的所有可用文件,然后通过它们循环加载每个文件谢谢,但我对这方面非常熟悉,我可以要求您提供有关MSXML和MSHTML参考的提示吗?我该怎么办?非常感谢。好的,我想我成功地安装了这两个,但后来我得到了错误:“Sub或Function not defined”突出显示(URLDownloadToFile)和宏的名称。。。有什么建议吗?URLDownloadToFile来自您的代码。我假设您有下载给定URL的文件的方法,并且您已经创建了一个名为URLDownloadToFile的函数来完成此操作。现在,添加一个具有该名称的函数(请参见“编辑我的答案”),然后,如果您不知道如何通过vba下载文件,请开始一个新问题。@user2087878不要针对一个问题发布多个问题。我没有意识到URLDownloadToFile是一个win API函数。我回答这个问题是为了解决这个问题。我不认为这是一个问题,他们在两个问题,虽然这是一个更好的标题“如何迭代通过一个匹配模式的web文件夹中的文件”
Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, lNum1 As Long, lNum2 As Long) As Long

    UrlDownloadToFile = 0

End Function