如何使用单元格地址作为Range()的参数?

时间:2019-10-10 17:14:09

标签: excel vba

我有一个模板文件,将使用该文件来填充更多文件,并且我需要根据选择的内容隐藏一些行,但是同时我无法隐藏其他行。如果数据始终保持相同大小,我会做得很好,但是文件将根据信息而增加和减少。

我在C列中有一系列值。我试图做的是查找包含“ Pack”的单元格值(所有文件都相同)。从包含“ Pack”的单元格中(假设现在位于C8,但可以在其他文件的C30中),我需要开始查找与我从下拉列表(行)获得的值不相等的值,然后隐藏行。

也许可以更好地解释一下,我也试图做的是分配一个变量,该变量将保存下拉列表的值,并查找不相等的值并将其隐藏。然后执行.Find()查找“ Pack”字样。一旦找到,获取单元地址。最后,将该地址用作Range()中的参数,就像您在我编写的代码中看到的那样:对于Range(“ packR:C5”)中的每个单元格,我知道这是非常错误的,因为我无法通过那个。

    Dim cell As Range
    Dim pack As Range


    rowing = Range("A2").Value

    Set pack = Range("C1:C12").Find("Pack")
    Set packA = Range(pack.Address)

    Set packR = packA

        For Each cell In Range("packR:-end point here")
          cell.EntireRow.Hidden = False
        If Not IsEmpty(cell) Then
       If cell.Value <> rowing Then
          cell.EntireRow.Hidden = True
        End If
    End If
   Next

我对vba的了解很少,但是通过研究我可以了解一些。基本上,目标是忽略“ Pack”顶部的所有行,并从“ Pack”(需要具有单元格地址)开始查找到excel文件的末尾。最大的问题是获取该单元格地址并将其用作Range(“”:“”)的参数。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找类似的东西。请注意有关指定Range.Find的其他参数的注释。

Sub Test()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim rowing As Variant
    rowing = ws.Range("A2").Value

    Dim pack As Range
    Set pack = ws.Range("C1:C12").Find("Pack") '<--- you should specify the other parameters of Find

    Dim lastCell As Range
    Set lastCell = ws.Cells(ws.Rows.Count, "C").End(xlUp)

    If Not pack Is Nothing Then '<--- tests to see if pack was found
        Dim cell As Range

        For Each cell In ws.Range(pack, lastCell)
            If Not IsEmpty(cell) Then
                cell.EntireRow.Hidden = (cell.Value <> rowing)
            End If
        Next
    End If

End Sub

编辑:    如果行已隐藏,End(xlUp)将找不到真正的最后一行。为了解决这个问题,这里有两个选择:

  1. 找到“ Pack”后取消隐藏所有行。
Sub Test()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim rowing As Variant
    rowing = ws.Range("A2").Value

    Dim pack As Range
    Set pack = ws.Range("C1:C12").Find("Pack") '<--- you should specify the other parameters of Find

    If Not pack Is Nothing Then '<--- tests to see if pack was found
        ws.UsedRange.EntireRow.Hidden = False '<--- unhide all rows so as to find the last cell properly

        Dim lastCell As Range
        Set lastCell = ws.Cells(ws.Rows.Count, "C").End(xlUp)

        Dim cell As Range

        For Each cell In ws.Range(pack, lastCell)
            If Not IsEmpty(cell) Then
                cell.EntireRow.Hidden = (cell.Value <> rowing)
            End If
        Next
    End If

End Sub
  1. 使用另一种方法来查找最后一个单元格:
Sub Test()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim rowing As Variant
    rowing = ws.Range("A2").Value

    Dim pack As Range
    Set pack = ws.Range("C1:C12").Find("Pack") '<--- you should specify the other parameters of Find

    Dim lastCell As Range
    Set lastCell = GetLastCell(ws, 3)

    If Not pack Is Nothing Then '<--- tests to see if pack was found
        Dim cell As Range

        For Each cell In ws.Range(pack, lastCell)
            If Not IsEmpty(cell) Then
                cell.EntireRow.Hidden = (cell.Value <> rowing)
            End If
        Next
    End If

End Sub


Private Function GetLastCell(ByVal ws As Worksheet, Optional ByVal colNum As Long = 1) As Range

    With ws
        Dim lastCell As Range
        Set lastCell = .Columns(colNum).Find(What:="*", _
                      After:=.Cells(1, colNum), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False)

        If lastCell Is Nothing Then
            Set lastCell = .Cells(1, colNum)
        End If
    End With

    Set GetLastCell = lastCell
End Function