因此,我对使用 excel 的 VBA 代码非常陌生,并且我正在尝试创建一个“修改日期”列,以便为工作中的 list 编辑前一列中的值。我以前为另一个 list 做过一次,但我用老式的方式做,因为它不是一个很长的 list 。但是对于这个应用程序,这根本没有效率,因为这个列表将持续进行。我使用社区中其他人的其他示例拼凑了这段代码,但我无法弄清楚错误的来源。这是说有一个编译错误'Do without Loop'。根据我对其他帖子的理解,它认为'If'语句没有被关闭,但我使用了'End If'并且我的代码中只有一个'If'语句。我需要它从第 6 列开始交替列,然后重复每一行。任何帮助深表感谢!
Sub Worksheet_Change(ByVal Target As Range)
Dim ColCount As Long
ColCount = 6
Dim RowCount As Long
RowCount = 2
Dim iCol As Long
iCol = 7
Dim iRow As Long
iRow = 2
Do While RowCount < 2
Do While ColCount < 6
Do While iCol < 7
Do While iRow < 2
If Target.Column = ColCount And Target.Row = RowCount Then
ActiveSheet.Cells(iRow, iCol).Value = Format(Date, "mm/dd/yyyy")
End If
RowCount = RowCount + 1
ColCount = ColCount + 2
iCol = iCol + 2
iRow = iRow + 1
Loop
End Sub
最佳答案
更简单的方法:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
Set rng = Application.Intersect(Target, Me.Range("F:F,H:H,J:J")) 'adjust to suit...
If rng Is Nothing Then Exit Sub 'no updates in monitored range
For Each c In rng.Cells
c.Offset(0, 1).Value = Format(Date, "mm/dd/yyyy")
Next c
End Sub
关于excel - 具有增量行和列的 VBA Excel 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70466092/