Double Click Command Button to run module

T

Todd

I have code like the following in a module and a Call
command linked to a command button on Sheet1 which is
named Index. Sheet 2 is named Form. When I check the
check box and press the command button only the cell range
defined is highlighted but does not change to yellow.
After going back the the Index sheet and pressing the
command button again will the cells then turn yellow.
What can I add so the cells will follow the complete code
on the first press of the command button. Thanks for the
help!

Public Sub AddingEquipment()
If Worksheets("Index").CheckBox1.Value = False Then Exit
Sub
Sheets("Form").Activate
ActiveSheet.Range("AddingEquipment").Select
If Selection.Interior.Pattern = xlNone Then
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Else
With Selection.Interior
.Pattern = xlNone
End With
End If

End Sub
 
D

Dave Peterson

Your code worked ok for me, but you could get rid of the selections:

Option Explicit
Public Sub AddingEquipment()
If Worksheets("Index").CheckBox1.Value = False Then Exit Sub

With Worksheets("Form").Range("AddingEquipment")
If .Interior.Pattern = xlNone Then
With .Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Else
With .Interior
.Pattern = xlNone
End With
End If
End With

End Sub

And I think I'd rename either the range or the macro--I wouldn't have two things
named AddingEquipment. (although it did work ok as is.)
 
J

JulieD

Hi Todd

it seemed to work fine for me, but here's a variation on your macro that
doesn't "select" anything ... maybe it will work better
Public Sub AddingEquipment()
If Worksheets("Index").CheckBox1.Value = False Then Exit Sub
If Sheets("Form").Range("AddingEquipment").Interior.Pattern = xlNone Then
With Sheets("Form").Range("AddingEquipment").Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Else
With Sheets("Form").Range("AddingEquipment").Interior
.Pattern = xlNone
End With
End If

End Sub
 

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

Top