Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cell and command button 1

Status
Not open for further replies.

Harley78

Electrical
May 8, 2007
77
Was wondering if anyone has ever had the experience when clicking a command button that it will fill in a cell( e.g. A1) with the name of a command button that was pressed (e.g. HC12).


Thanks all
 
Replies continue below

Recommended for you

You are wishing to do this?

One way is to assign a different (but similar) subroutine to each button. When the button is clicked it writes the desired text to the cell A1.

A nicer way is to assign one subroutine to all the applicable command buttons. Name the buttons for easier identification. Example subroutine:

Sub buttonSmart()
ActiveSheet.Cells(1, 1) = Application.Caller
End Sub
 
Ok, let me just understand this. So if I press the command button that has a label HC12 (Which has a macro)

Private Sub HC12_Click()
Range("A11").Select
ActiveCell.FormulaR1C1 = "1"
Range("A43").Select
ActiveCell.FormulaR1C1 = "1"
Range("A53").Select
ActiveCell.FormulaR1C1 = "1"
Range("A67").Select
ActiveCell.FormulaR1C1 = "1"
Range("A123").Select
ActiveCell.FormulaR1C1 = "1"
Range("A166").Select
ActiveCell.FormulaR1C1 = "1"
Range("A168").Select
ActiveCell.FormulaR1C1 = "1"
Range("A184").Select
ActiveCell.FormulaR1C1 = "1"
Range("A194").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select

End Sub

_________________________________________________________

Sub buttonSmart()
ActiveSheet.Cells(1, 1) = Application.Caller
End Sub


So this would automatically look at the command button properties look at the (Name) field and place it in Column 1 Row 1. Am I understanding this correctly?
 
You are better off getting your command button to what you want directly in the command button code. This makes application maintenance staight forward. I've rewriten your sample code with the statement needed to place the button name on your worksheet. HTH
Code:
Private Sub HC12_Click()
  Range("A11,A43,A53,A67,A123,A168,A184,A194").Value = 1
  Range("A1") = HC12.Name
End Sub
 
OK, thanks for really condensing my macro... It works exactly as I wanted it to....

Thanks so much Cummings54
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor