Menu update

  • Thread starter Thread starter donh
  • Start date Start date
D

donh

Hi there,

I've had a go and created a couple of additions to the menu bar using
VBA. Some of the captions are based on a cell ref which contains a
variable date in text form. I need to call my menu procedure when the
cell contents that I have refrenced to change (a new date is entered),
but I don't know how.

Hope you can help

DonH
 
You would use worksheet change event code, but you would need the name of
the menu and the sub-menu item to change it. Something like


Const WS_RANGE As String = "H10" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Application.CommandBars("myCB").Controls("myButton").Caption =
..Value
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
Missed the first line of the code

Private Sub Worksheet_Change(ByVal Target As Range)

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
Bob,

Had a look and didn't understand. Have copied part of my VBA below.
Can you explain a little more please.

Many thanks

DonH

Month1 = Worksheets("Setup").Range("I5")
MyYear1 = (Month1)

Set MenuItem = NewMenu2.Controls.Add _
(Type:=msoControlButton)
With MenuItem
..Caption = MyYear1
..OnAction = "Period1"
..FaceId = 2114
End With
 
This is problematical, as you will have no idea what the menu caption is at
any time.

Presuming that your menu is recreated every time you open the workbook, make
MenuItem a global variable, and then use that in the code


Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H10" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
MenuItem.Caption = .Value
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Variable name for VBA Command Button 8
Update Now() 9
Text into a VBA Text Box 6
Control Source Property 2
Excel Import Comments 3
Updating Form Information 1
Change file ref in VBA 2
Finding last row data in other worksheet 15

Back
Top