View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
Ace70 Ace70 is offline
external usenet poster
 
Posts: 12
Default Deactivating a Forms macro button based on a worksheet condition?

Thanks for the suggestions.
I got the first suggestion working as I can easily make the change to
the macro script (I have little paractical VB experience). However, I
am now thinking I need to go one better and get the same thing
happening on a CommandButton from the Controls Toolbox. I searched on
this forum and found some very nice things you can do with the
CommandButtons that you can't do with the the Forms buttons.
eg. You can disable the CommandButton, the text on it can be greyed
out and display a custom message alerting the user to the reason why
the button is disabled. Here is some script that I found that does
this (I don't fully understand it but i can get it to work if i just
copy and paste):

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCellsToCheck As Range
Set myCellsToCheck = Me.Range("a1,b2")

If Intersect(Target, myCellsToCheck) Is Nothing Then
Exit Sub
End If

'Top one is for the Forms toolbar
' bottom one used the control toolbox toolbar
'With Me.Buttons("button 2")
With Me.CommandButton1
Select Case Application.CountA(myCellsToCheck)
Case Is = 0:
.Enabled = True
.Caption = "click me to run macro"
Case Is = 1
.Enabled = False
.Caption = "Clear other cell" & vbLf & "before
running"
Case Is = 2
.Enabled = False
.Caption = "Clear both cells " &
myCellsToCheck.Address(0, 0) _
& vbLf & " before clicking"
End Select
End With

End Sub

Now, I am hoping to maybe combine the cool greying out/custom message
you can get on the button above, with the suggested disable/enable
script based on A1 being TRUE or not suggested below.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1").Value = True Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub

this will disable/enable the button on the sheet.

regards
FSt1


I do need to also learn how to assign a macro to a CommandButton. I
know you are meant to assign the macro to the event on click feature
but I don't know the syntax to do that. I do knwo that a line or two
needs to be added.....

Private Sub CommandButton1_Click()
[inbetween here!!!!!!]
End Sub

....when you double click the CommandButton.

Any further help much appreciated.

Cheers

Ace70