VBA 在第 2-100 行中重复函数的代码
VBA Code to repeat a function in rows 2-100
我对 VBA 代码没有什么经验,我正在尝试让此代码重复第 2-100 行。与其他行中重复的代码相比,我发现的问题是我的代码有多个结束参数,我不确定如何解释这一点。非常感谢任何帮助。
Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
a = Date
b = 2
If Cells(b, 3).Value <> Blank Then
If Cells(b, 2).Value = Blank Then
Cells(b, 2).Value = a
Exit Sub
End If
If Cells(b, 2).Value < a Then
Exit Sub
End If
Cells(b, 2).Value = a
End If
End Sub
这就是我正在使用的。我试图使单元格引用成为一个我可以计算的变量,但无论我尝试什么,它都不起作用。
编辑:很抱歉没有说明。当 C2 从空变为有任何内容时,代码应该将今天的日期放在 B2 中。如果那里已经有日期,它还可以防止更改日期,即使 C2 已清除。我正在尝试扩展它,以便它不仅仅是 C2 和 B2,而是 C2-C100,然后是相应的 B2-B100。
编辑 2:正在通过手动输入更改 C2。目的是让某人将数据输入 C2(以及该行的其余部分),并自动输入和锁定日期,这样他们就无法更改它,我可以看到输入数据的时间。
工作表更改(时间戳)
- 这仅在手动修改
C
列中的值时有效,即通过手动输入、通过 copy/pasting 以及通过 VBA. 写入
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const fRow As Long = 2
Const sCol As String = "C"
Const dCol As String = "B"
Dim scrg As Range: Set scrg = Columns(sCol).Resize(Rows.Count - fRow + 1)
Dim srg As Range: Set srg = Intersect(scrg, Target)
If srg Is Nothing Then Exit Sub
Dim drg As Range
Dim dCell As Range
Dim sCell As Range
For Each sCell In srg.Cells
Set dCell = sCell.EntireRow.Columns(dCol)
If Len(CStr(dCell.Value)) = 0 Then
If drg Is Nothing Then
Set drg = dCell
Else
Set drg = Union(drg, dCell)
End If
End If
Next sCell
' All cells already contain a date.
If drg Is Nothing Then Exit Sub
Dim dDate As Date: dDate = Now ' after prooving that it works, use 'Date'
' To prevent retriggering this event and any other event while writing.
Application.EnableEvents = False
' Write in one go.
drg.Value = dDate
SafeExit:
' Enable events 'at any cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
我对 VBA 代码没有什么经验,我正在尝试让此代码重复第 2-100 行。与其他行中重复的代码相比,我发现的问题是我的代码有多个结束参数,我不确定如何解释这一点。非常感谢任何帮助。
Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
a = Date
b = 2
If Cells(b, 3).Value <> Blank Then
If Cells(b, 2).Value = Blank Then
Cells(b, 2).Value = a
Exit Sub
End If
If Cells(b, 2).Value < a Then
Exit Sub
End If
Cells(b, 2).Value = a
End If
End Sub
这就是我正在使用的。我试图使单元格引用成为一个我可以计算的变量,但无论我尝试什么,它都不起作用。
编辑:很抱歉没有说明。当 C2 从空变为有任何内容时,代码应该将今天的日期放在 B2 中。如果那里已经有日期,它还可以防止更改日期,即使 C2 已清除。我正在尝试扩展它,以便它不仅仅是 C2 和 B2,而是 C2-C100,然后是相应的 B2-B100。
编辑 2:正在通过手动输入更改 C2。目的是让某人将数据输入 C2(以及该行的其余部分),并自动输入和锁定日期,这样他们就无法更改它,我可以看到输入数据的时间。
工作表更改(时间戳)
- 这仅在手动修改
C
列中的值时有效,即通过手动输入、通过 copy/pasting 以及通过 VBA. 写入
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const fRow As Long = 2
Const sCol As String = "C"
Const dCol As String = "B"
Dim scrg As Range: Set scrg = Columns(sCol).Resize(Rows.Count - fRow + 1)
Dim srg As Range: Set srg = Intersect(scrg, Target)
If srg Is Nothing Then Exit Sub
Dim drg As Range
Dim dCell As Range
Dim sCell As Range
For Each sCell In srg.Cells
Set dCell = sCell.EntireRow.Columns(dCol)
If Len(CStr(dCell.Value)) = 0 Then
If drg Is Nothing Then
Set drg = dCell
Else
Set drg = Union(drg, dCell)
End If
End If
Next sCell
' All cells already contain a date.
If drg Is Nothing Then Exit Sub
Dim dDate As Date: dDate = Now ' after prooving that it works, use 'Date'
' To prevent retriggering this event and any other event while writing.
Application.EnableEvents = False
' Write in one go.
drg.Value = dDate
SafeExit:
' Enable events 'at any cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub