Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 单元格值为空的范围循环_Excel_Loops_For Loop_Foreach_Vba - Fatal编程技术网

Excel 单元格值为空的范围循环

Excel 单元格值为空的范围循环,excel,loops,for-loop,foreach,vba,Excel,Loops,For Loop,Foreach,Vba,我已经定了一个范围。该范围内的一些单元格有值,而许多单元格没有值,它们为空。我有一个需要时间的范围循环,因为它处理每个单元格: For Each cel1 In rngsh1 以上处理范围内的所有单元格 范围循环仅处理非空白单元格的确切语法是什么 类似于rngsh1中每个cel11的,而不是什么都没有我知道此语法错误,但我正在寻找正确的语法。类似于以下内容: Dim cel11 As Range Dim rngsh1 As Range 'Function MultiOr(str As Str

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

For Each cel1 In rngsh1
以上处理范围内的所有单元格

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


类似于rngsh1中每个cel11的
,而不是什么都没有
我知道此语法错误,但我正在寻找正确的语法。

类似于以下内容:

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...

要真正搜索非空白单元格,需要使用SpecialCells Range方法(请参阅

此过程仅处理非空白单元格

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

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

对于rngsh1中的每个cel11
,然后
如果Len(Cel1.Value)>0,那么
谢谢…让我试试这个
如果不是IsEmpty(Cel1.Value),那么
:Pboth就像charm一样工作。这将只检查非空白单元格。如果我只需要检查具有特定字符串的单元格,那么?我需要为此开始一个新问题吗?我的字符串是“center1”“center5”“center8”“center9”。我有54个这样的中心。如果range循环在非银行单元格中找到这些中心中的任何一个,那么我只需要处理一些信息。例如,我的所有字符串都是“center1”“center5”“center8”以此类推……在特定的范围内,
I1:BJ1
如果cel1值是这54个值中的一个,那么就继续吧。如何为其编写语法?哎呀!这对我来说太难理解了。如果我在
I1:BJ1
中有字符串,那么如果MultiOr(cel1.value,I1:BJ1),行将是
if MultiOr(cel1.value,I1:BJ1)然后
正如我在前面的评论中所说的,例如,我的所有字符串“center1”“center5”“center8”等等……在特定的范围I1:BJ1中,如果cel1值是这54个值中的一个,那么就继续进行。如何为此编写语法?这个函数能帮助我吗?
MultoOr(值,数组)
只需检查
数组中的任何内容是否等于
…因此只需将
如果是MultiOr(cel1.value,condi),则将
更改为
如果是MultiOr(cel1.value,Range(“I1:BJ1”))然后
…它将检查I1:BJ1=cel1.values中的所有内容。非常感谢。出于好奇,有一个问题,是否真的需要编写一个函数来实现我所寻找的功能?我只想检查
cel1
值,如果它在我的范围内
I1:BJ1
。有没有更短的行来转换长li如果cel1.value=[i1].value或cel1.value=[j1].value或cel1.value=[k1].value或……则类似于
,如果不是iError(Application.Match(cel1.value,Range(“i1:BJ1”),则可以使用
然后
…我喜欢我的
MultiOr
cus,因为它能够处理包含数组的数组…但是
Match
在这种情况下也可以…还编辑了答案并记住了始终使用
Len(…)>0的原因
…感谢EEM:)您还可以将
Len
Match
放在一行中,如:
如果Len(cel1.Value)>0而不是IsError(Application.Match(cel1.Value,Range(“I1:BJ1”),则
谢谢@DirkReichel。我是从OP的评论中得到的。如果是这样,那么将期望值列在工作表中并与该范围进行比较将更加有效。还需要知道在单元格中搜索整个值还是部分值。让我们知道如何调整程序。