1

I have some buttons that run some code, each time I click the button, the VBE opens and I need it to not. I've tried Application.VBE.MainWindow.Visible = False in several different locations (some not shown here), but it doesn't prevent anything.

Sub Clear_Sales_Button()
'
' Clear_Sales_Button Macro
'
    Application.VBE.MainWindow.Visible = False
    ActiveWorkbook.Sheets("Sales").Activate
    ActiveSheet.Buttons.Add(44.25, 4.5, 144.75, 19.5).Select
    Application.Goto Reference:="Clear_Sales_Button"
    Selection.Characters.Text = "Clear Sales"
    With Selection.Characters(Start:=1, Length:=11).Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = 3
    End With
    Selection.OnAction = "Module1.Readd_Button"
    Range("A1").Select

End Sub
Sub Readd_Button()
'
' Readd_Button Macro
'
' Keyboard Shortcut: Ctrl+w
'
    Application.VBE.MainWindow.Visible = False
    Application.ScreenUpdating = False
'Delete all cells
    ActiveWorkbook.Sheets("Sales").Activate
    ActiveSheet.Cells.Delete
'Add button back to Sales sheet
    ActiveSheet.Buttons.Add(48, 15.75, 96, 29.25).Select
    Application.Goto Reference:="Readd_Button"
    Selection.Characters.Text = "Import Sales"
    With Selection.Characters(Start:=1, Length:=12).Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = 1
    End With
    Selection.OnAction = "Button1_Click"
    Range("A1").Select
    Application.ScreenUpdating = True
End Sub

I simply don't want the VBE to open, but cannot find a way to prevent it

braX
  • 11,506
  • 5
  • 20
  • 33
TKE-439
  • 87
  • 10
  • 1
    VBE doesn't open unless you have Break points on your code, or an error raises and you click on debug. – Damian Nov 11 '19 at 15:59
  • @Damian I have no Breakpoints and there are no errors thrown. I've used "control + shift +F9" to remove all breakpoints (even though none are showing), and VBE still opens. – TKE-439 Nov 11 '19 at 16:04
  • I can't see anything on your code to show the VBE... Thought on aside note, you should try to avoid using the `Application.Goto` and all your `.Activate` or `.Select` on your code. Read [this](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) to find more info. – Damian Nov 11 '19 at 16:07
  • Is the button located on a worksheet? Also remove this line from your code `Application.VBE.MainWindow.Visible = False` – Nick Nov 11 '19 at 16:07
  • @Damian I generally don't use `.Activate` & `.Select` but being new to using buttons I employed them this go-around. @Nick Yes, the button is on a worksheet. I also included that line in my attempts to not show the VBE. – TKE-439 Nov 11 '19 at 16:09
  • If you are using Windows 10 and Excel 2013 there is a known bug that causes the VBE to open up. The resolution for this was to Maximize the VBE Window, unmaximize it, close the VBE Window and Excel and it resolves the issue. – Zack E Nov 11 '19 at 16:12
  • @ZackE Running Windows 10 and Excel 2016. Does this pairing have the same bug? – TKE-439 Nov 11 '19 at 16:13
  • I believe so, but I wasnt able to find anything on Windows Support KB if its specific to only the Windows 10 and Excel 2013 pairing. Another few things to check is to make sure you dont have breakpoints set and make sure "Break on all errors" under options isnt chosen. – Zack E Nov 11 '19 at 16:18
  • @ZackE I went ahead and tried the solution. It worked for the first button press (although it still opens the VBE, but then closes it), but then the issue came back the next time I clicked a button. – TKE-439 Nov 11 '19 at 16:21
  • 1
    What do you want to achieve with the `Application.Goto`-statement? You ask Excel to Goto `Clear_Sales_Button` and that is the name of a `Sub` in your code - so it opens the VBE and jumps to that routine. – FunThomas Nov 11 '19 at 16:23
  • @FunThomas that is how I understood to assign a macro to a button. I believe this is how the recorder did it and I manipulated that code. I'm new to inserting buttons via VBA so if there is a cleaner way to do it, I'm all ears. – TKE-439 Nov 11 '19 at 16:30

3 Answers3

4

The guilty statement in your code is the Application.Goto statement. While it's normal use is to jump to a specific range, it is valid to provide a procedure name, see Application.Goto on Microsoft docu.

In your case, you provide the name of a sub in your code Reference:="Clear_Sales_Button". Although not explicitly stated in the MS page, this will "jump" to the specific routine in the VBE - with other words, show the VBA and scroll to the routine (not execute it).

FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • I think I follow what you're saying. Do you have a suggestion for a remedy? – TKE-439 Nov 11 '19 at 16:36
  • 1
    Just removing the statement should do the trick - assigning a macro is done with `OnAction`. However, you want to check if the button is already there before you add it. And think about if it is not easier to place the button on the sheet and simply show or hide it. – FunThomas Nov 11 '19 at 16:47
2

This won't help you on the matter but will help you understanding how to add buttons without activating sheets or anything:

Option Explicit
Sub Add_Button(MyMacro As String, MyName As String)
'
' Readd_Button Macro
'
' Keyboard Shortcut: Ctrl+w
'
    Application.ScreenUpdating = False
    With ThisWorkbook.Sheets("Sales")
        'Add button back to Sales sheet
        Dim MyButton As Button
        Set MyButton = .Buttons.Add(48, 15.75, 96, 29.25)
    End With

    With MyButton
        .Characters.Text = MyName
        With .Characters(Start:=1, Length:=12).Font
            .Name = "Calibri"
            .FontStyle = "Bold"
            .Size = 12
            .ColorIndex = 1
        End With
        .OnAction = MyMacro
    End With

    Application.ScreenUpdating = True

End Sub
Sub Clear_sales_Button()

    Add_Button "Module1.Readd_Button", "Clear Sales"

End Sub
Sub Readd_Button()

    'Delete all cells
    ThisWorkbook.Sheets("Sales").Cells.Delete
    Add_Button "Button1_Click", "Import Sales"

End Sub

P.S: I have win10 64bits with office 32bits and my VBE doesn't open when running a macro.

Damian
  • 5,152
  • 1
  • 10
  • 21
  • Thank you @Damian ! I copied this into my module and solved the issue. I will study it to better understand the difference between mine and yours. Thanks for the help! – TKE-439 Nov 11 '19 at 16:44
2

Sounds like a ghost breakpoint. Breakpoints are opcodes inserted into the compiled code, to instruct the debugger to, well, break there.

You don't want to "hide the VBE window", that's working on the symptom, not the cause.

Remove the module from your project, and export the code file when prompted. Then re-import the exported code files into the project.

If the code is in a worksheet's code-behind (you can't really export/re-import worksheet modules), move it to a standard module and recompile.

This will essentially "clean up" the garbage leftover compiled p-code, and the code should stop hitting ghost breakpoints that aren't there anymore.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • This happened to me a lot of times, but after the first run all the ghost breakpoints just vanish. – Damian Nov 11 '19 at 16:24
  • Attempted this with both the module and userform that are in the workbook; no success. – TKE-439 Nov 11 '19 at 16:25
  • @TKE-439 unclear if that means you've done it with the module that's breaking execution... – Mathieu Guindon Nov 11 '19 at 16:26
  • @MathieuGuindon I removed the module that is causing the problem and (just for good measure) the userform that is in the workbook that also references the module. – TKE-439 Nov 11 '19 at 16:27