Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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/ios/119.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_Excel 2010_Vba - Fatal编程技术网

Excel 循环行

Excel 循环行,excel,excel-2010,vba,Excel,Excel 2010,Vba,我有这个if statemtns,如果我想让它在列(“A2:A50”)中有一个值的每一行中循环,我该怎么做?我试了很多,但还没有想出怎么做。我的if statemtens代码如下 If WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("B2:D50")) = 0 Then Error 1001 ElseIf WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("B2:B50

我有这个if statemtns,如果我想让它在列(“A2:A50”)中有一个值的每一行中循环,我该怎么做?我试了很多,但还没有想出怎么做。我的if statemtens代码如下

 If WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("B2:D50")) = 0 Then
   Error 1001
ElseIf WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("B2:B50")) > 0 And WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("C2:C50")) > 0 Then
   Error 1001
End If
if语句起作用,它们做我想做的事情,但它们没有按照我的意愿循环,我想它做的是:

If WorksheetFunction.CountA(ThisWorkbook.Sheets(1).Range("A2:A50")) > 50 Then
Do the if statements above and loop. 

我已经重新编辑了,你能测试一下吗

Sub loopXER()

'Define variables
Dim rng As Range
Dim cell As Range
Dim cell1 As Range
Dim cell2 As Range
Dim cell3 As Range


'Set the range to use
Set rng = Range("A2:A50")
Set rng2 = Range("B2:D50")
Set rng3 = Range("B2:B50")
Set rng4 = Range("C2:C50")


'Iterate through each cell in range
For Each cell In rng
'If cell is empty or it's value is over 50, go to if's defined by OP, else go to next cell
    If IsEmpty(cell) Or cell.Value > 50 Then
         For Each cell1 In rng2
            If IsEmpty(cell1) Or cell1.Value = 0 Then
                MsgBox "Error 1001"
            End If
         Next cell1

         For Each cell2 In rng3
            If cell2.Value > 0 Then
                MsgBox "Error 1001"
            End If
         Next cell2

         For Each cell3 In rng4
            If cell3.Value > 0 Then
                MsgBox "Error 1001"
            End If
         Next cell3

    End If

Next cell

End Sub
请尝试以下代码:-

  Dim rng As Range
  Dim data As Range
  Dim myCell As Range

  Set rng = ThisWorkbook.Sheets(1).Range("A2:A50")  

  'If *any* cell has a value in this range (adjust as required)
  If WorksheetFunction.CountA(rng) > 0 Then

    'Capture cells with values and formulae
    Set data = Union(rng.SpecialCells(xlCellTypeConstants, 23), _
                     rng.SpecialCells(xlCellTypeFormulas, 23))

    data.Select

    For Each myCell In data

      Debug.Print myCell.Address

    Next

  End If

您的意思是要检查A2到A50之间的值?谢谢,但如果第2行不是空的,但范围B2:D50是空的,则不会给出错误。不好,我知道了。既然你说if语句有效,我想你不需要手机验证。我将尝试更改它:)