Excel VBA real time UserForm Labels from Workbook cells

mrwad

New Member
Joined
Oct 16, 2018
Messages
49
I have userform called "DisplaySummaryForm" that meant to display live information about project being calculated. I have Labels to display values from worksheet cells. Now I have to reopen UserForm all the time to get my values updated or press Refresh button on userform. How they can be updated all the time? So they are so called "real time" in opened UserForm?


Button for opening UserForm:


Code:
Sub DisplaySummary()


DisplaySummaryForm.Show vbModless


End Sub


UserForm code:


Code:
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


I know I can use something like:


Code:
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


But the problem is that I am not updating my cell Q148 "manually" but by formula. In cell Q148 I have something like =A1+A6+A7+A8*23 etc. Above Macro does not work in this case.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
If you aren't manually updating any values you could use the Calculate event.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...m-labels-from-workbook-cells.html#post5001232

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Personally, I would try to keep all the code in the userform itself, like this:

Code:
Option Explicit
Private WithEvents ws As Worksheet
Private Sub CommandButton1_Click()

    Unload Me

End Sub
Private Sub UserForm_Initialize()
    Set ws = ThisWorkbook.Sheets("Price calculation")
    PopulateControls
End Sub

Private Sub ws_Calculate()
    PopulateControls
End Sub
Sub PopulateControls()
    With ThisWorkbook.Sheets("MAIN")
        Controls("Label11").Caption = .Range("D11").Value
        Controls("Label12").Caption = .Range("D14").Value
    End With

    With ws
        Me.TextBox2.Value = .Range("I148").Value
        Controls("Label14").Caption = .Range("Q148").Value
        Controls("Label15").Caption = .Range("Q148").Value
        Controls("Label18").Caption = .Range("Q148").Value
        Controls("Label16").Caption = .Range("Q148").Value
        Controls("Label17").Caption = .Range("Q148").Value
        Controls("Label20").Caption = .Range("Q148").Value
        Controls("Label22").Caption = .Range("Q148").Value
    End With
End Sub
 
Upvote 0
Thanks to everyone. I came up with this solution.


Code:
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top