[英]Excel Macro: Use Column A Cell Value as prefix of downloaded file for that row
This macro downloads the files I need from the web using a url found in columns C and D for multiple rows. 此宏使用C列和D列中多个行的URL从Web下载我需要的文件。 However, it adds the value of cell A1 as the prefix of every filename downloaded.
但是,它将单元格A1的值添加为下载的每个文件名的前缀。
I need help modifying my macro so that instead of using Cell $A$1 as the prefix to every file downloaded, I want it to use the value in Column "A" of the same row. 我需要修改宏的帮助,因此我希望它不使用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
Example Data: 示例数据:
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
In the above example, the desired filenames would then be: -12345 picture1.jpg -12345 file1.pdf -abcde picture2.jpg -abcde file2.pdf 在上面的示例中,所需的文件名将是:-12345 picture1.jpg -12345 file1.pdf -abcde picture2.jpg -abcde file2.pdf
Anyone? 任何人?
只需使用C.Row
代替1
:
LocalFileName = "C:\downloaded\" & Range("A" & C.Row) & " " & Evaluate("TRIM(RIGHT(SUBSTITUTE(""" & C.Text & """,""/"",REPT("" "",1000)),1000))")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.