Dynamically Change an ActiveX Label Caption based on the Value of another Cell

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
I have a very simple label that reads a date from a hidden cell in the sheet. The hidden cell is AB1. I am having trouble trying to get the caption of ActiveX Label1 to change value when the value of AB1 changes.
This is really quite easy with UserForms but with ActiveX I don't see any easy options. I can get it to change by clicking or moving mouse over but that's not what I want.

This is the code I've tried within a Worksheet_Change module.

VBA Code:
  'Dynamically change ActiveX Label1 Caption to match cell value of AB1
    
    Dim MyLabel As OLEObject
    Set MyLabel = Sheet1.OLEObjects("Label1")
    
    If Not Intersect(Target, Sheet1.Range("AB1")) Is Nothing Then
          MyLabel.Object.Caption = Format(Target.Value, "mmm d, yyyy")
    End If
    
End Sub

Any help much appreciated. Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I have also tried using this code as it works for caption of an ActiveX command button but it doesn't work either

VBA Code:
Dim xShapeRg As ShapeRange, xStr As String
    On Error Resume Next
    If Not Application.Intersect(Target, Range("AB1")) Is Nothing Then
        Sheet1.Label1.Caption = Format(Target.Value, "mmm d, yyyy")
        Set xShapeRg = ActiveSheet.Shapes.Range(xStr)
        If xShapeRg Is Nothing Then Set xShapeRg = ActiveSheet.Shapes.Range("Label1")
        Application.EnableEvents = False
        xShapeRg.Select
        Application.EnableEvents = True
        Selection.Name = Target.Text
    End If
      
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
 Dim xStr As String
 If (Not Intersect(Target, Range("AB1")) Is Nothing) And Target.Value <> "" Then
        xStr = Target.Text
    End If
End Sub
 
Upvote 0
I fixed it. It was the placement of the code within the worksheet module in relation to other code. Also, the object was not named properly. This was the final solution.
VBA Code:
 If Not Intersect(Target, Range("AB1")) Is Nothing Then
    Sheet1.Label1.Object.Caption = Format(Sheet1.Range("AB1").Value, "mmm d, yyyy")
 End If
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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