简体   繁体   English

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

[英]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.

相关问题 Excel宏可根据列和单元格值更改行的颜色 - Excel Macro to Change Color of a Row Based on Column and Cell Value 如果 A 列中的单元格等于 B 列中的单元格,则 Excel 宏将删除一行 - Excel Macro to delete a row if cell in Column A equals cell in Column B Excel 宏根据匹配两个值(行/列)查找单元格并替换单元格中的值 - Excel Macro to find a cell based on matching two values (Row/Column) and replace value in cell Excel Vba宏根据单元格值和列名称删除行或列 - Excel Vba Macro to Delete Row or Column Based on Cell Value and Column Name VBA Excel:宏,该宏在一行中搜索特定单元格中的某个变量值,然后将值复制并粘贴到此列中 - VBA Excel : Macro that searches a row for a certain variable value from specific cell and then copy&pastes value to this column excel宏:对于在一行中具有值的每个单元格,在该值下方插入一行 - excel macro: for each cell with a value in a row, insert a row below with that value Microsoft Excel 2013-VBA宏-插入值并选择同一列(下一行)中的下一个单元格 - Microsoft Excel 2013 - VBA Macro - Insert value and select next cell in same column (row underneath) Excel宏-如何根据特定的单元格值复制/拆分行 - Excel Macro - How to copy/split row based on specific cell value Excel宏,按行搜索返回下一个单元格的值 - Excel macro, search by row return next cell's value 默认值是没有宏的新行的单元格(Excel) - default value to a cell of a new row without a macro (Excel)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM