Excel宏:使用列A单元格值作为该行的下载文件的前缀

时间:2013-06-29 18:52:34

标签: excel excel-vba excel-2010 vba

这个宏使用在C和D列中找到的多行来从Web下载我需要的文件。但是,它会将单元格A1的值添加为每个下载的文件名的前缀。

我需要帮助修改我的宏,以便不使用Cell $ A $ 1作为每个下载文件的前缀,我希望它使用同一行的“A”列中的值。

Dim URL As String
Dim LocalFileName As String
Dim B As Boolean
Dim ErrorText As String
Dim C As Range
Dim sTxt As String
sTxt = Cells(1, "a").Value
Dim Lastrow As Long

Lastrow = Range("C:D").Find("*", , , , xlByRows, xlPrevious).Row
For Each C In Range("C1:D" & Lastrow)
    If Len(C) > 0 Then
        URL = C.Text
'        LocalFileName = "C:\downloaded\" & Range("A1") & " " & Evaluate("TRIM(RIGHT(SUBSTITUTE(""" & C.Text & """,""/"",REPT("" "",1000)),1000))")
        B = DownloadFile(UrlFileName:=URL, _
                         DestinationFileName:=LocalFileName, _
                         Overwrite:=DoNotOverwrite, _
                         ErrorText:=ErrorText)
        If B = True Then
            Debug.Print "Download successful"
        Else
            Debug.Print "Download unsuccessful: " & ErrorText
        End If
    End If
Next C
End Sub

示例数据:

    A   B   C   D
1   12345   Record1 http://www.test.com/picture1.jpg    http://www.test.com/file1.pdf
2   abcde   Record2 http://www.test.com/Picture2.jpg    http://www.test.com/file2.pdf

在上面的示例中,所需的文件名将是: -12345 picture1.jpg -12345 file1.pdf -abcde picture2.jpg -abcde file2.pdf

任何?

1 个答案:

答案 0 :(得分:0)

只需使用C.Row代替1

LocalFileName = "C:\downloaded\" & Range("A" & C.Row) & " " & Evaluate("TRIM(RIGHT(SUBSTITUTE(""" & C.Text & """,""/"",REPT("" "",1000)),1000))")