繁体   English   中英

自动保存文本框中的文本

[英]Auto Saving text from textbox

我的工作簿中有一张工作表,用于记录对另一张工作表的更改。 它不会记录对工作表上文本框的更改。

我试图在保存时将信息保存在文本框中。

我尝试使用我用来记录其他更改的代码,但该代码不起作用。 我没有收到任何错误,但文本没有保存在单元格中。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim sSheetName As String
    Dim Narrative As Object
    sSheetName = "1107"
    If ActiveSheet.Name <> "LogDetails" Then
        Sheets("LogDetails").Unprotect
        Application.EnableEvents = False
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = "Narraive Box"
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = Sheets("1107").Narrative.Text
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Environ("username")
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Now
    
        Sheets("LogDetails").Columns("A:D").AutoFit
        Application.EnableEvents = True

    End If
End Sub

文本框不是 ActiveX 文本框,只是一个普通的文本框。

我现在看到事件没有发生,因为If ActiveSheet.Name <> "LogDetails" Then
当我删除 if 语句时,大部分代码都可以工作,但它不会从 Narrative 文本框中复制文本。

当前代码。 一旦我复制了文本框,我就会担心触发事件。

    Dim sSheetName As String
    Dim logSheet As Worksheet
    Dim logRow As Range
    Set logSheet = Sheets("LogDetails")
    Dim Narrative As Object
    sSheetName = "1107"

    logSheet.Unprotect
    Application.EnableEvents = False
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = "Narrative Box"
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = Sheets("1107").Shapes("Narrative").TextFrame.Characters.Text
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Environ("username")
    Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Now
            
    Sheets("LogDetails").Columns("A:D").AutoFit
    Application.EnableEvents = True
  
End Sub

我的主要问题是我正在使用调用对象而不是形状的脚本。 获得最终答案令人困惑,因为其他问题也在不同时间混入其中。 就像它没有复制信息一样,因为文本框中没有信息。

如果您的 TextBox 是经典形状,您可能需要使用Shapes("Narrative").TextFrame.Characters.Text

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim logSheet as Worksheet
    Dim logRow as Range
    Set logSheet = Sheets("LogDetails")
    If ActiveSheet.Name <> "LogDetails" Then
        logSheet.Unprotect
        Application.EnableEvents = False
        Set logRow = Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        logRow.Value = "Narrative Box"
        logRow.Offset(0, 1).Value = Sheets("1107").Shapes("Narrative").TextFrame.Characters.Text
        logRow.Offset(0, 2).Value = Environ("username")
        logRow.Offset(0, 3).Value = Now
        logSheet.Columns("A:D").AutoFit
        Application.EnableEvents = True
    End If
End Sub

您是否尝试过使用 ActiveX 文本框?

你可以试试这个

Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = ThisWorkbook.Sheets("1107").TextBox1.Text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM