I am trying to get some VBA code running when I click a hyperlink in excel. I am using the following test code but it is not working. I have tried placing the code (VBA editor) in the sheet, thisworkbook, and it's own module. Nothing works. What am I doing wrong? Is there some master setting that must be configure to run VBA code? I'm sure it's simple. :)
Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox "It Ran"
End Sub
Related
I want to run the macro 'HighlightMisspelledCells' defined in the module node of Personal.xlsb on cell change of any workbook sheet.
I know that if I insert the following code on the sheet node in VBA Editor then this will call the macro defined in Personal.xlsb, but then for this, I have to insert this code every time for every new sheet, so is there any other way by which I simply add few lines in the PERSONAL.XLSB macro which runs on cell change of any opened sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.Run "Personal.xlsb!HighlightMisspelledCells"
End Sub
Note : There is also another problem (not specific to the question) is, when I save the above code then get an error message box "The Following feature cannot be saved in macro-free workbook" but still code works if I click on 'Yes' to save. But when I opened the same workbook next time then this 'Sheet specific' VBA code disappears.
PERSONAL.XLSB
Sub HighlightMisspelledCells()
Dim cl As Range
For Each cl In ActiveSheet.UsedRange
If Not Application.CheckSpelling(word:=cl.Text) Then
cl.Interior.Color = vbRed
End If
Next cl
End Sub
Note : I am using MS Excel 2016 on Windows PC
I have already tried searching on the internet but have not been able to find a solution to my specific problem.
I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub
I'm using the following simple code to run a macro "mymacro" when clicking/selecting a sheet.
Private Sub Worksheet_Activate()
Call mymacro
End sub
When i execute the macro manually it's working very well but when i click on the sheet it is not.
Basically i'm using the macro to change a chart colors...so when i apply this to a normal sheet where i have a chart as object it's working but when i tried with a sheet where there is only a chart (created by using "Move Chart" on new sheet option) nothing happens
Thank you for the help
The name needs to be Private Sub Chart_Activate() since it is a Chart and not a Worksheet. As VBasic2008 pointed out, the code needs to be in the code module for the Chart. Press Alt+F11 to open the VB Project, CRTL+R to open the Project Explorer. Double click on the Chart, eg.Chart1.
Your code module should look like :
Private Sub Chart_Activate()
Call mymacro
End Sub
Ensure that mymacro is Public or is also in this code module.
I've been having a heck of a time accomplishing what I thought would be an incredibly simple test. All I am trying to achieve is to pop up a MsgBox when a user selects a new cell or changes the contents of a cell.
I've been at this for about 6 hours and so far have zero success! I have identical behavior with Office 2016 (Windows 10) and with Office 2013 (Windows 7).
Here are my method(s):
Create a new macro-enabled workbook.
Record a new macro in the workbook. Stop the recording. Open VBA.
Open the code for "Module 1" and replace the undesired code with the code below. Save the file.
File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> "Trust access to the VBA project object model" is selected. Save the file.
I also have ensured Application.EnableEvents = True
I am expecting to be able to click on various cells, or edit cells, and received a MsgBox whenever the event occurs.
Here is my code:
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
MsgBox "changed!"
End
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox "selected!"
End Sub
Public Sub Just_In_Case()
Application.EnableEvents = True
End Sub
What am I missing? Is there a security setting preventing this action event? I have the same behavior online at work as I do offline at home.
Thank you in advance for your help! :)
PS Here is the screenshot of my VBA environment, if relevant: https://i.stack.imgur.com/yXkMK.png
That Workbook_SheetChange code needs to be in the ThisWorkbook code module, not in a regular module.
EDIT: and the Worksheet_SelectionChange goes in the Worksheet code module
http://www.cpearson.com/excel/events.aspx
For complete reference, the solution is that my code was incorrectly placed in the Module1 coding area. (I had no idea code could go anywhere else!)
Workbook_SheetChange() belongs under ThisWorkbook
Worksheet_SelectionChange() belongs under Sheet1
Like this! Image screenshots:
Sheet1
ThisWorkbook
Thank you so much, Tim, for your help! :D
I'm using Excel 2010.
I added this code to my sheet and it works.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") > 0.5 Then
MsgBox "Discount too high"
End If
End Sub
But I want to create some way where I could create code and share that code across all excel workbooks on our network.
So I made a module and created an add-in and put the code INTO the module of the add-in.
I deleted the code from the sheet since I want the code to be accessible from the module.
I enabled the module.
And the VBA code doesn't work unless I put the code into the sheet. Please advise how I can just have this code in one file and have that code shared across all excel workbooks on our server.
You must put the Worksheet_Change under the Sheetname of the Excel Objects. So if it is Sheet1 that you want this procedure to run on, you will have to put it under Sheet1. If you want it to work on all sheets in the workbook, you will need to put it under ThisWorkbook AND you need to change the procedure name to Workbook_SheetChange