使用过滤器定义excel宏范围

时间:2013-03-04 16:35:45

标签: excel-vba vba excel

首先,我承认我几乎不知道在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).

但所有这一切都是选择一个过滤范围的文件。

非常感谢任何帮助您做到这一点。

克里斯。

1 个答案:

答案 0 :(得分:0)

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

For Each vCell In Range("J4:J" & Cells(Rows.Count, "J").End(xlUp).Row)
    If not vCell.EntireRow.Hidden Then
    [...]