【问题标题】:Loop through the range VBA循环遍历范围 VBA
【发布时间】:2019-06-06 13:18:59
【问题描述】:

如果在该范围内有一个空白单元格,则在该范围内寻找一个简单的循环(例如列 A 范围(“A5:A15”))我需要隐藏与空白单元格/单元格关联的整个行/行.

我正在考虑这样的事情来适应各种范围,但会出现“类型不匹配”错误。任何原因

Sub test()

    Dim rng As Range, cell As Variant, ar As Variant
    Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range

    Dim MyArray(1 To 4) As Range

      With ThisWorkbook.Worksheets("sheet1")

      'Set MyArray = rng


       Set MyArray(1) = Range("O8:O17")
       Set MyArray(2) = Range("O55:O64")
       Set MyArray(3) = Range("G37:G46")
       Set MyArray(4) = Range("G89:G98")


        'ar = Array(Rng1, Rng2, Rng3, Rng4)

        'Set rng = .Range("O8:O17")

        For Each cell In MyArray

            If Len(cell.Value) < 1 Then

               cell.EntireRow.Hidden = True

            End If

        Next cell

    End With

End Sub

?

【问题讨论】:

  • 你尝试过什么了吗?如果是,您也可以粘贴代码。
  • @Mikku 是的,我在这里有什么建议吗?
  • 检查下面添加的新答案 (stackoverflow.com/a/56478651/5720144)
  • @Mikku 你是明星 它就像魔术一样工作!从来没有想过将 LBound 应用于 UBound
  • 好吧,现在您可以根据需要添加更多范围:)

标签: excel vba


【解决方案1】:

类似这样的:

你可以把它放在一个主题中:

For Each cell In Range("A5:A15")

    If Len(cell.Value) < 1 Then

        cell.EntireRow.Hidden = True

    End If

Next
For Each cell In Range("A40:A55")

    If Len(cell.Value) < 1 Then

        cell.EntireRow.Hidden = True

    End If

Next

新答案:

Dim rng As Range, cell As Variant, ar As Variant
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range

Dim MyArray(1 To 4) As Range

  With ThisWorkbook.Worksheets("sheet1")

  'Set MyArray = rng


   Set MyArray(1) = Range("O8:O17")
   Set MyArray(2) = Range("O55:O64")
   Set MyArray(3) = Range("G37:G46")
   Set MyArray(4) = Range("G89:G98")


    'ar = Array(Rng1, Rng2, Rng3, Rng4)

    'Set rng = .Range("O8:O17")
Dim i As Integer

    For i = LBound(MyArray) To UBound(MyArray)

            For Each cell In MyArray(i)

             If Len(cell.Value) < 1 Then

               cell.EntireRow.Hidden = True

            End If

        Next

    Next

End With

【讨论】:

  • Mikku 更好的方法是If cell = vbNullString
  • @Damian checking the string length is actually faster,虽然不太可能引起注意
  • @Damian ...我认为有很多方法可以做到这一点。但是,是的,总是更喜欢更快的。
  • @accurist ...太好了!如果它工作正常,请接受答案。
  • 刚刚做了。很好的帮助谢谢。只是一个快速的问题。假设我有更多的范围要循环。 ("A5:A15 & "A40:A45") 是否可以在上面的代码中将这些范围连接在一起?
【解决方案2】:

试试:

Option Explicit

Sub test()

    Dim rng As Range, cell As Range

    With ThisWorkbook.Worksheets("Sheet1")

        Set rng = .Range("A5:A15")

        For Each cell In rng

            If cell.Value = "" Then

                .Rows(cell.Row).EntireRow.Hidden = True

            End If

        Next cell

    End With

End Sub

【讨论】:

  • 这对预先存在的答案没有任何帮助
  • @TimStack OP 的一些有用提示是如何使用With Statement、范围的SetRows 之前的.
  • 所以除了Set语句,没有新信息
  • @Tim Stack 有很多新东西要添加。 'for each' 的使用比'for i' 更快。变量中的范围集可以帮助您清楚地指示范围,并更容易按照 OP 的要求添加更多范围。当您知道范围时,也没有理由计算最后一行。更多的代码应该尽可能短,以避免无缘无故地声明变量(起点和终点。)。最后 vbNullString 和 Is empty 可能会导致问题,所以最好使用 ""
【解决方案3】:

这充分利用了 Excel VBA 模型。我猜它比上面的要快,但还没有进行性能测试。

Dim Cell As Range
For Each Cell In Range("A5:A15").SpecialCells(xlCellTypeBlanks)
   Cell.EntireRow.Hidden = True
Next

【讨论】:

  • 这也是一种享受。如果我在上述场景(A5:A15)和(A40:A55)中有两个单独的范围怎么办我怎么能在上面的代码中将这两个范围连接在一起?
  • @accurist 使用Union,像这样:For Each Cell In Union(Range("A5:A15"), Range("A40:A55")).SpecialCells(xlCellTypeBlanks)
  • 或者对于范围内的每个单元格("A5:A15,A40:A55").SpecialCells(xlCellTypeBlanks)
【解决方案4】:

试试下面的

Option Explicit
Sub youcouldhaveatleasttriedtodosomethingyourself()

Dim r1 As Range, r2 As Range, c As Range, target As Range

With Workbooks(REF).Sheets(REF)
    Set r1 = .Range("A1:A54")
    Set r2 = .Range("F3:F32")

    Set target = Application.Union(r1, r2) 
    For Each area In target.Areas
        For Each c In area
            If c.Value = vbNullString Then .Rows(c.Row).EntireRow.Hidden = True
        Next c
    Next area
End With

End Sub

请注意,我现在设置了两个示例范围。您始终可以向 Union 函数添加更多范围变量。

【讨论】:

  • Tim, For Each 在工作表上的工作速度比 For i 快​​
  • @Damian 是的...didn't we talk about this?
  • 我喜欢使用这些循环。如果我指的是多个列,则不必使用.Offset。让我的代码更短更干净。另一方面,在这种情况下为什么不使用数组呢?
  • 因为您正在处理工作表,对于每个条件您需要隐藏一行,您也不需要在此循环中使用 Offset。例如,只需使用.Cells(Cell.Row, "A") 即可。
  • 是的,但是.Cells(i, 1) 有点短:D
猜你喜欢
  • 2011-12-11
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多