【发布时间】:2017-05-14 00:25:02
【问题描述】:
我正在整理一个宏,该宏可以根据某个值是否向上/向下移动四分位数或保持季度与季度相同,自动手动选择符号(向上/向下箭头、等号) .
我正在使用三个 FOR EACH.... NEXT 循环,每个循环都有自己的一组嵌入式 if、elseif、else 语句。我在想处理这个问题的正确方法是让第一个循环运行第一个旧值(例如 A1),并将其存储在正确的 UDF 存储桶中,并在其中分配它的四分位数。一旦该循环结束,让新循环以相同的程序运行。一旦分配了两个值,第三个循环将比较两个四分位数,然后输入符号。然后它将重新开始并循环遍历指定范围内的所有单元格,直到全部设置完毕。
循环看起来可以正确迭代,但似乎没有达到范围内的所有值。
我认为我的问题在于循环的设置方式。有谁知道更好的方法来处理这个问题?
理想情况下,代码查看 b 列中的第一个值并为“currentQuart”变量分配一个值,然后循环到 a 列并为“oldQuart”变量分配一个值。完成后,第三个循环将比较两个值并根据运算符输入一个字符。
函数,存储在模块中:
函数 quartOne(ByVal cellValue As Variant) As Boolean quartOne = (cellValue >= 0.01 and cellValue
Function quartTwo(ByVal cellValue As Variant) As Boolean
quartTwo = (cellValue >= 25.01 And cellValue <= 50)
End Function
Function quartThree(ByVal cellValue As Variant) As Boolean
quartThree = (cellValue >= 50.01 And cellValue <= 75)
End Function
Function quartFour(ByVal cellValue As Variant) As Boolean
quartFour = (cellValue > 75)
End Function
代码
Sub CommandButton1_Click()
Dim cellOld As Range, cellCurrent As Range, cell As Range
Dim oldRng1
Dim currentRng1 As Range
Dim oldQuart As Integer
Dim currentQuart As Integer
Set oldRng1 = ActiveSheet.Range("A1:A4")
Set currentRng1 = ActiveSheet.Range("B1:B4")
For Each cellCurrent In currentRng1.Cells
For Each cellOld In oldRng1.Cells
For Each cell In currentRng1.Cells
'checks cellCurrent against functions in module and assigns variable
If quartOne(cellCurrent.Value) Then
currentQuart = 1
ElseIf quartTwo(cellCurrent.Value) Then
currentQuart = 2
ElseIf quartThree(cellCurrent.Value) Then
currentQuart = 3
ElseIf quartFour(cellCurrent.Value) Then
currentQuart = 4
Else
End If
'checks cellOld against functions in module and assigns variable
If quartOne(cellOld.Value) Then
oldQuart = 1
ElseIf quartTwo(cellOld.Value) Then
oldQuart = 2
ElseIf quartThree(cellOld.Value) Then
oldQuart = 3
ElseIf quartFour(cellOld.Value) Then
oldQuart = 4
Else
End If
'takes variable from above loops, runs through if/else and inputs corresponding character
If currentQuart = 1 And oldQuart = 1 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 1 And oldQuart > 1 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 2 And oldQuart < 2 Then
cell.Offset(, 1).Value = ChrW(&H2193)
ElseIf currentQuart = 2 And oldQuart = 2 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 2 And oldQuart > 2 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 3 And oldQuart > 3 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 3 And oldQuart = 3 Then
cell.Offset(, 1).Value = ChrW(&H3D)
ElseIf currentQuart = 3 And oldQuart < 3 Then
cell.Offset(, 1).Value = ChrW(&H2193)
ElseIf currentQuart = 4 And oldQuart < 4 Then
cell.Offset(, 1).Value = ChrW(&H2191)
ElseIf currentQuart = 2 And oldQuart = 4 Then
cell.Offset(, 1).Value = ChrW(&H3D)
End If
Exit For
Next cell
Next cellOld
Next cellCurrent
End Sub
!测试数据以单个值 (1-100) 的形式存储在 a-b 列中!
【问题讨论】:
-
您的问题/错误具体是什么?我看不出问题。
-
很抱歉。添加了问题。
-
尝试使用
F8单步执行代码并按照 VBA 执行循环的方式进行操作。当循环不能正常工作时发现它非常有用,因为您可以看到代码逐行运行。