Excel VBA初学者:大于
我在第1行中输入了从Excel VBA初学者:大于,excel,vba,Excel,Vba,我在第1行中输入了从A到IA列的数值。我想创建一个循环,将一个单元格与其前面的单元格进行比较(也称为单元格B1到A1或单元格F到E)。让我们以B1和A1为例。它查看单元格B1中的值,并查看该值是否大于A1中单元格的值。如果大于,则我希望在单元格B2中输入+。另外,如果B1是previousValue,则 targetCell.Value=“+” ElseIf currentValue
A
到IA
列的数值。我想创建一个循环,将一个单元格与其前面的单元格进行比较(也称为单元格B1
到A1
或单元格F
到E
)。让我们以B1
和A1
为例。它查看单元格B1
中的值,并查看该值是否大于A1
中单元格的值。如果大于,则我希望在单元格B2
中输入+
。另外,如果B1
是
则将-
放入单元格B2
。我希望程序能够循环此过程,以便对所有列A-AI
执行此操作。下面是我想让程序做的(当然不包括虚线和围绕正负符号的妄想)
好的,接下来的节目。它变得更复杂了。我们移到第三排。第3行要么有一个U(表示向上),要么有一个D(表示向下),要么什么都没有
让我们从C列开始。C1列的值为34.92,C2的值为a+(因为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)…注意:我对原来的值做了一些更改。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
'现在获取前面单元格的值(按列)
previousValue=单元格偏移量(0,-1).Value
'为目标单元格创建一个变量
设置targetCell=Cell.Offset(1,0)
这是基本的比较
如果currentValue>previousValue,则
targetCell.Value=“+”
ElseIf currentValue<先前的值然后
targetCell.Value=“-”
ElseIf currentValue=之前的值,然后
targetCell.Value=“=”
其他的
“不知道会发生什么,但是
“在比较失败的情况下,你能做到什么
targetCell.Value=“??”
如果结束
'现在转到范围内的下一个单元格
下一个细胞
“亚历克斯开始编码了
对于范围内的每个单元格(“H3:B3”)
“找出它前面牢房里的标志是什么
previousSign=单元格偏移量(0,-1).Value
'变量,用于查找具有
'与当前单元格相反的符号
对立符号=单元格偏移量(0,-2).Value
'变量将第一个相反符号单元格上方的数字关联起来
对立数值单元格=单元格偏移量(-1,-2).Value
'为目标单元格创建一个变量
设置targetSignCell=Cell.Offset(1,0)
如果currentSign.Value=“+”和currentSign.Value previousSign.Value和oppositeSign.Value=currentSign.Value和currentNumericalCell.Value>oppositeNumericalCell.Value,则
targetSignCell=“U”
ElseIf currentSign.Value=“-”和currentSign.Value previousSign.Value和oppositeSign.Value=currentSign.Value和currentNumericalCell.Value
我同意@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
这会将公式上方的单元格与该单元格左侧的单元格进行比较,并添加相应的符号。如果要使用的区域中没有空单元格,可以这样做:
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
伟大的如果您发布到目前为止的代码和您正在使用的语言,那么有人将能够提供帮助。我正在使用VBA(excel)。我已相应地标记了您的问题。建议您添加您当前拥有的任何代码。编辑您的问题并将代码添加到其中,以便正确格式化!您想这样做是为了学习VBA吗?因为这也可以很容易地用Excel公式完成…哇!我一直在试验cell.value,但正在搜索以前的cell函数(不知道cell.offset),非常感谢@阿列克斯亨特一点问题都没有——你肯定走对了方向。祝你一切顺利:)嘿!查看我最近对代码的编辑。我的代码不起作用,但我想我已经接近了!让我知道!
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
' 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
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
=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