VBA Excel macro stopped seeing textbox - vba

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...

Related

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

Run-Time error '32809': Application-defined or object-defined error

All,
I just upgraded to Excel 2013 and am running into a strange issue. I have a macro-enabled workbook that has worked successfully for quite some time now. I was updating some of the code and came across this error (32809) when trying to write to a specific sheet. In troubleshooting, I tried this.
Sheets("Summary").Range("G8").Value = "Test"
This resulted in the same error. Then I tried this.
Debug.Print ActiveSheet.Name
Same error. So I selected another sheet on the workbook and debug.print on the name. Worked fine.
Then I tried this.
Debug.Print Sheets(2).Name (This is the sheet number of the problemic sheet)
Same error. It seems to me that there is some form of corruption with this sheet but I am reticent about deleting and recreating. Any suggestions?
Thanks!
While it wasn't a perfect solution, here is how I finally resolved it. I had a user that was still on Excel 2010 make a copy the corrupt sheet within the same workbook, delete the corrupt sheet, then rename the new sheet to the original name. I was then able to use the workbook in Excel 2013 without issue.
I have a Workbook Open Macro which counts the number of rows in a table on opening so that I can summarize how many records were added or deleted during the data entry session:
Private Sub Workbook_Open()
Dim TotalRows As Integer
TotalRows = Worksheets("Data").ListObjects(1).ListRows.Count
If Range("LastRecordOnStart") <> TotalRows Then Range("LastRecordOnStart") = TotalRows
' Stop
End Sub
I started getting the run-time error '32809' with the following line in the code above highlighted in the debugger every time I opened the workbook...
TotalRows = Worksheets("Data").ListObjects(1).ListRows.Count
Rebooting or opening the workbook on another PC still would not fix it. Even opening a previously saved working copy would give the same error - strange. Note:
While coding, I will periodically save the workbook and use windows explorer to copy a snapshot of the workbook where it creates myworkbook copy(1).xlsm, copy(2).xlsm, etc and if I get a corrupted workbook, I rename it myworkbook.bad.xlsm and rename the latest saved working version (e.g myworkbook copy(6).xlsm to MyWorkbook.xlsm with minimal loss.
When this run-time error occurs though, I've had to rebuild the book several times. It only appears to happen when the workbook crashes after a long session of debugging new code with the Userform open and I have to close excel from the Task Manager.
I read the posts here today and was convinced that it was page related when I could list the listrows count on another sheet and if I converted the Listobject on the "Data" worksheet back to a range and recreated the table, I'd still get the run-time error. To cut a long story short, I tried the following:
Open the workbook
When the error popped up, clicked debug
Right clicked on the "End Sub" line of the WorkBook_Open sub and clicked "Set Next statement"
Pressed F5 to allow the code to finish executing
Saved the workbook and it all works now!
I had another crash with the subsequent run-time error and repeated the steps above and it worked a charm again.

ActiveSheet object not recognized on some computers

I have an Excel 2007 VBA project which works fine on my computer and some others, but on some it hits error 32809 on a line that references the ActiveSheet object. I found that on the affected computers, the ActiveSheet object is not recognized. The ActiveWorkbook object works fine.
On the affected computers, if I create a new workbook, reference ActiveSheet, and activate the affected workbook, the ActiveSheet object works normally; I can get the Name property of ActiveSheet.
On my computer the VBA compiles fine. But on the affected computers, Compile stops on an "Option Explicit" line and returns the error:
"Contains references to object definitions that could not be found." But the selected entries in the References dialog look normal.
On the affected computers we've checked for updates but they were up to date; I assume IT handles updates remotely.
My thoughts are that:
The strange behavior suggests workbook corruption, but the fact that it works fine on some computers suggests not. All the computers are on Excel 2007 SP3.
On the other hand, the fact that it works fine on some computers but not on others, suggests a problem with Excel on the affected computers.
Any ideas?
UPDATE:
Sample code, as requested:
Sub main()
Debug.Print ActiveSheet.Name
End Sub
UPDATE #2:
Thinking it might be workbook corruption, I rebuilt the workbook from scratch. But the new workbook had the same problem.
On a computer where the error was happening, I saved a copy of the file as xlsx, no macros. Then I closed and opened that file to ensure that the macros were gone. Then I copied back all VBA from the original file. Then I resaved as xlsm, with macros.
That file worked fine on the affected computer, and also worked fine on the original computer.
However, on the original computer, if I saved the file, then copied that to the affected computer, it was back to the original problem.
So that gives me a clumsy workaround anyway. Next I'll try KB article 3025036, "Cannot insert object" error after installing MS14-082 security update. Not the same error message but worth a try.
could you post the code please?
Also one thing to check for is that you are not trying to put the Excel.ActiveSheet Object into a Worksheet Object without verifying it's Type. The Activesheet can also be a Chart Object (i.e. a Chart Sheet).
Sub Example()
Dim ws As Excel.Worksheet
'Risks Error:
Set ws = Excel.ActiveSheet
'Safer:
If TypeOf Excel.ActiveSheet Is Excel.Worksheet Then
Set ws = Excel.ActiveSheet
Else
'Do something else:)
End If
End Sub
To check against corruption, copy/paste everything into a new workbook, export your code module and re-import them into the new workbook, then compile. If that fixes it, then it likely was corruption.
This problem went away only after removing all listbox controls. The listbox controls had been there a long time and had been working fine on the affected machines; I'm not sure what suddenly changed to make them not work on some computers.
As a workaround, I'm going to try deleting/creating the listboxes in runtime, instead of using the same listbox control. That way it's always a new listbox created on that computer.
UPDATE:
It turned out that the problem was a bad Office update -- update KB2596927 from the MS14-082 security update. We hadn't had a problem back in November when that update came out, but recently our corporate IT had tried removing KB2596927 from all computers. However, the removal was not successful on some. KB2596927 was still there on mine and some others, but gone from some.
I found that if, on a computer where the problem occurred, I removed the VBA and added it back, it worked on that comptuter and all our computers. But as soon as I modified the VBA on my computer and saved it, and distributed the workbook, the problem was back.
So I removed the KB2596927 Office update, and that fixed the problem. Now I can modify the VBA in the workbook on my computer, and distribute the workbook, and it works normally for everyone.
I had same issue. The VBA compiler shows a problem in line:
ActiveSheet.Shapes("Key_1").Visible = False
and it didn't reorganized the function (and the shape also).
I solved this issue very simple!
I made a copy from same sheet (price list_2018) to "price list_2018 (2)
then I deleted the sheet "price list_2018" and then
I renamed "price list_2018 (2)" to "price list_2018" ... very easy!
The issue has been gone!
Thank You ...

How do I delete VBA code from a sheet using VBA?

I want to delete the VBA code contained within a sheet through VBA. Currently, I have a code that copies a sheet across to a new workbook and deletes all images from it. However, these images are set to do things on Worksheet_Activate in the code, which then causes an error whenever I flick to that sheet with no images there.
I know I can get rid of modules etc using something along the lines of:
With ActiveWorkbook.VBProject
For x = .VBComponents.Count To 1 Step -1
.VBComponents.Remove .VBComponents(x)
Next x
For x = .VBComponents.Count To 1 Step -1
.VBComponents(x).CodeModule.DeleteLines _
1, .VBComponents(x).CodeModule.CountOfLines
Next x
End With
but that does not delete from the sheet (or the workbook for that matter. Would be interesting to know if that was possible too).
The code itself will need to be valid for Excel versions 2003 through to 2013 (so cannot use the save as xlsx workaround).
Found out the issue. My code works fine, just the computer I was testing it on did not allow access to the VBA project object model (and was running it with an On Error Resume Next earlier on in the code)
Will have to write an exception in to sort that out in such cases.
Thanks to #mehow #nixda #SiddharthRout and #DaveU for the help

Run-time error '-2147188160(80048240) DataType:=ppPasteEnhancedMetafile Error

Seems there is some bug. Can't resolve this problem, all code is running fine and I am able to see the AutoShape is getting copied from Excel file but it is not adding it to PowerPoint. Popping up an error Run-time error '-2147188160(80048240) View.Pastespecial : Invalid Request. The specified data type is unavailable
If Range("H" & i).Value = 1 And Range("B" & i).Value = "FRONT" Then
objPPT.Presentations(1).Slides(9).Select
objPPT.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
Your code will be faster and possibly more reliable if you don't rely on selecting anything:
With objPPT.Slides(9).Shapes
Set objShape = .PasteSpecial(ppPasteEnhancedMetafile)(1)
With objShape
' set coordinates and such here
End With
End With
As to why you're getting the error message, try stopping the code after you've put something on the clipbard. Then switch to PowerPoint, use Paste Special to see what paste options are available. If EMF isn't one of them, that's your problem ... you're not putting anything in EMF format on the clipboard.
I had a similar issue, but I found a different solution; it may be specific to what I was doing though.
I setup a program where I would:
(manual) Copy an entire webpage that was a report on several performance metrics
(manual) Pasted it in to excel
Run the program to extract the values I want and then clear contents of the sheet I pasted them on.
Eventually after many tests, it would fail with this same automation error when I tried to access the sheet:
Sheets("PDX Paste").Activate
I was able to activate every other sheet except that particular one, even using the index value instead of the direct name reference. After googling to no success I found out that the copy and paste from the website was also pasting invisible controls. When I found this out I had 1,300+ shapes when I only expected 1 (the button I use to trigger the program). It was actually only apparent when a glitch - presumably due to so much memory being used to store these controls - displayed for a few seconds.
I ran the following code independently and then appended it to the end of my program when I do the cleanup of the data. The code goes through the sheet and deletes any shape that isn't the same type as my button. It would have to be adapted if the shapes you want to delete are the same type as the shapes you want to keep. It also becomes simpler if you don't have any shapes to keep.
Dim wsh As Worksheet
Set wsh = ActiveSheet
Dim i As Integer
For i = wsh.Shapes.Count To 1 Step -1
If wsh.Shapes(i).Type <> wsh.Shapes("UpdateDataButton").Type Then
wsh.Shapes(i).Delete
End If
Next i
I'm not sure this would solve this problem, but hopefully this can help others and prevent loss of time figuring out what may be causing this relatively vague error message.