Excel VBA Application.Interaction - vba

I have some macro code that runs in my BeforeSave event and before it runs I am using the following line.
Application.Interactive = False
When saving the file normally everything works fine. If I select multiple tabs via ctrl/click and try to save I get an error which reads.
Run-time error '1004':
Method 'Interactive' of object '_Application' failed
It seems to me that the Interactive property is not available to set when you have multiple sheets selected. The reason I want to select several sheets and not the entire document is I am doing a save as and selecting PDF.
Any ideas why this would not work or what could be use in place of Application.Interactive?

Related

VBA Excel macro stopped seeing textbox

I have a VBA macro in excel that used to work fine for a few days but today when I launch it's not working anymore. Funny thing is that no changes were made to the excel file nor the macro.
The macro is attached to a button and gets data from a TextBox named CSVExportRange. When I try to launch it I get an
"Object doesn't support this property or method"
error in the line where I set Range based on value of the textfield:
Dim r As Range
Set r = Range(Application.ActiveSheet.CSVExportRange.Value)
I tried experimenting with it and it turns out it doesn't see the TextBox at all now:
Dim a As Worksheet
Dim k As TextBox
Set a = Application.ActiveSheet
Set k = Application.ActiveSheet.CSVExportRange
In the above case debug breaks at the last line with "Object doesn't support this property or method" error.
I have the TextBox setup properly and didn't change it since last time everything worked fine.
What's wrong?
No idea why it happened but restarting Excel twice helped...
Like, when I closed all Excel files and relaunched it, it was not working still. After doing so once more it started working again.
Note that neither is a remote / network / shared file so it's not a case of someone / something else locking some file...

Why does Excel VBA calling an add-in function crash Excel?

I am running 32-bit Excel 2010 (Windows 10.1) with an add-in loaded (which I developed). I need to call one of the add-in functions (I will call it changeText) from VBA.
I've written the following VBA code behind one of the worksheets:
Sub MakeChange()
Dim result As String
result = Application.Run("changeText", "teststring")
Debug.Print result
Debug.Print "Complete"
End Sub
and assigned this Sub to a button on that worksheet.
If I run this function directly from within the VBA editor it works fine every time and returns the correct result to the Debug window.
If I call the function from within Excel by pressing the button, I get a Type Mismatch error (via a message box) the first time. If I then press the button again, everything hangs and the only way out is to kill the process.
If I move the function to a workbook-level module, more or less the same thing happens - I get a Type Mismatch error on the Application.Run line to which I click 'End', then the same again on the second click but this time the application hangs after pressing 'End'.
The add-in is mature and the function being called works perfectly when called directly from within Excel or otherwise invoked by the add-in. The XLL function being called takes a string and returns a string.
Does anyone have any idea what could be happening here, and in particular why the application is crashing?

Error 91 when using Cells.Find with window hidden

Please read this fully and understand that this program was working fine until I changed the way I was hiding the workbook.
I have a program that worked great while I was using Application.Visible = False and only showing the user form. I came to realize that this would hide all Excel windows and not just the one I was using. This is going to be distributed throughout the department and hiding all Excel windows was unacceptable.
I started using ActiveWindow.Visible = False, but I am now getting Error 91 anytime I search a worksheet for a value (Cells.Find).
Modifying the worksheet is not an option and the value for which I'm searching can move around the sheet depending on what has been added or removed.
Cells.Find worked out great for this reason. I need to either find another way to search the page, or find another way to hide the worksheet. Please help
When the window is not visible, the Cells reference is not qualified to a worksheet object (unless qualified, Cells refers to ActiveSheet.Cells and there is no ActiveSheet), so you can do like:
Sheets("sheetname").Cells.Find ' modifying "sheetname" as needed
This may also fail (with the same error), or it could also yield incorrect results if there are other open workbooks, so it's best to qualify to a workbook fully, e.g.:
Workbooks("workbookname").Sheets("sheetname").Cells.Find(...
It is still a good idea to test the result of Find before performing additional method/property calls against an object which could be Nothing, as per this answer:
Find command giving error: "Run-time Error '91': Object variable or With block variable not set"

Excel Macro opens with run time error 32809

I have an Excel Macro Enabled workbook that I created on Excel 2013. The workbook's macros work well with my computer but does not work on some other computers even if they are using Excel 2013. Works on 7/10 computers I tried both on windows 7 and 8. When I send it to someone's computer that does not work this is what happens:
They open the workbook and once the user clicks "enable content" the workbook errors out "run time error '32809': Application-defined or object-defined error"
debug shows it getting stuck on 2nd line of code below:
private sub workbook open()
Worksheets(1).OLEObjects("ComboBox21").ListFillRange = "impacts"
**Worksheets(2).OLEObjects("ComboBox21").ListFillRange = "yesnoo"** This line errors
if I comment out these two lines, the workbook will open but the combobox21 on worksheet(2) gets renamed to combobox22 and does not work but the first combobox on worksheet(1) loads and functions fine.
I would like to add that if I comment out the lines
Worksheets(1).OLEObjects("ComboBox21").ListFillRange = "impacts"
Worksheets(2).OLEObjects("ComboBox21").ListFillRange = "yesnoo"
I get the error now that "Can't exit design mode because control ' dattimepicker21' cannot be created
I found and fixed the problem, the machines that were not working were missing MSCOMCT2.OCX
http://answers.microsoft.com/en-us/office/forum/office_2013_release-excel/date-picker-dropdown-in-excel-2013/71decdcd-6cc6-4a70-b1ab-20294a2a315a

A private subroutine in VBA is being activated by other workbooks -- Why?

I have a private subroutine in Workbook A that is running any time I open or close and save other unrelated workbooks. I'm trying to understand why that occurs so I can capture all potential errors that may occur.
The subroutine is an ActiveX ComboBox named TabProg that is supposed to run when the value is changed. I have currently added in a check to see if the active sheet trying to run the code is the "Program Loading" sheet to try and divert any potential errors. See snippet below.
Private Sub TabProg_Change()
MsgBox "Whomp!", vbOKOnly + vbExclamation
If ActiveSheet.Name <> "Program Loading" Then 'do nothing
Else
'Run desired actions on "Program Loading" sheet
End If
End Sub
Any known reasons why this is occurring or other ways to catch it would be helpful. Thanks!
Edit 1: I do not see this problem occur when I open other workbooks in new instances of Excel.
Edit 2: I have modified the code to include a message box whenever the code tries to run, now shown above. It is occurring every time I open or close and save any Excel file, including the file itself. The drop down list in the ActiveX ComboBox is a list of names that correspond to 10 sheets within Workbook A. If I delete the sheet that ComboBox is currently set to, the error will disappear. If I change the ComboBox to a different sheet, the error will reappear.
From what you've wrote in your question, I don't think that your problem can be replicated. I think that your Excel file got corrupted. I had an experience like this: had a file for experimenting with macros, one of the macros used Excel Speech object to say "This is a test file" on opening that particular file. The macro was (as all other macros from this file) not part of my PERSONAL workbook, it was assigned to ThisWorkbook in the bespoke file. At some point a funny thing happened: this "This is a test file" private subroutine got activated every time I opened any Excel file. I did not find any solutions, I just deleted the file where the subroutine was stored. This resolved the problem, but I have no explanation for this. I am afraid the same thing may apply to your file, but maybe other folks have a better idea... maybe it's something in the system registry??? I don't know. Can you manually copy elements / code from this file to a newly created file and just delete the original?