VBA - 循环解决变量

时间:2017-01-06 19:29:11

标签: excel vba excel-vba excel-2013

我正在使用VBA从SharePoint中提取excel书籍并将它们保存在指定的文件夹中,我有大约11位分析师需要提取他们的工作簿。我宁愿不复制/粘贴我使用11次的公式,所以我试图将他们的名字存储在变量中,然后使用循环。我似乎无法让它正常工作。

以下是我目前的样本:

    Sub DownloadTimeTrackers()
Dim i As Integer
Dim Name1 As String
Dim Name2 As String
Dim Name3 As String

Name1 = "Barb"
Name2 = "Alex"
Name3 = "Cole"

For i = 1 To 3 Step 1

Const strUrl As String = "mypath\Secured\Name(i) Time Tracker.xlsm"
Dim strSavePath As String
Dim returnValue As Long

 strSavePath = "C:\Users\lxys\Desktop\TimeTrack\Name(i) Time Tracker.xlsm"
 returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

Next I

End Sub

我排除了一些有助于从SP中提取报告的代码,但我认为这与此无关。

如果我将Name(i)替换为实际名称“Barb”,则该公式可以正常工作,并保存到我的桌面文件夹中。

如何让姓名(i)替代Barb,Alex,Cole等?我试过Namei,Name + i和Name(i)但没有运气。任何建议为什么这似乎没有正确解决?我没有任何错误。

修改

根据反馈,我将程序更改为以下内容,但收到“下标超出范围”错误:

    Sub DownloadTimeTrackers()
Dim i As Integer
Dim Name() As Variant
Dim strUrl As String

Name = Array("Barb", "Alex", "Cole", "Cim", "Adam", "Lane", "Kristin", "Mike", "Nicki", "George", "Lori")


For i = 1 To 11 Step 1

        strUrl = "\\collab.sfcollab.org@SSL\DavWWWRoot\sites\WSS005469\DisputeTeam\Secured\" & Name(i) & "Time Tracker.xlsm"

Dim strSavePath As String
Dim returnValue As Long

 strSavePath = "C:\Users\fip1\Desktop\TimeTrack\" & Name(i) & "Time Tracker.xlsm"
 returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

Next i



End Sub

我发现错误,我在“Time Tracker.xlsm”中没有空格

2 个答案:

答案 0 :(得分:4)

而不是

Dim Name1 As String
Dim Name2 As String
Dim Name3 As String

Name1 = "Barb"
Name2 = "Alex"
Name3 = "Cole"

使用

Dim Name() As Variant
Name = Array("Barb", "Alex", "Cole")

这会创建一个"数组"可以由变量i编制索引的名称。请注意,默认情况下,此数组将从零开始,即第一个位置为Name(0)

您还需要更改

Const strUrl As String = "mypath\Secured\Name(i) Time Tracker.xlsm"

Dim strUrl As String
strUrl = "mypath\Secured\" & Name(i) & " Time Tracker.xlsm"

(你不能拥有"常数"它有不同的值)

并改变

strSavePath = "C:\Users\lxys\Desktop\TimeTrack\Name(i) Time Tracker.xlsm"

strSavePath = "C:\Users\lxys\Desktop\TimeTrack\" & Name(i) & " Time Tracker.xlsm"

您的重构代码将是

Sub DownloadTimeTrackers()
    Dim strUrl As String
    Dim strSavePath As String
    Dim returnValue As Long
    Dim i As Integer
    Dim Name() As Variant

    Name = Array("Barb", "Alex", "Cole")

    For i = LBound(Name) To UBound(Name)

        strUrl = "mypath\Secured\" & Name(i) & " Time Tracker.xlsm"
        strSavePath = "C:\Users\lxys\Desktop\TimeTrack\" & Name(i) & " Time Tracker.xlsm"
        returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

    Next i

End Sub

(我也改变了你的循环,从数组的下限转到上限。这样你就可以为数组添加额外的名字,而不必担心更改For语句。)< / p>

答案 1 :(得分:2)

问题在于缺少数组和路径:

要遍历名称,您需要它们作为数组变量的一部分,而不是作为单独的字符串。

Dim Name() As String
Name = Array("Barb", "Alex", "Cole")

然后,您将路径定义为不变的变量:

Const strUrl As String = "mypath\Secured\Name(i) Time Tracker.xlsm"
strSavePath = "C:\Users\lxys\Desktop\TimeTrack\Name(i) Time Tracker.xlsm"

字符串总是:

  

&#34; mypath \ Secured \ Name(i)Time Tracker.xlsm&#34;

     

&#34; C:\ Users \ lxys \ Desktop \ TimeTrack \ Name(i)Time Tracker.xlsm&#34;

而不是循环遍历数组并评估为名称:

  

&#34; ...&#34;姓名(0)&#34; ...&#34;

     

&#34; ...&#34;姓名(1)&#34; ...&#34;

因为没有名称的迭代,因为 i 是文本字符串的一部分,而不是变量的实例化,它将为您提供数组的正确成员。你需要将它放在字符串之外并连接到它。

Const strUrl As String = "mypath\Secured\" & Name(i) & " Time Tracker.xlsm"

strSavePath = "C:\Users\lxys\Desktop\TimeTrack\"Name(i) & " Time Tracker.xlsm"