Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / December 2006

Tip: Looking for answers? Try searching our database.

Variable name for VBA Command Button

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
donh - 20 Dec 2006 09:59 GMT
Hi,

Thought I had finished, but a user of my spreadsheet has accidently
removed a hyperlink from the Index of the project I've been working on.
To ensure this doesn't happen again I'm swapping cell based hyperlinks
for VBA Command Buttons.  These have been fine but the last few are
based on variable data that appears in cells when the sheet is setup.
for example  Sheet 1 Cell A1 contains Jan-Feb cell A2 contains Mar-Apr
etc. The contents of these cells change each year, so I guess I need a
formula in the caption to point at these cells but don't know the
formatt it should take.

Any help would be gratefully received

DonH
Bob Phillips - 20 Dec 2006 11:49 GMT
Don,

You could do it with event code that traps  a change in cells A1:A4, and
changes the appropriate button accordingly

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

   On Error GoTo ws_exit
   Application.EnableEvents = False

   If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
       With Target
           Select Case .Address(False, False)
               Case "A1": Me.Buttons("Button 1").Caption = .Value
               Case "A2": Me.Buttons("Button 2").Caption = .Value
               Case "A3": Me.Buttons("Button 3").Caption = .Value
               Case "A4": Me.Buttons("Button 4").Caption = .Value
           End Select
       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

This assumes that the buttons are Forms buttons.

Signature

---
HTH

Bob

(change the xxxx to gmail if mailing direct)

> Hi,
>
[quoted text clipped - 11 lines]
>
> DonH
donh - 20 Dec 2006 12:34 GMT
Bob,

I have added your code to the VBA page where my Index is.  So far no
luck in getting any further.  My buttons were/are VBA Command Buttons
not Form Buttons but have tried creating Form buttons and renaming
those Button 1 etc and then updating cell A1, so far  without any
change to the buttons name.

Could you please let me know where I might be going wrong.  Also if
possible can this also be done with VBA command buttons.  I wanted to
go the VBA route, if it worked, to protect the buttons from accidental
editing or deletion.

Many thanks

Don H

> Don,
>
[quoted text clipped - 51 lines]
> >
> > DonH
donh - 20 Dec 2006 12:49 GMT
Bob,

Forget that.  been playing some more and don't think I have done
anything different but that now works thank you.  I also recognise my
comment on protection is wrong as I am able to protect the Form button
too.

However :-)  if this can be done with VBA Command Buttons I would be
interested as this would help with the colour formatting to group my
index subjects

> Bob,
>
[quoted text clipped - 12 lines]
>
> Don H
Bob Phillips - 20 Dec 2006 12:59 GMT
Here you go Don, control toolbox buttons

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

   On Error GoTo ws_exit
   Application.EnableEvents = False

   If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
       With Target
           Select Case .Address(False, False)
               Case "A1": Me.OLEObjects("CommandButton1").Object.Caption =
.Value
               Case "A2": Me.OLEObjects("CommandButton2").Object.Caption =
.Value
               Case "A3": Me.OLEObjects("CommandButton3").Object.Caption =
.Value
               Case "A4": Me.OLEObjects("CommandButton4").Object.Caption =
.Value
           End Select
       End With
   End If

ws_exit:
   Application.EnableEvents = True
End Sub

Signature

---
HTH

Bob

(change the xxxx to gmail if mailing direct)

> Bob,
>
[quoted text clipped - 23 lines]
>>
>> Don H
donh - 20 Dec 2006 13:23 GMT
Bob,

Thanks for being so quick. Sorry to say I still have a problem!  I
found out what I had done differently to make it work, which is to
update the cell directly.

My worksheet is a works time sheet and has been designed to be used for
the next 10 years.  The pattern of work is held in a table (as it
doesn't fall nicely into a monthly cycle).

A  year start date is selected from a drop down, from there each period
date is set using a lookup to the work table.  It is these period dates
I'm wishing to capture in the buttons.  So no cell is actually typed
into.  Starts with a drop down and completed by lookups.

Is there a way around this?

Many thanks

Don

> Here you go Don, control toolbox buttons
>
[quoted text clipped - 22 lines]
>     Application.EnableEvents = True
> End Sub
Bob Phillips - 20 Dec 2006 15:03 GMT
Don,

Try this approach. It is less efficient as it happens every calculate, but
should work

Private Sub Worksheet_Calculate()

   If Not IsError(Me.Range("A1").Value) Then _
       If Not IsEmpty(Me.Range("A1").Value) Then _
           Me.OLEObjects("CommandButton1").Object.Caption =
Me.Range("A1").Value
   If Not IsError(Me.Range("A2").Value) Then _
       If Not IsEmpty(Me.Range("A2").Value) Then _
           Me.OLEObjects("CommandButton2").Object.Caption =
Me.Range("A2").Value
   If Not IsError(Me.Range("A3").Value) Then _
       If Not IsEmpty(Me.Range("A3").Value) Then _
           Me.OLEObjects("CommandButton3").Object.Caption =
Me.Range("A3").Value
   If Not IsError(Me.Range("A4").Value) Then _
       If Not IsEmpty(Me.Range("A4").Value) Then _
           Me.OLEObjects("CommandButton4").Object.Caption =
Me.Range("A4").Value

End Sub

Signature

---
HTH

Bob

(change the xxxx to gmail if mailing direct)

> Bob,
>
[quoted text clipped - 47 lines]
>>     Application.EnableEvents = True
>> End Sub
donh - 20 Dec 2006 15:25 GMT
Bob,

Bit confused, you mention in your post that it is less efficient
because it happens every calculate.  Doesn't seem to!

Not that I mind.  As this one is a Sub() I can execute I'll just have
an update button to run it when the year is set.

Thank you very much for your time and effort.

Don H

> Don,
>
[quoted text clipped - 21 lines]
>
> End Sub
donh - 20 Dec 2006 15:46 GMT
Bob,

I must confess I talk dribble.  In future I shall make an effort to
check what I'm doing before replying to a post.  Your suggestion seems
to work fine, haven't a clue why it didn't before.

As for being able to execute it, that at least for me, was dribble too.

If anyone would like to buy me a good book for Christmas, perhaps on
something else I'm more suited to, that would be nice.

I'm off to lie down in a dark room.

DonH

> Bob,
>
[quoted text clipped - 7 lines]
>
> Don H

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.