【问题标题】:Excel VBA real time UserForm Labels from Workbook cells来自工作簿单元格的 Excel VBA 实时用户窗体标签
【发布时间】:2018-10-25 13:12:56
【问题描述】:

我有一个名为“DisplaySummaryForm”的用户表单,用于显示有关正在计算的项目的实时信息。我有标签来显示工作表单元格中的值。现在我必须一直重新打开 UserForm 才能更新我的值。它们如何随时更新?所以他们在打开的用户窗体中被称为“相关时间”?

打开用户窗体的按钮:

Sub DisplaySummary()

DisplaySummaryForm.Show vbModless

End Sub

用户窗体代码:

Private Sub CommandButton1_Click()

Unload Me

End Sub
Private Sub UserForm_Initialize()

Controls("Label11").Caption = ThisWorkbook.Sheets("MAIN").Range("D11").value
Controls("Label12").Caption = ThisWorkbook.Sheets("MAIN").Range("D14").value

Me.TextBox2.value = ThisWorkbook.Sheets("Price calculation").Range("I148").value

Controls("Label14").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label15").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label18").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label16").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label17").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label20").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
Controls("Label22").Caption = ThisWorkbook.Sheets("Price calculation").Range("Q148").value
End Sub

【问题讨论】:

    标签: excel label userform


    【解决方案1】:

    我相信 Excel 中没有这样的选项可以自动更新用户窗体中的值,但是您可以在使用以下代码更新单元格时卸载并加载 DisplaySummaryForm,并根据需要稍作修改。

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    
    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("Q148")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
    
        ' Display a message when one of the designated cells has been 
        ' changed.
        ' Place your code here.
        MsgBox "Cell " & Target.Address & " has changed."
    End If
    End Sub
    

    Reference 以上代码

    【讨论】:

    • 感谢您的回复!如果我自己编辑单元格,这将有效。问题是我在单元格 Q148 中有一个公式,并且此代码无法以这种方式识别单元格中的更改。
    【解决方案2】:

    感谢大家。我想出了这个解决方案:

    Private Sub Worksheet_Calculate()
        Dim KeyCell1 As Range
        Dim KeyCell2 As Range
        ' The variable KeyCells contains the cells that will
        ' cause an alert when they are changed.
        Set KeyCell1 = Range("Q148")
        Set KeyCell2 = Range("Q149")
        Set KeyCell3 = Range("Q150")
        Set KeyCell4 = Range("Q151")
        Set KeyCell5 = Range("Q152")
        Set KeyCell6 = Range("Q156")
            ' Display a message when one of the designated cells has been
            ' changed.
            DisplaySummaryForm.Controls("Label14").Caption = Format(KeyCell1.Value, "#,##0.00")
            DisplaySummaryForm.Controls("Label15").Caption = Format(KeyCell2.Value, "#,##0.00")
            DisplaySummaryForm.Controls("Label16").Caption = Format(KeyCell3.Value, "#,##0.00")
            DisplaySummaryForm.Controls("Label17").Caption = Format(KeyCell4.Value, "#,##0.00")
            DisplaySummaryForm.Controls("Label18").Caption = Format(KeyCell5.Value, "#,##0.00")
            DisplaySummaryForm.Controls("Label20").Caption = Format(KeyCell6.Value, "#,##0.00")
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2019-06-02
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多