如果单元格具有相同的数据包标签,我会做一些关于减去时间值的事情,假设我有以下数据:
| 1 | A | 01 | x |
| 2 | B | 03 | x |
| 3 | A | 05 | x |
| 4 | C | 06 | x |
| 5 | B | 09 | x |
| 6 | A | 11 | x |
,值应该是这样的:
| 1 | A | 01 | 4 | >> Value of subtract time A1 from A3 5-1 = 4
| 2 | B | 03 | 6 | >> Value of subtract time B2 from A5 9-3 = 6
| 3 | A | 05 | 6 | >> Value of subtract time A3 from A6 11-5 = 6
| 4 | C | 06 | value |
| 5 | B | 09 | value |
| 6 | A | 11 | value |
我目前的代码是:
sub deltaip()
dim x as long
dim i as long
for i = 2 to rows.count
for x = 3 to rows.count
if cells(i,2).value = cells(x,2).value then cells(i,4) = cells(x,3).value-cells(i,3).value
next x
next i
End Sub
但是它被卡住了,我需要让X循环在每次找到相同的值时停止,进行减法并继续下一个x。另外我发现在下一个I循环中,X不会从3重置,但是从最后的X值开始重置,这是错误的吗?
有人能帮帮我吗?我很擅长使用Excel上的VBA,提前谢谢答案 0 :(得分:0)
通过更改为Do来完成像这样的代码(实际工作表上的代码):
Sub deltaip()
Dim x As Long
Dim i As Long
Dim n As Long
n = 3
x = 3
For i = 2 To 11
Do Until Cells(i, 3).Value = Cells(x, 3).Value
x = x + 1
If x > 12 Then Exit Do
Loop
If Cells(x, 2).Value - Cells(i, 2).Value > 0 Then
Cells(i, 8) = Cells(x, 2).Value - Cells(i, 2).Value
Else
Cells(i, 8) = ""
End If
n = n + 1
x = n
Next i
End Sub