Design Comments or Macro to Write Macro Needed

Eldrod

Board Regular
Joined
Mar 11, 2010
Messages
76
I just finished a macro to look through a named range on a worksheet and add or remove a cell pattern based on a "hide" or "show" code in the first column. This macro is called by the Workbook_SheetActivate and Workbook_SheetChange event handlers coded in the macro's ThisWorkbook sheet. So when ever a cell is changed, the macro is called and resets the pattern based on any changes to the hide or show codes. This seems to work well when I place test code in the macro's (.xlsm) file. However, once I save this as an .xlam file and install it as an addin, it does not function when a new spreadsheet is loaded.

If I manually add the Workbook_SheetActivate and Workbook_SheetChange events to this new spreadsheet's Thisworkbook sheet, then it does apprear to try to call the macro.

So the questions I have are:
1. Is there some reason why the calls in the macro's ThisWorkbook sheet are not functioning (or is that by design) when running as an Addin macro? (I have other code for Workbook_Open in that macro that works, but it only gets called on open, not every change...)

2. Can I progammatically open the new spreadsheet's ThisWorkbook sheet and write the Sub Workbook_SheetChange macro (it's only about 3 lines)? It's OK if that programmatically built code is lost when the file is closed -- in fact, I would like to remove it on close if it doesn't.

Does anyone know how to do that or have a better idea for what I'm trying to accomplish?

Thanks!
 
Thanks for the help! I did find that link a little while after my posting and it seems to be just what I needed. Being able to write macros that can construct code that must be located in the spreadsheet's ThisWorkbook sheet opens up a lot of new possibilities.
 
Upvote 0
Andrew,
I was able to get my macro to write the Worksheet functions to the calling spreadsheet's ThisWorkbook but after it does it, the VBA editor left open on the ThisWorkbook tab where the new code was written. In reviewing that document, I didn't see how to close the VBE or prevent it from opening in the first place. Do you or anyone else have any idea on how to prevent that from opening? I don't want my easily frightened user to have to manually close the VBE when this runs.
Thanks again!
 
Upvote 0
Andrew, I followed the examples on the link you provided and I was successful in getting the code in my macro to write the other code into its ThisWorkbook file. The problem I have is that when I run my macro, even with the VBE closed, it opens the VBE to write the code to ThisWorkbook, but then exits the macro leaving the VBE open and with the focus. So when I activate the macro from the spreadsheet, I end up looking at the VBE withe the code that was just constructed in the Thisworkbook file.
I was asking is anyone who has done this knows how to get the VBE to close when the macro ends. I'm looking for a seamless macro run that doesn't leave the user sitting in the VBE instead of the original spreadsheet. Make sense?
 
Upvote 0
Here's the code from my macro that writes the code to ThisWorkbook:
Code:
Option Explicit
Public SIM_Version_Number As String


