如何从用户表单中检索值

问题描述

我正在尝试编写代码以跟踪用户在任何工作表上所做的所有更改。用户将输入数据,并且会不时擦除这些数据和/或纠正他们插入的原始值。如果更改是删除修改,则将弹出一个用户窗体,并且用户将说明更改的原因。现在,我可以在用户每次进行前面提到的更改之一时显示表单,但是我无法检索原因,你们可以帮我吗?

这是我为用户表格准备的

Private Sub CommandButton1_Click()
Dim msgvalue As VbMsgBoxResult
Dim value As String
msgvalue = MsgBox("Do you wish to save the change?",vbYesNo + vbinformation,"Confirm")
    If msgvalue = vbNo Then GoTo Command 
    If msgvalue = vbYes Then
        value = UserForm1.txtCmmt.Value
        If value = "" Then GoTo Command
        End
    End If
Command:
MsgBox ("A reason must be provided")
    With UserForm1
        .txtCmmt.Value = ""
    End With
End Sub

因此,如果用户尝试删除值,则代码如下:

Private Sub Workbook_SheetChange(ByVal Sh As Object,ByVal Target As Range)

Dim sLastAction As String
Dim Cell As Range
sLastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
For Each Cell In Target
    If sLastAction = "Clear" Or sLastAction = "Delete" Or Left(sLastAction,9) = "Typing ''" Then
       
        UserForm1.Show 'this is where I'm stuck,I'm not sure how to retrieve the value from the form
    End If
'the code continues to retrieve other info from the changes made,including the "reason"

感谢您的帮助!

解决方法

请尝试以下方法:

  1. 让我们说您将在其中写评论的文本框名为“ txtComment”。
  2. 将此代码放入其Exit事件中:
Private Sub txtComment_Exit(ByVal Cancel As MSForms.ReturnBoolean)
 If Me.txtComment.text <> "" Then
    If ActiveSheet.Name <> "LogDetails" Then
        Application.EnableEvents = False
         Sheets("LogDetails").Range("A" & rows.count).End(xlUp).Offset(0,5).Value = Me.txtComment.text
        Application.EnableEvents = True
        Unload Me
    End If
 End If
End Sub

让现有的Worksheet_Change事件保持原样,仅启动表单,并可能创建Public布尔变量(来自标准模块)True(某事boolStop),这将不允许更改任何工作表中的任何内容,直到没有False

然后在文本框中填写所需的文本(“ txtComment”,或按您的名字命名),然后按Enter。如果上述建议对您来说很有趣,则文本框事件的最后一行将是boolStop = False

如果您了解如何实现上述简单解决方案,则可能会忘记用户表单,而使用简单的InputBox,如下例所示:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0,0) <> "E1" Then Exit Sub
    Dim sLastAction As String,comValue As String
    Dim Cell As Range
    sLastAction = Application.CommandBars("Standard").Controls("&Undo").list(1)
    For Each Cell In Target
        If sLastAction = "Clear" Or sLastAction = "Delete" Or left(sLastAction,9) = "Typing ''" Then
WritePlease:
            comValue = InputBox("Please write the reason for cell """ & Cell.Address & """ (" & sLastAction & ").","Reason,please")
              If comValue = "" Then GoTo WritePlease
              If ActiveSheet.Name <> "LogDetails" Then
                  Application.EnableEvents = False
                    'Stop
                   Sheets("LogDetails").Range("A" & rows.count).End(xlUp).Offset(0,5).Value = comValue
                  Application.EnableEvents = True
              End If
        End If
    Next
End Sub

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...