0

首先,我承认我对在 excel 中编写宏几乎一无所知。我设法通过拼凑来自其他各种帖子的其他宏来创建下面的宏。

下面的宏有效,但我需要它来做一件事,但我无法弄清楚。

基本上,宏的作用是允许用户选择一个文件夹位置,然后浏览一列并选择该列每一行中包含的链接,然后将该链接另一端的文件保存到所选文件夹路径具有定义的命名格式。

唯一我想不通的是,当我对 excel 表单应用过滤器时,它仍然会抓取所有文件,无论它们是否可见。

当前宏:

*Private Declare PtrSafe 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
Sub Button1_Click()
Dim intranetLink As String
Dim mainBook As Workbook
Dim Counter As Integer
Dim saveDialog As FileDialog
Dim savePath As String
Dim filename As String
Counter = 4

    Set saveDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With saveDialog
        .Title = "Select a Folder"              'sticks a title on the dialog so the user kind of knows what they're supposed to be doing
        .AllowMultiSelect = False               'prevents the user from selecting more than one item out the dialog.
        .InitialFileName = strPath              '
        If .Show <> -1 Then GoTo FolderBombed   'if the user does something funky or cancels, abort the rest of the macro.
        savePath = .SelectedItems(1)            'get the file path to the selected folder
    End With

    For Each vCell In Range("J4:J" & Cells(Rows.Count, "J").End(xlUp).Row)
    intranetLink = vCell.Text
    filename = Cells(Counter, 6)
        filename = "c:\Path\" + filename
        URLDownloadToFile 0, intranetLink, filename, 0, 0
        Counter = Counter + 1
    Next vCell

FolderBombed:
        MsgBox ("Completed")
End Sub*

我需要修改的行如下:

  For Each vCell In Range("J4:J" & Cells(Rows.Count, "J").End(xlUp).Row)

我试过把它改成这样:

For Each vCell In Range("J4:J" & Cells(Rows.Count, "J").CurrentRegion.SpecialCells(xlVisible).End(xlUp).Row).

但这所做的只是选择一个被过滤掉的文件。

将不胜感激任何有助于做到这一点的帮助。

克里斯。

4

1 回答 1

0

使用这个 - 循环遍历单元格并检查每个单元格:

For Each vCell In Range("J4:J" & Cells(Rows.Count, "J").End(xlUp).Row)
    If not vCell.EntireRow.Hidden Then
    [...]
于 2013-03-05T15:29:51.720 回答