Run VBA by clicking on a cell instead of using ActiveX Control (Command Button)

maclachlan19

Board Regular
Joined
Jul 8, 2013
Messages
53
Most of my spreadsheet are shared by a number of people that use different computer setups, different monitor resolutions and thru a Remote desktop connection.
The Command buttons I use to run some VBA often grow and shrink, often covering up the data.
I have searched and this appears to be an Excel issue.

My question is can you run VBA without using a command button. Perhaps a hyperlink in a cell of the spreadsheet.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
for sure. You can try something like the following. As soon as A3 is selected, the script would run:

VBA Code:
Private Sub worksheet_selectionchange(ByVal target As Range)

 If Not Intersect(Range("A3"), target) Is Nothing Then
     'do something
End If

End Sub
 
Upvote 0
Solution
I tried the code above but nothing happened,
I replaced 'do something with the code I wanted to run. Is this right?

Is using a Hyperlink a better option than clicking the cell.

VBA Code:
Private Sub worksheet_selectionchange(ByVal targe As Range)
    If Not Intersect(Range("L5"), Target) Is Nothing Then
      Application.ScreenUpdating = False
        Worksheets("New").Unprotect Password:="xxxxx"
        Sheets("New").Range("B5:B5").Copy
        Sheets("Tim").Range("B1:B1").PasteSpecial Paste:=xlPasteValues
        Sheets("Used").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
        Sheets("Serv").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
        Sheets("Parts").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
        Sheets("Leasing").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
        Sheets("Other").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
        Sheets("Dealer").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
 
 Range("E:CF").EntireColumn.Hidden = False
 Dim c As Range

    For Each c In Range("E3:CF3").Cells
        If c.Value = "0" Then
            c.EntireColumn.Hidden = True
          
        End If
    Next c
Worksheets("New").Protect Password:="xxxxx"
Application.ScreenUpdating = True

End If
    
End Sub
 
Upvote 0
Thanks, I just changed that but no luck on making this work.

The vba code is located in "Sheet1 (New)", is this right?

I've setup a hyperlink in cell L5 using:
Place in this Document
Type the cell reference = L5

Anything else I might be missing.
Thanks
 
Upvote 0
The code should be in whichever sheet L5 (your trigger) cell is located in. You shouldn’t need a hyperlink in this cell.

One other thing to check is that you have application events enabled…and that this is in a macro enabled workbook.
 
Upvote 0
The code is in the correct sheet, just checked.
I have deleted the hyperlink from L5

I'm not sure how to have application events enabled. I added some code, but I'm over my head here.

VBA Code:
Private Sub worksheet_selectionchange(ByVal target As Range)
    If Not Intersect(Range("L5"), target) Is Nothing Then
  Application.EnableEvents = True
  Application.ScreenUpdating = False
Worksheets("New").Unprotect Password:="xxxx"
Sheets("New").Range("B5:B5").Copy
Sheets("Rob").Range("B1:B1").PasteSpecial Paste:=xlPasteValues
Sheets("Used").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
Sheets("Serv").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
Sheets("Parts").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
Sheets("Leasing").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
Sheets("Other").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
Sheets("Dealer").Range("B5:B5").PasteSpecial Paste:=xlPasteValues
 
 Range("E:CF").EntireColumn.Hidden = False
 Dim c As Range

    For Each c In Range("E3:CF3").Cells
        If c.Value = "0" Then
            c.EntireColumn.Hidden = True
          
        End If
    Next c
Worksheets("New").Protect Password:="xxxx"
Application.ScreenUpdating = True
 

End If
    
End Sub
 
Upvote 0
Create a new sub such as.
Private sub test()
Application.EnableEvents = True
End sub

Run that. It will ensure your events are enabled. The events must be enabled for worksheet_selection change to trigger.
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,159
Members
449,295
Latest member
DSBerry

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
Back
Top