[英]Dynamically change cell values based on cells adjacent
我尝试使用公式来执行此操作,但不幸的是,它取决于单元格值动态变化的循环
我正在尝试在学习Excel VB时快速执行此操作,但是偏移量和单元格名称使我败北-有人在乎给我一些代码:-)
遍历D中的单元格到D的末尾
if Dn = "999" then
if C(n-1) = Cn then
Modify Dn = D(n-1) + 1 else
Modify Dn = 1
结束
示例单元格
C D
1852304 4
1852304 5
1852319 1
1852319 999
1852321 1
1852326 1
1852351 1
1852351 999
1852351 999
1852351 999
1852352 1
1852353 1
1852355 1
1852355 2
1852355 3
这就是我在上面翻译您的逻辑的方式:
Sub sample()
'~~> Declare Variables recommended
Dim rng As Range, cel As Range
'~~> this gets hold of the Range Object you want to work in
Set rng = Range("D1", Range("D" & _
Rows.Count).End(xlUp).Address)
'~~> loop through each cell in range
For Each cel In rng
'~~> check the value of current cell in range
If cel.Value = 999 Then
'~~> use .Offset to check the
'~~> adjacent cell in C column and compare
If cel.Offset(-1, -1).Value = cel.Offset(0, -1).Value Then
'~~> Modify Dn
cel.Value = cel.Offset(-1, 0).Value + 1
Else
cel.Value = 1
End If
End If
Next
End Sub
结果:假设您具有示例中的数据。
运行后,宏将变为:
这是您要尝试的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.