我正在尝试在我的Excel数据库上创建一些自动操作。
在这种情况下,如果填充了另一个单元格,则递增一个值。
示例:
如果B21包含数字或文本,则从A21开始计数。 如果B相邻单元格上没有值,则计数将停止。
A B
21 1 Text 1
22 2 Text 2
23 3 Text 3
24
25
到目前为止,我得到了这个:
Sub Macro1()
Dim r1 As Range, r2 As Range, cell As Range, mynumber As Long
Set r1 = Range("B21:B2642")
Set r2 = Range("A21:A2642")
mynumber = 1
For Each cell In r1
If cell.Value <> "" Then
cell.Value("A21:A2642") = mynumber
mynumber = mynumber + 1
End If
Next
End Sub
贝斯茨
答案 0 :(得分:0)
使用它,
Sub Button1_Click()
Dim rw As Long, rng As Range, c As Range
rw = 2642
Set rng = Range("B21:B" & rw)
For Each c In rng.Cells
If c <> "" Then
If c.Row = 21 Then
c.Offset(, -1) = 1
Else
c.Offset(, -1) = c.Offset(-1, -1) + 1
End If
End If
Next c
End Sub