简体   繁体   中英

Trigger a VBA Macro on any opened workbook and sheets with following code

I am using the following code to highlight spelling mistakes in the cell-on-cell change

Sub ColorMispelledCells()
For Each cl In ActiveSheet.UsedRange
 If Not Application.CheckSpelling(Word:=cl.Text) Then _
 cl.Interior.ColorIndex = 15
Next cl
End Sub

Now, the problem is that every time I have to run this code by pressing f5 and also this only works on a particular workbook (Workbook specific).

So, my question is, what is the process to make a universal/global macro that runs on every workbook and every sheet which is opened, and that too runs internally when the cell is changed? It should not open up with the VBA editor window again and again. Along with this is there any way where I can make a enable and disable button highlighted on the excel toolbar for this macro? see image to see my VBA editor project explorer hierarchy

I tried searching on the inte.net and also saw many sample codes but nob body explained to make a global/universal macro, I am unable to figure out how to develop a global/universal macro that runs automatically on cell changes. which is not bounded to any one workbook but works globally on any opened workbook on all sheets.

I'm interested in your case. I myself is not an expert, so just now I search the inte.net and playing around and try to "cheat" by making this kind of code - which I'm not so sure if that meets your requirement.

In PERSONAL.XLSB workbook - ThisWorkbook module:

Private WithEvents app As Application

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
On Error Resume Next
If Wb.Name <> "PERSONAL.XLSB" Then
    Set src = Workbooks("PERSONAL.XLSB").VBProject.VBComponents("Sheet1").CodeModule
    Set trg = Wb.VBProject.VBComponents("ThisWorkbook").CodeModule
    trg.insertlines 1, src.Lines(1, src.countoflines)
End If
End Sub

Private Sub Workbook_Open()
Set app = Application
End Sub

In PERSONAL.XLSB workbook - Sheet1 module:

Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
 If Not Application.CheckSpelling(Word:=Target.Text) Then _
 Target.Interior.ColorIndex = 15 Else Target.Interior.ColorIndex = xlNone
End Sub

Source code from this link and this link

The sub in "ThisWorkbook" module of PERSONAL.XLSB will be triggered when opening any workbook. The sub will copy the macro in "Sheet1" module of PERSONAL.XLSB into "ThisWorkbook" module of the open workbook.

The sub in "Sheet1" module of PERSONAL.XLSB is an event handler to any sheet of the active workbook which will be triggered if there is a change on any cell of any sheet.

I noticed that if I edit the sub in the PERSONAL.XLSB to something else, then I open another workbook, the sub will not run. I need to close the Excel application first, then open it again. If I don't do any sub-editing, opening other workbook will trigger the sub to run.

在此处输入图像描述

The animation above is opening two xlsx workbook which for sure there can't be any code in that workbook. But because at the time it's opened the macro in PERSONAL.xlsb is triggered (copying a sub to "ThisWorkbook" module of that xlsx workbook), so then there is a sub in that xlsx workbook. If I close this xlsx workbook, it will prompt me if I want to save the workbook. If I click yes, it will complain because it can't be saved as xlsx while there is a macro in "ThisWorkbook" module of this workbook.

Still not sure though if this is the kind that you want. Also maybe what you want is something like this: in whatever computer which has Excel app, it can do the same thing without doing anything before hand . So it's not doable, because to any computer which has Excel app, the macro need to be copied to PERSONAL.XLSB on each computer before hand.

Please note, I don't test to open an xlsm workbook which already has Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module. And I think most likely it will throw an error if the opened workbook has already Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM