VBA从文件名每天不断变化的URL下载文件

有人可以帮我,因为我拉我的头发。 我试图从网站下载文件并将其复制到另一个位置。 如果我使用下面注释的实际文件名称,它的作品。 可悲的是,文件名称每天都在变化,我已经在B1单元(01 04 2016)中进行了说明。 无论如何,我收到错误“编译错误:所需的常量expression式”,并突出显示第一个“&”。 有没有人有任何想法,可能有帮助?

{

Option Explicit Private Declare Function URLDownloadToFile Lib "urlmon" Alias _ "URLDownloadToFileA" ( _ ByVal pCaller As Long, ByVal szURL As String, _ ByVal szFileName As String, _ ByVal dwReserved As Long, _ ByVal lpfnCB As Long) As Long `enter code here`Sub DownloadFileFromWeb() Dim i As Integer Const strUrl As String = "https://test.com/sites/Field Completions Reported " & Range("B1") & ".xlsb" '"https://test.com/sites/Field Completions Reported 01 04 2016.xlsb" Dim strSavePath As String Dim returnValue As Long strSavePath = "C:\Users\1234\Desktop\Field Completions Reported.xlsb" returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0) End Sub 

}

编译错误:需要常量expression式

你不能在一个Variable使用Const 。 当你使用Const然后传递一些不会改变的东西。 例如

 Const a = "Sid" '<~~~ This will work Const a = "Sid" & i '<~~ This will not work. i is a variable Const a = "Sid" & Range("A1").Value '<~~ This will not work. Range("A1") is a variable 

将您的代码更改为此

 Dim strUrl As String '"https://test.com/sites/Field Completions Reported 01 04 2016.xlsb" strUrl = "https://test.com/sites/Field Completions Reported " & _ Range("B1").Value & ".xlsb"