[英]How can I replace single cell references with ranges
I have 5 columns ((a)uptick, (b)downtick, (c)original, (d)current), and (e) Stored Value.我有 5 列((a)uptick,(b)downtick,(c)original,(d)current)和(e)存储值。 All columns need to be a range of rows.
所有列都必须是一系列行。 When d2 changes I want to compare it to e2 and if d2>e2 then bump the counter in a2 by 1 (uptick), if d2<e2 then bump the counter in b2 (downtick).
当 d2 更改时,我想将其与 e2 进行比较,如果 d2>e2 则将 a2 中的计数器增加 1(上升),如果 d2<e2 则将 b2 中的计数器撞击(下降)。 I have it working with many if and elseif statements but would rather use less code using variables for the range.
我让它与许多 if 和 elseif 语句一起使用,但宁愿使用更少的代码来使用范围内的变量。 To detect the changing cell I use "If Not Intersect (Target, Range("d2:d10")) Is Nothing Then...."
为了检测变化的单元格,我使用“If Not Intersect (Target, Range("d2:d10")) Is Nothing Then....”
I cannot seem to figure out how to replace specific cell references with ranges.我似乎无法弄清楚如何用范围替换特定的单元格引用。 Any help would be most appreciated!
非常感激任何的帮助!
Sample Code below not using ranges yet.下面的示例代码尚未使用范围。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D2:D10")) Is Nothing Then
If Target.Value > Range("E2") Then
Range("A2") = Range("A2") + 1
Pause 2#
Range("E2") = Target.Value
ElseIf Target.Value < Range("E2").Value Then
Range("B2") = Range("B2") + 1
Pause 2#
Range("E2") = Target.Value
End If
End If
End Sub
I assume you want to change the cell value in the same row that the value was entered in column D, ie if D4 has been changed, then adjust A4 or B4.我假设您要更改在 D 列中输入值的同一行中的单元格值,即如果 D4 已更改,则调整 A4 或 B4。 To do that, you need the row number of the changed cell.
为此,您需要更改单元格的行号。 You can extract that with
target.row
.您可以使用
target.row
提取它。 Throw that into a variable and use the variable instead of the row number in the Range() property.将其放入变量中并使用该变量而不是 Range() 属性中的行号。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D2:D10")) Is Nothing Then
dim myRow as long
myRow = target.row
If Target.Value > Range("E" & myRow) Then
Range("A" & myRow) = Range("A" & myRow) + 1
Pause 2#
Range("E" & myRow) = Target.Value
ElseIf Target.Value < Range("E" & myRow).Value Then
Range("B" & myRow) = Range("B" & myRow) + 1
Pause 2#
Range("E" & myRow) = Target.Value
End If
End If
End Sub
You could use .Offset
to get the same result.您可以使用
.Offset
获得相同的结果。 The following code assumes you're only interested in the range D2:D10
and aren't concerned if the value in column D
equals the value in column E
.以下代码假定您只对范围
D2:D10
感兴趣,并且不关心列D
中的值是否等于列E
中的值。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("D2:D10"), Target) Is Nothing Then
If Target.Rows.Count > 1 Then Exit Sub
If Target > Target.Offset(, 1) Then
Target.Offset(, -3) = Target.Offset(, -3) + 1
Else
If Target < Target.Offset(, 1) Then
Target.Offset(, -2) = Target.Offset(, -2) + 1
End If
End If
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.