Excel 循环行
我有这个if statemtns,如果我想让它在列(“A2:A50”)中有一个值的每一行中循环,我该怎么做?我试了很多,但还没有想出怎么做。我的if statemtens代码如下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 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语句有效,我想你不需要手机验证。我将尝试更改它:)