Switching between workbooks in Excel VBA - vba

For some reason Excel 2016 switches to a different workbook when I run a macro. There is no code that indicates this is happening.
I've tried to switch the focus back using
Appworkbook.Activate
but it only works if I put a breakpoint there. I've tried sleep as well. It works fine on Excel 2010 and the macro does what it is supposed to do.
Has anyone experienced similar?
*****SOLUTION (Not ideal!)*****
I ended up fixing this issue but adding a new button and calling the macro from a button_click method. I have no idea wy that worked and assigning the macro to the button didn't.

I ended up fixing this issue but adding a new button and calling the macro from a button_click method. I have no idea why that worked and assigning the macro to the button didn't.

Related

MS Access: Repair broken button/control/vba

is there some way to repair broken functionality in ms access, like recompiling the thing?
I have a button on a form that is supposed to run vba code when clicked. The button no longer works, after like a couple of months. Except for the button getting focus nothing happens. I have tried the compact and repair option in access with no luck. The code works fine btw. I have made a new random button somewhere else on the form and literally copy pasted the vba code from the on_click event of the broken button to the new one, and on the new button it works perfectly. No problems at all. The old one, however, nothing. No matter what I do. I deleted the vba code in the on_click event, saved, closed and opened access again, then reentered the code. Still nothing. I tried removing certain functionality from the button by removing line by line, trying to see if any one line is the issue. Nothing again, no errors are being displayed, I don't think the button even runs the vba code at all.
In case someone wants to look at the code:
Private Sub Befehl107_Click()
[NavigationSubform].Form![stoffe_abfrage subform].Form.FilterOn = False
[NavigationSubform].Form![stoffe_abfrage subform].Form.Requery
Me.FilterAlleLink = "Alle"
Me.FocusDummy.SetFocus
End Sub
The button basically turns off the filter on a subform, requeries the form, sets the value for a textbox on the local form, and then gives focus to another textfield. Nothing complex really. I have 3 more buttons of the same type on my form that manage different filters for the same subform with almost identical code, they all work fine. Just this one broke.
Again, the code works fine if I make a new button, but on the old one it does not do anything.
Obviously I can just replace the button, but that would be annoying if down the line there is other functionality referencing it etc. So I was wondering if anybody encountered a similar problem and has a solution. Given that I have only worked with access for like 2 months total and already have this issue I hope there is some way to repair this more quickly, since I am now assuming that this will happen more frequently. Not sure though, maybe someone with more experience knows if this is common.
Edit: I just checked if literally copy pasting the broken button itself works and yes. I copy paste the broken button somewhere else, then copy paste the vba code from the broken button to the copy, and the copy now works...

Access Form suddenly stopped executing VBA code (solution) (Access365, Windows 10)

This is not a question, actually it is how I fixed it.
I have an Access Form that is my start up and, of course, opens automatically. I have 1 button that runs VBA code, and suddenly, it stopped working, as well as the VBA code in OnFormLoad event. The buttons with MACRO code, though, worked fine.
Solution: I went to Design View, added a new button with the VBA code in OnClick event: msgbox "Works" (can be anything). Once clicked on the button, the code executed normally and, "magically", button and OnLoad script started working again. I have no reasonable explanation for that...

Is there a way to put a breakpoint when my active window changes?

Is there a way to put a breakpoint on ActiveWindow.change ? I have a macro that I run in a new, unsaved Excel file, that is supposed to open CSV file, save it as, process it, and keep it in focus. Instead of that, when the macro is done it selects the empty new file that was unsaved, and puts the CSV in the background. This happens even though I use the code below, just before End Sub:
WKB1.Activate
WKB1.Sheets(1).Activate
WKB1.Sheets(1).Range("A1").Select
The thing is the three rows of code above do set the focus correctly, but at end sub it switches back to the unsaved file.
I was thinking that if I can set a breakpoint whenever the active window name changes, I can catch when that happens, because the macro I'm working with is huge, and I can't find the bug manually.
Kind regards,
Daniel
The fix was quite stupid, but I fixed the issue by creating a new ribbon tab and run the macro from the ribbon instead of running it from VBA. Also i removed and Activate o Select commands. The macro was behaving the same, even after i removed the Activate and Select commands, the thing that fixed it was running it from a new menu.

Excel VBA macro runs from the VBA editor but not from the Macro window

I have a macro written in excel that has now stopped being able to run from the Macro window in the main excel window. It runs fine when I start it from the editor itself and I have another macro in the same file that also runs fine, and displays properly in the macro window. Please see picture.
If anyone has any idea why this is happing I'd be grateful, its a pain to run at the minute...
Running excel 2013 if that matters.
Thanks!
EDIT
Both subs are stored in the same file, the one highlighted in the picture above
Edit 2
The issue has weirdly resolved itself now, I did absolutely nothing and it fixed itself. would still like to know the cause
One is in a module and the other is in "ThisWorkbook".
You need to reference it from ThisWorkbook.
Problem has magically resolved itself without me doing anything, very weird. If anyone has an idea I'd still be interested to know why this happened

Excel VBA add right-click button to addin

I am trying to make an Addin with Pivot right-click button.
All the macros are working in the addin perfectly on every workbook that I open.
The problem is that the right-click buttons don't appear in other workbooks - only in the one with the addin.
Screenshot
As you can see I have written the code in ThisWorkbook module, and the Subs are Private. - I guess that somewhere here is the problem.
Would be very grateful if somebody can help.
Ah, I found the problem.
Obviously, the first part of the code was preventing the buttons to appear in other workbooks.
Other than that, Temporary:=True. it should be False.