首页 > 解决方案 > 是否可以立即从表单更新 Excel 单元格,而不需要按下更新按钮?

问题描述

我们有一个用于退休社区继续教育计划的预注册 Excel 电子表格数据库。数据库有 37 个数据列,其中 3 个是计算出来的,一个是日期格式,一个是 0 或 1,作为居民是否注册讲座/课程的指标(实际上是 1 或字段留空)。它目前有 1126 行,反映了 1123 名居民。

如果居民预先注册,他们大多为课程支付 1 美元,但有些活动是音乐表演,预付 8 美元。如果他们在没有预先注册的情况下出现,他们分别支付 2 美元和 9 美元。此外,有些课程是不止一次的活动,这些活动的注册费用可能高达 111 美元,因此需要计算一下居民需要在他们的注册表中发送多少支票。支票金额是总计栏。我们也有一次性活动的小计和多日课程的小计。

在此处输入图像描述

我发现了一个很好的最近已停用的大部分免费 Excel 插件,它允许无限数量的字段,因此表单字段的 32 个限制不是问题。我破解了密码,所以我可以访问表单的 VBA,并且能够添加几个按钮,例如,一个为所有晚间活动填写一个的按钮,因为这是收银机的常见选择。我很乐意向程序员支付 15 美元,以换取他曾经收取的密码特权,并将尝试与他取得联系。

我想知道的是表单是否可以立即更新电子表格和计算字段,然后立即更新表单上的计算字段。我确实有一个更新按钮,但希望在输入数据时更新电子表格,以防输入者忘记按下更新按钮。我意识到将数据直接输入电子表格可以实现这一点,但我想坚持使用表格。

我认为这将涉及以某种方式识别表单状态的变化,但我不知道如何编写 VBA 来做到这一点。一旦按下更新按钮,我已经能够弄清楚如何在表格中重新输入计算出的金额,然后可以将其与表格上显示的总金额中的居民支票金额进行比较。

这是我相信将插入相关 VBA 或基于一个或两个子程序的单独子程序的位置:

Sub UpdateForm()
    'This sub updates the fields in the form
    Dim ctl As Control
    Dim Col As Long
    Dim CurrentCell As Range
    Col = 0

    On Error Resume Next
    For Each ctl In Frame1.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
            Col = Col + 1
            Set CurrentCell = Cells(CurrentRecord + RowOffset, Col + ColumnOffset)
            ctl = CurrentCell
            If CurrentCell.PrefixCharacter = "'" Then ctl = "'" & ctl

            'Check for True/False cells (they would appear as 0 or -1)
            If Application.WorksheetFunction.IsLogical(CurrentCell) Then
                ctl = CurrentCell.Text
            End If

            'Is the cell displaying an error value?
            If Err <> 0 Then
                ctl = CurrentCell.Text 'Display this if the cell has an error value
                Err = 0
            End If

'           Date?
            If IsDate(Cells(CurrentRecord + RowOffset, Col + ColumnOffset)) Then
                ctl = CurrentCell.Text
            End If

            'Formula?
            If Cells(CurrentRecord + RowOffset, Col + ColumnOffset).HasFormula Then
                ctl = FormatCurrency(CurrentCell)
                ctl.Enabled = False
                ctl.BackColor = RGB(240, 240, 240)
            Else
                ctl.Enabled = True
                ctl.BackColor = RGB(255, 255, 255)
            End If
        End If
    Next ctl
    LabelRecNum = Text(9) & " " & CurrentRecord & " " & Text(10) & " " & RecordCount
    On Error GoTo 0
End Sub

Sub UpdateDatabase()
    'Updates the database with new data from the form
    Dim ctl As Control
    Dim Col As Long
    Dim TestCell As Range
    Dim NumberWritten As Long
    Col = 0
    NumberWritten = 0
    For Each ctl In Frame1.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
            Col = Col + 1
            Set TestCell = Cells(CurrentRecord + RowOffset, Col + ColumnOffset)
            If Not TestCell.HasFormula Then ' Don't check formula cells
                'Use Clean so cells with non-printing characters will be compared correctly
                If TestCell.PrefixCharacter & Application.WorksheetFunction.Clean(TestCell) <> Application.WorksheetFunction.Clean(ctl.Text) Then
                    'Save original data for undo
                    NumberWritten = NumberWritten + 1
                    ReDim Preserve UndoArray(1 To NumberWritten)
                    With UndoArray(NumberWritten)
                        .Address = TestCell.Address
                        .Contents = TestCell.PrefixCharacter & TestCell.Text
                        .RecNum = CurrentRecord
                    End With
                    'write the new data
                    '(date check code is new in v3) - revised Aug 6-08
                    If IsRealDate(ctl.Text) Then
                        If IsDate(DateValue(ctl.Text)) Then
                            TestCell.Value = CDate(ctl.Text)
                        End If
                    Else
                        TestCell.Value = ctl.Text
                    End If
                End If
            End If
        End If
    Next ctl

    If NumberWritten <> 0 Then
        UndoButton.Caption = Text(18) '"Undo Entry"
        UndoButton.Visible = True
    End If
End Sub

此外,如果有人对如何更有效地收集数据和计算有任何建议,我很乐意听到。我们现在使用带有复选框的手动表格,并依靠老年人来计算他们的支票金额。复选标记手动传输到 Excel 电子表格。我建议在课程目录中放置一个计算机可读的表格,例如你找到的用于参加标准化考试的表格,但由于对老年人来说太复杂而被否决。我不同意,也想试试,但我是新来的,不是负责人,所以无法实施实验。

按 PEH 编辑:这是不起作用的代码 -

Sub FormMain_Change()
     UpdateDatabase 
     Call UpdateForm 
End Sub

在 UpdateForm 代码块中将“.ControlSource”添加到我的控件(“ctl”)也不能解决问题。

标签: excelvbareal-timecalculated-columnsdata-entry

解决方案


推荐阅读