PC Review


Reply
Thread Tools Rate Thread

Capture button name or row # during click?

 
 
robotman
Guest
Posts: n/a
 
      9th May 2007
I have a worksheet where I have a created a button in column A of
every row.

When the user presses the button for a particular row, I need to know
what row the user pressed the button in so I can take appropriate
action for that row.

Any ideas?!

Thanks.

John

__ WHAT DIDN'T WORK __

I'll include my failed approach but hopefully there's some simple
approach that I'm not thinking of...

When I create the buttons, I change the name to incorporate the row
number, but I can't figure out how to capture the row # based on which
button was pressed. I've tried passing a parameter to the sub with
the ".OnAction" parameter, but that doesn't work.

Example code to create button for 10 rows:

For Loop = 1 To 10
With ActiveSheet.Buttons.Add(Cells(Loop, 1).Left + 3,
Cells(Loop, 1).Top, 30, 10)
.Characters.Text = "Toggle"
.Name = Loop ' Name the button the Loop (i.e. Row #)
.OnAction = "Toggle_Row_Selection " & Loop ' Can't pass
the Row # here
End With
Next AnimalLoop

 
Reply With Quote
 
 
 
 
JW
Guest
Posts: n/a
 
      10th May 2007
On May 9, 5:39 pm, robotman <robot...@gmail.com> wrote:
> I have a worksheet where I have a created a button in column A of
> every row.
>
> When the user presses the button for a particular row, I need to know
> what row the user pressed the button in so I can take appropriate
> action for that row.
>
> Any ideas?!
>
> Thanks.
>
> John
>
> __ WHAT DIDN'T WORK __
>
> I'll include my failed approach but hopefully there's some simple
> approach that I'm not thinking of...
>
> When I create the buttons, I change the name to incorporate the row
> number, but I can't figure out how to capture the row # based on which
> button was pressed. I've tried passing a parameter to the sub with
> the ".OnAction" parameter, but that doesn't work.
>
> Example code to create button for 10 rows:
>
> For Loop = 1 To 10
> With ActiveSheet.Buttons.Add(Cells(Loop, 1).Left + 3,
> Cells(Loop, 1).Top, 30, 10)
> .Characters.Text = "Toggle"
> .Name = Loop ' Name the button the Loop (i.e. Row #)
> .OnAction = "Toggle_Row_Selection " & Loop ' Can't pass
> the Row # here
> End With
> Next AnimalLoop


Never name a variable as one of VBAs reserved words, such as Loop. In
the example below, I renamed Loop to L. Also, in the OnAction
property, you need to assign that to a separate sub routine. You can
capture which button was clicked by using Application.Caller.

Sub foo()
For L = 1 To 10
With ActiveSheet.Buttons.Add(Cells(L, 1).Left + 3, Cells(L,
1).Top, 30, 10)
.Characters.Text = "Toggle"
.Name = L ' Name the button the Loop (i.e. Row #)
.OnAction = "toggler"
End With
Next
End Sub

Sub toggler()
MsgBox Application.Caller
End Sub

HTH
-Jeff-

 
Reply With Quote
 
robotman
Guest
Posts: n/a
 
      10th May 2007
Application.Caller works. I was stumped on this one.

Thank you!

John

P.S. I was just using "Loop" as an example variable, but thanks for
the reminder when posting sample code.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I capture the button's click event of a DataGridView column? maarif@gmail.com Microsoft C# .NET 1 30th Apr 2008 02:58 PM
Need to capture keypress or button click from another app Terry Olsen Microsoft VB .NET 1 29th Dec 2006 03:06 AM
capture right mouse button click event on cell Reinhard Thomann Microsoft Excel Programming 1 20th Jan 2005 12:28 PM
Can i capture print button click event on crystalreportview Joki Microsoft C# .NET 0 29th Nov 2004 11:45 AM
Capture button click in datagrid Lloyd Sheen Microsoft ASP .NET 1 29th Sep 2004 06:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 PM.