【问题标题】:Range loop where value of a cell is blank单元格值为空白的范围循环
【发布时间】:2016-01-31 18:04:07
【问题描述】:

我已经设置了一个范围。该范围内的一些单元格有值,而许多单元格没有值,它们是空白的。我有一个需要时间的范围循环,因为它处理每个单元格:

For Each cel1 In rngsh1

上面处理了范围内的所有单元格。

范围循环仅处理非空白单元格的确切语法是什么?

For Each cel11 in rngsh1 and not nothing 之类的东西我知道这种语法是错误的,但我正在寻找一个正确的。

【问题讨论】:

  • For Each cel11 in rngsh1 然后If Len(Cel1.Value) > 0 Then
  • 谢谢...让我试试这个
  • If Not IsEmpty(cel1.Value) Then:P
  • 两者都很有魅力。这将只检查非空白单元格。如果我只需要检查带有特定字符串的单元格呢?我需要为此提出一个新问题吗?我的字符串是“center1”“center5”“center8”“center9”。我有 54 个这样的中心。如果范围循环在非银行单元格中找到这些中心中的任何一个,那么我只需要处理一些信息。
  • 例如,我的所有字符串 "center1" "center5" "center8" 等等......在特定范围内I1:BJ1 如果 cel1 值是这 54 个值之一,则继续.如何为此编写语法?

标签: excel loops for-loop foreach vba


【解决方案1】:

类似这样的:

Dim cel11 As Range
Dim rngsh1 As Range

'Function MultiOr(str As String, ParamArray arr() As Variant) As Boolean
'  Dim holder, runner
'  MultiOr = True
'  For Each holder In arr 'look for everything in arr
'    If IsArray(holder) Then 'if what you found is an array
'      For Each runner In holder 'for everything in that array
'        If MultiOr(str, runner) Then Exit Function 
'      Next
'    Else 'if its no array
'      If Not IsMissing(holder) Then If holder = str Then Exit Function
'    End If
'  Next
'  MultiOr = False
'End Function

Sub MySub()
  For Each cel1 In Range
'   If Not IsEmpty(cel1.Value) Then 'see EEM's answer
    If Len(cel1.Value) > 0 Then 
'     If MultiOr(cel1.Value, condi) Then 'no need for this function 
      If Not IsError(Application.Match(cel1.Value, Range("I1:BJ1"), 0)) Then
        'your code here
      End If
    End If
  Next
End Sub

'`condi` can be a range with all the conditions or an array or simply a value...

【讨论】:

  • 糟糕!这对我来说太难理解了。如果我在I1:BJ1 中有我的字符串,那么该行将是If MultiOr(cel1.Value, I1:BJ1) Then
  • 正如我在之前的评论中所说,例如,我的所有字符串 "center1" "center5" "center8" 等等......在特定范围内 I1:BJ1 如果 cel1 值为 1在这 54 个值中,才继续。如何为此编写语法?这个功能对我有帮助吗?
  • MultoOr(value, array) 只检查array 中的任何内容是否等于value... 所以只需将If MultiOr(cel1.Value, condi) Then 更改为If MultiOr(cel1.Value, Range("I1:BJ1")) Then ...它将检查I1:BJ1 = cel1 中的所有内容。价值
  • 知道了.. 非常感谢。出于好奇的一个问题,是否真的需要编写一个函数来实现我正在寻找的东西?我只想检查cel1 值是否存在于我的范围I1:BJ1 中。有没有更短的线来转换像if cel1.value=[i1].value or cel1.value=[j1].value or cel1.value=[k1].value or ...... Then这样的长线
  • 你可以使用If Not IsError (Application.Match(cel1.Value, Range("I1:BJ1"), 0)) Then...我喜欢我的MultiOr cus 它能够处理包含数组的数组...但是Match 在这种情况下也会这样做...另外编辑了答案并记住了始终使用Len(...) > 0 的原因...感谢EEM :) 您还可以将LenMatch 放在一行中,例如:If Len(cel1.Value) > 0 And Not IsError(Application.Match(cel1.Value, Range("I1:BJ1"), 0)) Then
【解决方案2】:

要真正搜索非空白单元格,需要使用 SpecialCells 范围方法(请参阅Range.SpecialCells Method (Excel)

这个程序只处理非空白单元格

由于该过程中使用的某些资源对用户来说可能是新的,因此我建议访问Select Case Statement,但如果您对代码有任何疑问,请告诉我。

Sub Search_NonBlank_Cells()
Dim Rng As Range
Dim rCll As Range

    Rem Set Range
    Set Rng = ActiveSheet.Range(kRng)

    Rem Ensure blank intended cells are actually blank
    Rng.Value = Rng.Value2

    Rem Loop Through Non-Blank Cells Only
    For Each rCll In Rng.SpecialCells(xlCellTypeConstants, _
        xlErrors + xlLogical + xlNumbers + xlTextValues)

        Rem Validate if cell value starts with "center"
        If Left(rCll.Value2, 6) = "center" Then
            Rem Validate if remaining cell value is between 1 to 54
            Select Case Application.Substitute(rCll.Value2, "center", "")
            Case 1 To 54
                Rem Process Cell Found
                rCll.Interior.Color = RGB(255, 255, 0)

    End Select: End If: Next

End Sub

这是相同的过程,包括一些有助于调试和理解过程的行,还会在即时窗口中生成日志。

Sub Search_NonBlank_Cells_Debug()
Dim Rng As Range
Dim rCll As Range

: SendKeys "^g^a{DEL}": Stop
: Debug.Print vbLf; Now
: Debug.Print "Address"; Tab(11); "Cll.Value"; Tab(31); "Status"

    Rem Set Range
    Set Rng = ActiveSheet.Range(kRng)

    Rem Ensure blank intended cells are actually blank
    'i.e. Cells with formulas results as "" are not blank cell this makes then blank cells
    Rng.Value = Rng.Value2

    Rem Loop Through Non-Blank Cells Only
    For Each rCll In Rng.SpecialCells(xlCellTypeConstants, _
        xlErrors + xlLogical + xlNumbers + xlTextValues)

: Debug.Print rCll.Address; Tab(11); rCll.Value2;

        Rem Validate if cell value starts with "center"
        If Left(rCll.Value2, 6) = "center" Then
            Rem Validate if remaining cell value is between 1 to 54
            Select Case Application.Substitute(rCll.Value2, "center", "")

            Case 1 To 54
                Rem Process Cell Found
: Debug.Print Tab(31); "Processed"
                rCll.Interior.Color = RGB(255, 255, 0)

            Case Else
: Debug.Print Tab(31); "Skipped"

            End Select
        Else
: Debug.Print Tab(31); "Skipped"

    End If: Next

End Sub

【讨论】:

  • 谢谢@DirkReichel。我是从 OP 的 cmets 那里得到的。如果是这样,那么将预期值列在工作表中并与该范围进行比较会更有效。还需要知道是在单元格中搜索全部值还是部分值。操作只是让我们知道调整程序。
猜你喜欢
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多