Sub SimulatorLoad()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long
    Dim StartLine As Long
    Dim NumLines As Long
    Dim ProcName As String
    Dim STModel_Name As String
    Const DQUOTE = """" ' one " character
    Const vbext_wt_MainWindow As Long = 0&

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents("ThisWorkbook")
    Set CodeMod = VBComp.CodeModule

    SIM_Version_Number = "Ver 1.0.0"
    STModel_Name = "Stanley Tools v1.0.9.xlam"
       
    ' Delete the SheetActivate code in ThisWorkbook to ensure we have a clean install
        ProcName = "Workbook_SheetActivate"
        On Error Resume Next
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, Count:=NumLines
        End With

    ' Delete the SheetActivate code in ThisWorkbook to ensure we have a clean install
        ProcName = "Workbook_SheetChange"
            With CodeMod
                StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
                NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
                .DeleteLines StartLine:=StartLine, Count:=NumLines
            End With
        On Error GoTo 0
        
    ' Call the Stanley Tools Settings module to see of these events should be loaded to ThisWorkbook
    Call LoadSettings
    
    SimulatorControl.Show
    
    Unload SimulatorControl
    
    ' Write all the values back to the file
    Open STSettingsFile For Output As #1
        Print #1, "STSetting_VFB_Mode=" & STSetting_VFB_Mode_Value
        Print #1, "STSetting_SIM_Mode=" & STSetting_SIM_Mode_Value
    Close #1
    
    If STSetting_SIM_Mode_Value = "False" Then GoTo TerminateMain:
        
    ' Code to write the SheetActivate event to ThisWorkbook
    With CodeMod
        LineNum = .CreateEventProc("SheetActivate", "Workbook")
        LineNum = LineNum + 1
        .InsertLines LineNum, "    ' This code written by the SimulatorLoad macro in Stanley Tools"
        LineNum = LineNum + 1
        .InsertLines LineNum, "    Run (" & DQUOTE & "'" & STModel_Name & "'!Simulator" & DQUOTE & ")"
         'Run ("'Stanley Tools v1.0.9.xlam'!Simulator")
    End With
    
    ' Code to write the SheetChange event to ThisWorkbook
    With CodeMod
        LineNum = .CreateEventProc("SheetChange", "Workbook")
        LineNum = LineNum + 1
        .InsertLines LineNum, "    ' This code written by the SimulatorLoad macro in Stanley Tools"
        LineNum = LineNum + 1
        .InsertLines LineNum, "    Run (" & DQUOTE & "'" & STModel_Name & "'!Simulator" & DQUOTE & ")"
    End With


TerminateMain:
End Sub
and here is the code that shows up in the calling spreadsheet's ThisWorkbook:
Code:
Option Explicit

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    ' This code written by the SimulatorLoad macro in Stanley Tools
    Run ("'Stanley Tools v1.0.9.xlam'!Simulator")

End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    ' This code written by the SimulatorLoad macro in Stanley Tools
    Run ("'Stanley Tools v1.0.9.xlam'!Simulator")

End Sub
This code for the ThisWorkbook sheet is correctly produced, but the VBE is left open on this code in the VBE instead of building this code in background and retaining focus on the calling spreadsheet. It's kinda frustrating that I'm this close to getting this to work like I need, but can't get the VBE to stay invisible in the process. Thanks for any guidance you can provide.
 
Upvote 0
OK, I just came across this line placed at the end of the building macro that will close the VBE. I still get some screen flashing as the windows are opened and created that I'd prefer to not have, but this is at least closer to what I needed.
Code:
Application.VBE.MainWindow.Visible = False
Here is the module in it's entirety:
Code:
Option Explicit
Public SIM_Version_Number As String


Sub SimulatorLoad()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long
    Dim StartLine As Long
    Dim NumLines As Long
    Dim ProcName As String
    Dim STModel_Name As String
    Const DQUOTE = """" ' one " character
    Const vbext_wt_MainWindow As Long = 0&

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents("ThisWorkbook")
    Set CodeMod = VBComp.CodeModule

    SIM_Version_Number = "Ver 1.0.0"
    STModel_Name = "Stanley Tools v1.0.9.xlam"
       
    ' Delete the SheetActivate code in ThisWorkbook to ensure we have a clean install
        ProcName = "Workbook_SheetActivate"
        On Error Resume Next
        With CodeMod
            StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
            NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
            .DeleteLines StartLine:=StartLine, Count:=NumLines
        End With

    ' Delete the SheetActivate code in ThisWorkbook to ensure we have a clean install
        ProcName = "Workbook_SheetChange"
            With CodeMod
                StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
                NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
                .DeleteLines StartLine:=StartLine, Count:=NumLines
            End With
        On Error GoTo 0
        
    ' Call the Stanley Tools Settings module to see of these events should be loaded to ThisWorkbook
    Call LoadSettings
    
    SimulatorControl.Show
    
    Unload SimulatorControl
    
    ' Write all the values back to the file
    Open STSettingsFile For Output As #1
        Print #1, "STSetting_VFB_Mode=" & STSetting_VFB_Mode_Value
        Print #1, "STSetting_SIM_Mode=" & STSetting_SIM_Mode_Value
    Close #1

    Application.ScreenUpdating = False
    
    If STSetting_SIM_Mode_Value = "False" Then GoTo TerminateMain:
      
    ' Code to write the SheetActivate event to ThisWorkbook
    With CodeMod
        LineNum = .CreateEventProc("SheetActivate", "Workbook")
        LineNum = LineNum + 1
        .InsertLines LineNum, "    ' This code written by the SimulatorLoad macro in Stanley Tools"
        LineNum = LineNum + 1
        .InsertLines LineNum, "    Run (" & DQUOTE & "'" & STModel_Name & "'!Simulator" & DQUOTE & ")"
         'Run ("'Stanley Tools v1.0.9.xlam'!Simulator")
    End With
    
    ' Code to write the SheetChange event to ThisWorkbook
    With CodeMod
        LineNum = .CreateEventProc("SheetChange", "Workbook")
        LineNum = LineNum + 1
        .InsertLines LineNum, "    ' This code written by the SimulatorLoad macro in Stanley Tools"
        LineNum = LineNum + 1
        .InsertLines LineNum, "    Run (" & DQUOTE & "'" & STModel_Name & "'!Simulator" & DQUOTE & ")"
    End With
    
    Application.VBE.MainWindow.Visible = False

TerminateMain:
Application.ScreenUpdating = True
End Sub
If anyone can figure out how to make it not show the flashing VBE sections as it builds, please let me know. I tried adding the ScreenUpdating = False, but that had no effect.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,226,908
Messages
6,193,608
Members
453,810
Latest member
Gks77117

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