MassSpecGuru
Board Regular
- Joined
- Feb 23, 2010
- Messages
- 55
I'm running XL 2003 and I'm having some issues trying to get XL to automatically change the text in a button based on the user's choice from a ComboBox on an XL worksheet.
I'm trying to get the number ouputted by the ComboBox (i.e., the "cell link") to trigger the Worksheet_Change event but it's not behaving as I expect. (Maybe it's my expectations that are wrong?)
I'm new to VBA and am experimenting with some code I found that seems to work for the demonstrated purpose, but I can't seem to be able to tie this together to trigger my code. In fact, when I do it "backwards" and manually enter a number (1 - 6) in the "cell link" referenced in the ComboBox's format control, cell $A$1, everything changes as it should, just not when I choose any items from the ComboBox.
Below is the code for the Worksheet_Change macro as well as my own code. On their own, each works just fine; however, it just seems like the worksheet does not see the changing of the value in the "cell link" as an event change.
and
Any help, thoughts or ideas would be greatly appreciated.
Thanks!
MSG
I'm trying to get the number ouputted by the ComboBox (i.e., the "cell link") to trigger the Worksheet_Change event but it's not behaving as I expect. (Maybe it's my expectations that are wrong?)
I'm new to VBA and am experimenting with some code I found that seems to work for the demonstrated purpose, but I can't seem to be able to tie this together to trigger my code. In fact, when I do it "backwards" and manually enter a number (1 - 6) in the "cell link" referenced in the ComboBox's format control, cell $A$1, everything changes as it should, just not when I choose any items from the ComboBox.
Below is the code for the Worksheet_Change macro as well as my own code. On their own, each works just fine; however, it just seems like the worksheet does not see the changing of the value in the "cell link" as an event change.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Do nothing if more than one cell is changed or content deleted
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
MsgBox "You just changed " & Target.Address
If Target.Address = "$A$1" Then
'Debug.Print "Target = "; Target.Address
'Ensure target is a number before multiplying by 2
If IsNumeric(Target) Then
'Stop any possible runtime errors and halting code
On Error Resume Next
'Turn off ALL events so the Target * 2 does not put the code into a loop.
Application.EnableEvents = False
Target = Target + 0 '* 2
'Run "RenameButton"
'Turn events back on
Application.EnableEvents = True
Run "RenameButton"
'Allow run time errors again
On Error GoTo 0
End If
End If
End Sub
and
Code:
Sub RenameButton()
Dim MethodName As String
Dim MethodNumber As Integer
MethodNumber = Sheet1.Range("A1").Value
Select Case MethodNumber
Case "1"
MethodName = "Chloride"
Case "2"
MethodName = "Iodide"
Case "3"
MethodName = "Nitrate"
Case "4"
MethodName = "Nitrite"
Case "5"
MethodName = "Phosphate"
Case "6"
MethodName = "Sulfate"
End Select
Debug.Print "Method number ="; MethodNumber
Debug.Print "Method name = "; MethodName
'don't use "vbCrLf"! The "Cr" portion generates a square character.
Sheet1.Shapes("Button 2").TextFrame.Characters.Text = _
"Press button to" & vbLf & "generate " & MethodName & " LRN" & vbLf & "upload file"
End Sub
Any help, thoughts or ideas would be greatly appreciated.
Thanks!
MSG