Code that "knows" which button has been pressed?

TheWennerWoman

Board Regular
Joined
Aug 1, 2019
Messages
106
Office Version
  1. 365
Platform
  1. Windows
Hello, hope someone can help.

We have a file and each row has a hyperlink in column F. Each hyperlink points to a different Word document that is relevant to that particular record.

We have had a number of occasions where the spreadsheet has gone rogue and when Excel recovers it all of the hyperlinks then point to some location on the c: drive (Appdata / Local, just a random location).

The workaround, I think, is to add a button to each cell in column F and have it open the file which will be named in column N.

So rather than have a macro for each button, is it possible to have one macro that will open the right file? So row 2 button might call C:\test1.doc whereas row 9 button might call c:\hello.doc.

Essentially, the macro needs to "know" which of the hundreds of buttons has been pressed.

Hope that makes sense, thank you for reading.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,563
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Yes as long as you use Form buttons rather than ActiveX. In that case, Activesheet.Buttons(Application.Caller) would give you a reference to the specific button, and you can assign the same macro to all the buttons.
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,644
Office Version
  1. 365
Platform
  1. Windows
Dont have hundreds of buttons. You could use for example a double click event.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

MsgBox "Row " & Target.Row

End Sub
 

FormR

MrExcel MVP
Joined
Aug 18, 2011
Messages
6,577
Office Version
  1. 365
Platform
  1. Windows
If the spreadsheet has a habit of "going rogue" adding hundreds of buttons is unlikely to help with the stability.

You could also consider using the =HYPERKLINK() function.
 
Solution

Forum statistics

Threads
1,144,630
Messages
5,725,401
Members
422,623
Latest member
Dave52

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top