Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Unless you are referring to a Form Control, a control button has a property
called Caption, that is the name that appears on the button, not its's name which is used by Excel and VBA code to identifiy it. SO to change the caption, use CommandButton1.Caption = "My Button" To link this to a cell, you could use CommandButton1.Caption = Range("A1") To run the above manually store it in the code sheet for the worksheet where the control is placed. If you want it to automatically change it when the cell is changed, use the change event, something like Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Range("A1").Value End If End Sub -- Regards, Nigel "Tanya" wrote in message ... Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Nigel
I am trying to change the display text on the Commandbutton1 to read TEXT in cell reference sheet1!B2 Is this possible? I have several command buttons on this one sheet, each taking the user to a designated sheet, namely class 1, class 2 etc. The idea is a teacher can see at a glance which class is which. You may wonder why I haven't simply changed the sheet tap names, and I tried this originally, however, on a couple of the sheets I wanted to look up values in other sheets and created a vlookup. This required labelling each sheet and wouldn't allow me to then change the tabs, hence command button approach. Also, I thought this might be more user friendly as there are 10 classes in each workbook. Regards Tanya "Nigel" wrote: Unless you are referring to a Form Control, a control button has a property called Caption, that is the name that appears on the button, not its's name which is used by Excel and VBA code to identifiy it. SO to change the caption, use CommandButton1.Caption = "My Button" To link this to a cell, you could use CommandButton1.Caption = Range("A1") To run the above manually store it in the code sheet for the worksheet where the control is placed. If you want it to automatically change it when the cell is changed, use the change event, something like Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Range("A1").Value End If End Sub -- Regards, Nigel "Tanya" wrote in message ... Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Nov 27, 1:21 am, Tanya wrote:
Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya Of course. Let's say your title cell is A1, and your command button is named in code as CommandButton1. In the source code of the sheet object, enter this code in the Worksheet_Change event: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Target.Value End If End Sub Hopefully, it's as simple as that, but if not - post back more questions. Cheers, -Basilisk96 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Hi I did what you suggested but must be missing something. Private Sub Worksheet_Change(ByVal Target As Range) 'Target Address is on a separate sheet, I don't think I have done this correctly If Target.Address = "Sheet1!B2" Then CommandButton1.Caption = Target.Value End If End Sub I guess it sounds like a weird thing to want to do, but on my startup sheet I have a number of command buttons each linked to a separate sheet [classe lists] Thanking you in advance. Regards Tanya "Basilisk96" wrote: On Nov 27, 1:21 am, Tanya wrote: Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya Of course. Let's say your title cell is A1, and your command button is named in code as CommandButton1. In the source code of the sheet object, enter this code in the Worksheet_Change event: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Target.Value End If End Sub Hopefully, it's as simple as that, but if not - post back more questions. Cheers, -Basilisk96 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
[12:48AM!?]
The Private Sub Worksheet_Change(ByVal Target As Range) goes in Sheet1; and you need Sheets("sheetX").CommandButton1.Caption = Target.Value D-C Dave Tanya wrote: I did what you suggested but must be missing something. Private Sub Worksheet_Change(ByVal Target As Range) 'Target Address is on a separate sheet, I don't think I have done this correctly If Target.Address = "Sheet1!B2" Then CommandButton1.Caption = Target.Value End If End Sub I guess it sounds like a weird thing to want to do, but on my startup sheet I have a number of command buttons each linked to a separate sheet [classe lists] "Basilisk96" wrote: Of course. Let's say your title cell is A1, and your command button is named in code as CommandButton1. In the source code of the sheet object, enter this code in the Worksheet_Change event: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Target.Value End If End Sub On Nov 27, 1:21 am, Tanya wrote: Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Dave
I'm still missing something, when I try to run this macro it brings up a dialogue box which wants choose a macro to run. Sheet 33 has the commandButton1 which has the following code: Private Sub CommandButton1_Click() 'Unprotect workbook UnProtect_Workbook 'Show Task Weights Sheets("1").Visible = True Sheets("1").Select 'Protect workbook Protect_Workbook End Sub Private Sub Worksheet_Change(ByVal Target As Range) Sheets("sheet1").CommandButton1.Caption = Target.Value If Target.Address = "B2" Then CommandButton1.Caption = Target.Value End If End Sub Thanks Tanya "Dave D-C" wrote: [12:48AM!?] The Private Sub Worksheet_Change(ByVal Target As Range) goes in Sheet1; and you need Sheets("sheetX").CommandButton1.Caption = Target.Value D-C Dave Tanya wrote: I did what you suggested but must be missing something. Private Sub Worksheet_Change(ByVal Target As Range) 'Target Address is on a separate sheet, I don't think I have done this correctly If Target.Address = "Sheet1!B2" Then CommandButton1.Caption = Target.Value End If End Sub I guess it sounds like a weird thing to want to do, but on my startup sheet I have a number of command buttons each linked to a separate sheet [classe lists] "Basilisk96" wrote: Of course. Let's say your title cell is A1, and your command button is named in code as CommandButton1. In the source code of the sheet object, enter this code in the Worksheet_Change event: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Target.Value End If End Sub On Nov 27, 1:21 am, Tanya wrote: Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Tanya,
In understand the procedure, Private Sub CommandButton1_Click() is on Sheet 33. Where is the procedure, Private Sub Worksheet_Change(ByVal Target As Range) located? I'm trying to reproduce your problem, but I'm unable to. Are either of these the macro that returns the dialog box? Dan D. On Nov 27, 12:32 pm, Tanya wrote: Hi Dave I'm still missing something, when I try to run this macro it brings up a dialogue box which wants choose a macro to run. Sheet 33 has the commandButton1 which has the following code: Private Sub CommandButton1_Click() 'Unprotect workbook UnProtect_Workbook 'Show Task Weights Sheets("1").Visible = True Sheets("1").Select 'Protect workbook Protect_Workbook End Sub Private Sub Worksheet_Change(ByVal Target As Range) Sheets("sheet1").CommandButton1.Caption = Target.Value If Target.Address = "B2" Then CommandButton1.Caption = Target.Value End If End Sub Thanks Tanya "Dave D-C" wrote: [12:48AM!?] The Private Sub Worksheet_Change(ByVal Target As Range) goes in Sheet1; and you need Sheets("sheetX").CommandButton1.Caption = Target.Value D-C Dave Tanya wrote: I did what you suggested but must be missing something. Private Sub Worksheet_Change(ByVal Target As Range) 'Target Address is on a separate sheet, I don't think I have done this correctly If Target.Address = "Sheet1!B2" Then CommandButton1.Caption = Target.Value End If End Sub I guess it sounds like a weird thing to want to do, but on my staHrtup sheet I have a number of command buttons each linked to a separate sheet [classe lists] "Basilisk96" wrote: Of course. Let's say your title cell is A1, and your command button is named in code as CommandButton1. In the source code of the sheet object, enter this code in the Worksheet_Change event: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then CommandButton1.Caption = Target.Value End If End Sub On Nov 27, 1:21 am, Tanya wrote: Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Tanya,
Paste the following code in the ThisWorkbook module. Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Const SHEET_WITH_BUTTON = "Sheet1" '<<< CHANGE. Const SHEET_WITH_CHANGE_CELL = "Sheet2" '<<< CHANGE Const CHANGE_CELL_ADDRESS = "$A$1" '<<< CHANGE Const BUTTON_NAME = "Button1" '<<< CHANGE If StrComp(Sh.Name, SHEET_WITH_CHANGE_CELL, vbTextCompare) = 0 Then If Target.Address = CHANGE_CELL_ADDRESS Then Me.Worksheets(SHEET_WITH_BUTTON).OLEObjects(BUTTON _NAME). _ Object.Caption = Target.Text End If End If End Sub Change the value of SHEET_WITH_BUTTON to the name of the worksheet on which the command button resides. Change the value of SHEET_WITH_CHANGE_CELL to the name of the worksheet containing the cell whose text you want to assign to the button. Change CHANGE_CELL_ADDRESS to the address of the cell that triggers the change of the button's caption. The '$' characters are required as shown. Change BUTTON_NAME to the name of the button whose text is to change. -- Cordially, Chip Pearson Microsoft MVP - Excel, 10 Years Pearson Software Consulting www.cpearson.com (email on the web site) "Tanya" wrote in message ... Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Chip
An earlier post appeared to work, however I found that when I edited other cells in the worksheet the commandbutton would be updated, no idea how when I had specified the target cell Anyway, I tried what you suggested: Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Const SHEET_WITH_BUTTON = "Setup" <<NAME OF SHEET CONTAINING BUTTON Const SHEET_WITH_CHANGE_CELL = "Sheet1" '<<< NAME OF SHEET WITH CELL Const CHANGE_CELL_ADDRESS = "$B$1" '<<< CELL REFERENCE Const BUTTON_NAME = "CommandButton1" '<<< COMMANDBUTTON NAME If StrComp(Sh.Name, SHEET_WITH_CHANGE_CELL, vbTextCompare) = 0 Then If Target.Address = CHANGE_CELL_ADDRESS Then Me.Worksheets(SHEET_WITH_BUTTON).OLEObjects(BUTTON _NAME). _ Object.Caption = Target.Text End If End If End Sub I must be still missing something because nothing happens when I change cell value on sheet1!B1 Thanking you in advance for your support. Regards Tanya "Chip Pearson" wrote: Tanya, Paste the following code in the ThisWorkbook module. Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Const SHEET_WITH_BUTTON = "Sheet1" '<<< CHANGE. Const SHEET_WITH_CHANGE_CELL = "Sheet2" '<<< CHANGE Const CHANGE_CELL_ADDRESS = "$A$1" '<<< CHANGE Const BUTTON_NAME = "Button1" '<<< CHANGE If StrComp(Sh.Name, SHEET_WITH_CHANGE_CELL, vbTextCompare) = 0 Then If Target.Address = CHANGE_CELL_ADDRESS Then Me.Worksheets(SHEET_WITH_BUTTON).OLEObjects(BUTTON _NAME). _ Object.Caption = Target.Text End If End If End Sub Change the value of SHEET_WITH_BUTTON to the name of the worksheet on which the command button resides. Change the value of SHEET_WITH_CHANGE_CELL to the name of the worksheet containing the cell whose text you want to assign to the button. Change CHANGE_CELL_ADDRESS to the address of the cell that triggers the change of the button's caption. The '$' characters are required as shown. Change BUTTON_NAME to the name of the button whose text is to change. -- Cordially, Chip Pearson Microsoft MVP - Excel, 10 Years Pearson Software Consulting www.cpearson.com (email on the web site) "Tanya" wrote in message ... Hi Is it possible to have the description/label of a command button updated by the title in a sheet cell? cheers Tanya |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I apologize for the silence, I was away for a couple days...
I tried Chip's suggestion, and it worked the first time. I think I see why it didn't work for you, though. In an earlier post, you showed the button's Click code, where you call out the sheet by its tab name "1": 'Show Task Weights Sheets("1").Visible = True Sheets("1").Select whereas in the code you tried, you're calling it: Const SHEET_WITH_CHANGE_CELL = "Sheet1" '<<< NAME OF SHEET WITH CELL Am I correct to assume that the sheet tab of your Sheet1 object is named "1"? If so, then trying to index it by "Sheet1" will raise a "Index out of bounds" error because that tab doesn't exist. I like Chip's suggestion, because it paves the way for a systematic approach to your problem: with a loop construct and a little more code, you can update all the ten class buttons under one procedure call. I'd like to correct myself on my first suggestion, though. I think it's much more robust not to use the address property for ID directly in the way I showed it originally. A better way could be to use the Range property: Private Sub Worksheet_Change(ByVal Target As Range) If Target = Range("B2") Then Sheets("Sheet33").CommandButton1.Caption = Target.Value End If End Sub This eliminates the hassle of "$B$2" vs. "B2" comparison failures. The Target.Address property always returns an absolute address with the $ signs, whereas Range can take a wider variety of inputs. An earlier post appeared to work, however I found that when I edited other cells in the worksheet the commandbutton would be updated, no idea how when I had specified the target cell This doesn't make sense. How do you know that the button's caption got updated with the target cell's text, unless you changed the target cell's contents (which would trigger the caption change as expected) ? Cheers, -Basilisk96 |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Basilisk
"Basilisk96" wrote: Am I correct to assume that the sheet tab of your Sheet1 object is named "1"? YES you are correct <<<<<< If so, then trying to index it by "Sheet1" will raise a "Index out of bounds" error because that tab doesn't exist. I haven't had this error? <<<<<<<< I like Chip's suggestion, because it paves the way for a systematic approach to your problem: with a loop construct and a little more code, you can update all the ten class buttons under one procedure call. Chips code didn't appear to work. I wondered if I had placed the code under the incorrect sheet? I'd like to correct myself on my first suggestion, though. I think it's much more robust not to use the address property for ID directly in the way I showed it originally. A better way could be to use the Range property: Private Sub Worksheet_Change(ByVal Target As Range) If Target = Range("B2") Then Sheets("Sheet33").CommandButton1.Caption = Target.Value End If End Sub I tried this and again not working for me. Note I placed the code against sheet1 labeled '1' <<<<<<<<<< This eliminates the hassle of "$B$2" vs. "B2" comparison failures. The Target.Address property always returns an absolute address with the $ signs, whereas Range can take a wider variety of inputs. An earlier post appeared to work, however I found that when I edited other cells in the worksheet the commandbutton would be updated, no idea how when I had specified the target cell This doesn't make sense. How do you know that the button's caption got updated with the target cell's text, unless you changed the target cell's contents (which would trigger the caption change as expected) ? All I know is that when I worked in the rest of the workbook, i.e. insert column the macro ran and changed the commandbutton1 caption, relevant to the contents in a cell other than B2?? I have no idea why this was happening <<<<<<<<<< Thank you for your time considering this problem! Regards Tanya Cheers, -Basilisk96 |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Tanya,
If so, then trying to index it by "Sheet1" will raise a "Index out of bounds" error because that tab doesn't exist. I haven't had this error? <<<<<<<< Is any error handler active during the call, and is not set to catch the error? Chips code didn't appear to work. I wondered if I had placed the code under the incorrect sheet? I tried it, and it worked the first time. It must reside in the "ThisWorkbook" module, which is typically listed in the project tree under "Miscrosoft Excel Objects" section, after all the worksheets. If using Chip's version, it's best to disable (comment out) the previous code in Sheet "1", and vice versa. Private Sub Worksheet_Change(ByVal Target As Range) If Target = Range("B2") Then Sheets("Sheet33").CommandButton1.Caption = Target.Value End If End Sub I tried this and again not working for me. Note I placed the code against sheet1 labeled '1' <<<<<<<<<< There must be more to this problem than what I perceive so far, because that code works for me. But then again, I am testing it it a fresh workbook without any other code in it. <..snip.. This doesn't make sense. How do you know that the button's caption got updated with the target cell's text, unless you changed the target cell's contents (which would trigger the caption change as expected) ? All I know is that when I worked in the rest of the workbook, i.e. insert column the macro ran and changed the commandbutton1 caption, relevant to the contents in a cell other than B2?? I have no idea why this was happening Did you insert the said column before column B in sheet "1"? The recurring problems are rather strange... BTW, what version of Excel and OS are you running? Cheers, -Basilisk96 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Wanting to Create A Command Button Command | Excel Programming | |||
VB's Command Button vs Form's Command Button | Excel Programming | |||
Naming command buttons on a UserForm | Excel Programming | |||
Command Button vs Form Button | Excel Programming | |||
Command Button vs Form Button | Excel Programming |