简体   繁体   English

如何用范围替换单个单元格引用

[英]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.

相关问题 将 Sumproduct 与范围和单元格引用一起使用 - Using Sumproduct with ranges and cell references 用命名范围替换坐标参考 - Replace coordinate references with named ranges 如何在 VBA 的范围数组中引用单个单元格以对其进行排序? - How do I reference a single cell within an array of ranges in VBA to sort by it? 当它引用的工作表更新时,如何替换公式以保留在 vba 中的特定单元格上 - How do I replace a formula to stay on a specific cell in vba when the sheet it references updates 将2个值的范围与单个单元格进行比较 - Comparing 2 ranges of values to a single cell 如何在一行中找到包含值的所有单元格引用,并将其显示在一个单元格中? - How can I find all cell references in a row that contain a value, and display them in one cell? 替换部分单元格引用,而不是全部 - Replace some cell references, not all 如何在不同的单元格范围内将具有不同值的整行复制并粘贴到新表中? - How can I copy & paste entire rows with distinct values to a new sheet on varying cell ranges? 如何在具有特定单元格范围的列中循环并在VBA中设置数据验证? - How can I loop through columns with specific cell ranges and set data validation in VBA? 如何在Excel中填充两个命名范围中的单元格值? - How can I fill cell values from two named ranges in excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM