从用户表单更新后,如何使用单元格上的注释框跟踪更改?

时间:2018-08-29 18:46:44

标签: excel excel-vba userform

我正在尝试编写VBA宏,以在用户搜索和更新数据时自动在单元格上以某种颜色显示注释框,从而在单独的工作表中跟踪工作表的更改(显示更改的历史记录)。用户表单。

以下代码用于搜索和更新:

“此代码用于更新用户表单中的数据”

Private Sub cmdupdate_Click()
If Me.TextBox1.Value = "" Then
MsgBox "SL No Can Not be Blank!!!", vbExclamation, "SL No"
Exit Sub
End If
SLNo = Me.TextBox1.Value
Sheets("Sheet1").Select
Dim rowselect As Double
rowselect = Me.TextBox1.Value
rowselect = rowselect + 1
Rows(rowselect).Select
Cells(rowselect, 2) = Me.TextBox2.Value
Cells(rowselect, 3) = Me.TextBox3.Value
Cells(rowselect, 4) = Me.TextBox4.Value
Cells(rowselect, 5) = Me.TextBox5.Value
Cells(rowselect, 6) = Me.TextBox6.Value

End Sub

``下面的代码用于从excel工作表中搜索并显示在用户窗体中''

Private Sub cmdSearch_Click()
Do
        DoEvents
        row_number = row_number + 1
        item_in_review = Sheets("Sheet1").Range("A" & row_number)
        If item_in_review = TextBox1.Text Then
            TextBox2.Text = Sheets("Sheet1").Range("B" & row_number)
            TextBox3.Text = Sheets("Sheet1").Range("C" & row_number)
            TextBox4.Text = Sheets("Sheet1").Range("D" & row_number)
            TextBox5.Text = Sheets("Sheet1").Range("F" & row_number)
            TextBox6.Text = Sheets("Sheet1").Range("E" & row_number)
        End If
    Loop Until item_in_review = ""
End Sub

现在,我尝试使用用户窗体更新excel工作表后,添加以下代码来跟踪更改,但此行“ Target.Comment.Text Text:= OldVal”中出现错误,无法获得解决方案以完成我的操作任务。

Sub Worksheet_Change(ByVal Target As Range)
Dim X As Integer
    Set Wb = ThisWorkbook
        ShtName = "Edits Log"
    If Target.Cells.Count > 1 Then Exit Sub
    X = EndRow + 1
    Wb.Sheets(ShtName).Range("A" & X).Value = ActiveSheet.Name
    Wb.Sheets(ShtName).Range("B" & X).Value = Target.Address
    Wb.Sheets(ShtName).Range("C" & X).Value = OldVal
    Wb.Sheets(ShtName).Range("D" & X).Value = Target.Value
    Wb.Sheets(ShtName).Range("E" & X).Value = Now()
    Wb.Sheets(ShtName).Range("F" & X).Value = Environ("username")
    Target.Interior.ColorIndex = 6
    On Error Resume Next
    Target.AddComment
    On Error GoTo 0
    Target.Comment.Visible = False
    Target.Comment.Text Text:=OldVal
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    OldVal = Target.Value
End Sub

1 个答案:

答案 0 :(得分:0)

从注释中抛出错误,因为oldValEmpty。更改以前的空白单元格后,将触发Selection Change,并且由于该单元格中没有先前的值,因此oldVal将是Empty

Worksheet Change代码需要处理这种可能性,以及Target包含错误的可能性-例如#值!或#N / A。

去掉写入“编辑日志”标签的部分,您的Worksheet Change可能类似于以下代码:

Option Explicit

Public oldVal 'should be a Variant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    oldVal = Target.Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub

    Target.Interior.ColorIndex = 6

    On Error Resume Next
    Target.AddComment
    On Error GoTo 0

    With Target.Comment
        .Visible = False
        If Not IsEmpty(oldVal) And Not IsError(oldVal) Then
            .Text CStr(oldVal)
        Else
            .Text "Previously blank or an error"
        End If
    End With
End Sub