我从列 A
的第 1 行输入了数值至IA
.我想创建一个循环,将一个单元格与其之前的单元格进行比较(又名单元格 B1
到 A1
或单元格 F
到 E
)。让我们使用 B1
和 A1
作为例子。它查看单元格中的值 B1
并查看它是否大于 A1
中单元格的值.如果它更大,那么我想要一个 +
输入单元格B2
.如果 B1
是 < A1
放一个-
进入单元格B2
.我希望程序能够循环这个过程,以便它为所有列执行 A-AI
.以下是我希望程序执行的操作(当然,不包括正负号周围的破折号和括号)。
A B C D F 1 33.12 34.52 34.92 35.19 34.97 2 (+) (+) (+) (-)
I realize this task is easily performed in excel (not using VBA) but I am trying to learn VBA so I can perform much more complex tasks. I have written the basic code to do the simple task but I am not sure how to loop it so it will do this for all my cells!
Sub EnterFormula()
Dim x As Integer
Dim y As Integer
x = Worksheets("Sheet2").Range("C2").Value
y = Worksheets("Sheet2").Range("B2").Value
If x > y Then
Worksheets("Sheet2").Range("C4") = "+"
End If
End Sub
好的,所以对于我的程序的下一部分。它变得更加复杂。我们移动到第 3 行。第 3 行将要么有一个 U(代表向上)或一个 D(代表向下),要么什么都没有。
让我们从 C 列开始。C1 列的值为 34.92,C2 的值为 +(因为 34.92 大于前一天的 33.02)。现在我们转到前面的第一个“+”,中间至少有一个相反的符号(在本例中为“-”)。因此,在这种情况下,即为 A 行(B 行下方中间有一个“-”)。现在,如果 C1 (34.92) 中的数值大于 A (33.12) 中的数值,那么我们在 C3 中指定一个“U”。如果它不是更大,我们将在 C3 中留下一个空单元格。
让我们转到 D 列。 D1 列的值是 35.19,大于 C1 的值 34.92,这就是 D2 有“+”的原因。接下来我们转到前面的第一个“+”,中间至少有一个相反的符号(在本例中为“-”)。所以在这种情况下,这又是 A 行。由于 D1 (39.19) 中的数值大于 A1 (33.12) 中的数值,因此 D3 得到一个 U。
移至 F 列(32.97)...注意:我将值从原始 F 更改了一点。32.97 比 35.19(D1)少,这就是为什么 F2 是“-”。接下来我们转到前面的第一个“-”,中间至少有一个相反的符号(在本例中为“+”)。所以在这种情况下,这是 B 行(中间有两个“+”号)。现在因为我们这次处理的是“-”符号,所以我们看看 F1 中的数值是否小于 B1 中的数值……它是,所以在 F3 中输入了一个“D”。如果 F1 大于 B1,则该单元格将为空。
在 G 列 (35.21) 上。这大于 32.97 (F1),因此在 G2 中获得“+”。接下来我们转到前面的第一个“+”,中间至少有一个相反的符号(在本例中为“-”)。所以在这种情况下,这是 D 行(中间有一个“-”)。由于 G1 的数值大于 D1 的数值,我们指定为“U”。如果它不是更大,我们会将单元格留空。
A B C D F G H I 1 33.12 33.02 34.92 35.19 32.97 35.21 35.60 35.90 2 (+) (-) (+) (+) (-) (+) (+) (+) 3 U U D U U U
Here is my code so far for this. I have added to my original code which was creating the "+" signs and "-" signs.
Sub Comparison()
Dim targetCell As Range
Dim targetSignCell As Range
Dim currentSign As String
Dim currentNumericalCell As Currency
' Find out what sign (+ or -) the current Cell has in it
currentSign = Worksheets("Sheet2").Range("H3").Value
'Variable to associate the numerical number above the current Cell
currentNumericalCell = Worksheets("Sheet2").Range("H2").Value
' Here we iterate through each cell in a specified range
' Since you know you want to start at B1 and go until E1,
' you can ues the following syntax to go through each cell
For Each Cell In Range("B2:H2")
' Get the value of the current cell with the .Value property
currentValue = Cell.Value
' 现在获取它之前的单元格的值(按列)
previousValue = Cell.Offset(0, -1).Value
' Create a variable for our target cell
Set targetCell = Cell.Offset(1, 0)
' Here are the basic comparisons
If currentValue > previousValue Then
targetCell.Value = "+"
ElseIf currentValue < previousValue Then
targetCell.Value = "-"
ElseIf currentValue = previousValue Then
targetCell.Value = "="
Else
' Not sure how it would happen, but this
' is your catch-all in case the comparisons fail
targetCell.Value = "???"
End If
' Now go to the next cell in the range
Next Cell
'Alex starting to code
For Each Cell In Range("H3:B3")
' Find out what the sign is in the cell before it
previousSign = Cell.Offset(0, -1).Value
'Variable used to find the first cell with an
'Opposite sign as the current cell
oppositeSign = Cell.Offset(0, -2).Value
'Variable to associate the numberical number above the first Opposite Sign Cell
oppositeNumericalCell = Cell.Offset(-1, -2).Value
' Create a Variable for Target Cell
Set targetSignCell = Cell.Offset(1, 0)
If currentSign.Value = "+" And currentSign.Value <> previousSign.Value And oppositeSign.Value = currentSign.Value And currentNumericalCell.Value > oppositeNumericalCell.Value Then
targetSignCell = "U"
ElseIf currentSign.Value = "-" And currentSign.Value <> previousSign.Value And oppositeSign.Value = currentSign.Value And currentNumericalCell.Value < oppositeNumericalCell.Value Then
targetSignCell = "D"
Else
End If
Next Cell
End Sub
最佳答案
我同意@JohnBustos 的观点,即公式会更有效,但是如果这确实是一个学习练习,那么这里有一个简单的例子,可以满足你的要求:
Sub Comparison()
Dim targetCell As Range
' Here we iterate through each cell in a specified range
' Since you know you want to start at B1 and go until E1,
' you can ues the following syntax to go through each cell
For Each cell In Range("B1:E1")
' Get the value of the current cell with the .Value property
currentValue = cell.Value
' Now get the value of the cell that is before it (column-wise)
previousValue = cell.Offset(0, -1).Value
' Create a variable for our target cell
Set targetCell = cell.Offset(1, 0)
' Here are the basic comparisons
If currentValue > previousValue Then
targetCell.Value = "+"
ElseIf currentValue < previousValue Then
targetCell.Value = "-"
ElseIf currentValue = previousValue Then
targetCell.Value = "="
Else
' Not sure how it would happen, but this
' is your catch-all in case the comparisons fail
targetCell.Value = "???"
End If
' Now go to the next cell in the range
Next cell
End Sub
如果您将其作为公式来执行,它可能是这样的(输入
B2
并复制到范围的末尾):=IF(B1>A1,"+",IF(B1<A1,"-","="))
这将比较公式上方的单元格和该单元格左侧的单元格并添加适当的符号。
关于excel - VBA 初学者 : Greater than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238061/