初学者到VBA:大于

我有从列AIA行1中input的数值。 我想创build一个循环,比较一个单元格与它之前的单元格(又名单元格B1A1或单元格FE )。 我们以B1A1为例。 它查看单元格B1中的值,并查看它是否大于A1单元格的值。 如果它更大,我想要一个+进入单元格B2 。 另外,如果B1 < A1则将单元格放入单元格B2 。 我希望程序能够循环这个过程,所以对于所有的A-AI列来说都是如此。 下面是我想要的程序(不包括在正面和负面迹象当然破折号)。

         ABCDF
 1 33.12 34.52 34.92 35.19 34.97
 2(+)(+)(+)( - )

我意识到这个任务很容易在Excel中执行(不使用VBA),但我正在学习VBA,所以我可以执行更复杂的任务。 我已经写了基本代码来做简单的任务,但我不知道如何循环,所以它会为我所有的细胞做到这一点!

 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为+(比前一天的33.02大34.92)。 现在我们到第一个前面的“+”,至less有一个相反的符号(在这个例子中是“ – ”)。 所以在这种情况下是行A(在行B之下的一个“ – ”)。 现在如果C1(34.92)中的数值大于A(33.12)中的数值,那么我们在C3中指定一个“U”。 如果它不大,我们会在C3中留下一个空单元格。

让我们进入D列。D1列的值为35.19,大于34.92的C1值,这就是为什么D2有一个“+”的原因。 接下来我们到第一个前面的“+”,至less有一个相反的符号(在这里是“ – ”)。 所以在这种情况下,再次是行A. 由于D1(39.19)中的数值大于A1(33.12)中的数值,所以D3得到一个U.

移到F栏(32.97)…注意:我把原来的F值稍微改了一点,32.97小于35.19(D1)这就是为什么F2是“ – ”的原因。 接下来我们到第一个前面的“ – ”,至less有一个相反的符号(在这个例子中是“+”)。 所以在这种情况下,这是行B(中间有两个“+”号)。 现在因为我们这次是处理“ – ”符号,所以我们看看F1中的数值是否是LESS,然后是B1 …中的数值,那么在F3中input“D”。 如果F1比B1大,那么这个单元将被留空。

到G列(35.21)。 这大于32.97(F1),因此在G2中获得“+”。 接下来,我们前往第一个“+”,至less有一个相反的符号(在本例中为“ – ”)。 所以在这种情况下,这是D行(中间有一个“ – ”)。 由于G1的数值大于D1的数值,我们指定一个“U”。 如果它不是更大,我们会把这个单元格留空。

         ABCDFGHI
 1 33.12 33.02 34.92 35.19 32.97 35.21 35.60 35.90
 2(+)( - )(+)(+)( - )(+)(+)(+)
 3 UUDUUU

这是我迄今为止的代码。 我已经添加到我创build“+”号和“ – ”号的原始代码中。

 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 

我同意@JohnBus​​tos公式会更有效率,但如果这确实是一个学习练习,那么这里是一个简单的例子,可以做你想做的事情:

 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 

如果你是做一个公式的话,它可能是这样的(input到B2并复制到范围的末尾):

 =IF(B1>A1,"+",IF(B1<A1,"-","=")) 

这将比较公式上方的单元格和该单元格左侧的单元格,并添加相应的符号。

假设你想要工作的范围内没有空单元格,你可以这样做:

 Range("b2").Select Do Until IsEmpty(ActiveCell.Offset(-1, 0)) If ActiveCell.Offset(-1, 0).Value > ActiveCell.Offset(-1, 1).Value Then ActiveCell.Formula = "+" End If If ActiveCell.Offset(-1, 0).Value < ActiveCell.Offset(-1, 1).Value Then ActiveCell.Formula = "-" End If ActiveCell.Offset(0, 1).Select Loop 

如果在该范围内有空单元,则不用“直到”使用

 dim I for I = 1 to .. next I