Why is vba (powerpoint) generating an error ......Integer is out of range. 1 is not in the valid range of 1 to 0? - vba

When trying to execute the following:
Dim lCurrentSlide As Long
' Get the SlideID of the slide currently in view
lCurrentSlide = SlideShowWindows(1).View.Slide.SlideNumber
I get this:
Yesterday the code worked fine. Today - not so much.
Microsoft Visual Basic for Applications 7.1.1128
I copied some code from the internet to print one slide.
It worked fine yesterday.
I renamed all but the first slide in the deck.
It hasn't worked since.
The error message reads:
Run-time error '-2147188160 (800248240)':
SlideShowWindows (unknown member) : Integer out of range. 1 is not in the valid range of 1 to 0.

I modified the code:
lCurrentSlide = SlideShowWindows(1).View.Slide.SlideNumber
to read:
On Error Resume Next
Set currentSlide = Application.ActiveWindow.View.Slide
Set currentSlide = ActivePresentation.SlideShowWindow.View.Slide
lCurrentSlide = currentSlide.slideIndex
on Error GoTo 0
And now the procedure executes as expected whether I'm in design mode or slideshow mode.
I studied all the articles posted in the comments as well as some insights from pages found from searches on variations of my original question.
Hopefully soon I will understand more about how to work with slides in vba. I'm pretty good with Excel vba - and I know vba is vba but it's not, really.

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

VBA Error 457 when using Collection to create a unique list

BACKGROUND:
I am trying to identify how many unique time periods I have from a list of dates that have. Elsewhere, I have seen a method which utilizes collections and error trapping (right term? I mean "On Error Resume Next" in any case) to build the collection with unique values. I have even used this structure successfully in other code that I have written, but in my current circumstance, I am getting an "Error 457: This key is already associated with an element of this collection." Thinking I was using the collection incorrectly, I opened up some older code I wrote 6 months ago (on a different computer for a different company) which uses the same structure and was known to WORK. This older code broke on the same identical error, which it previously did not do. Here is the sample of my work-in-progress code:
Dim rng as range
Dim TimePeriod as Collection
Set TimePeriod = New Collection
For Each rng In Range("I2:I6")
On Error Resume Next
TimePeriod.Add rng.Value, CStr(rng.Value) 'This is where the code breaks
On Error GoTo 0
Next rng
QUESTION:
I'm wondering if there is a setting or a reference library that I am somehow missing that is causing both pieces of code to break, or how to determine that, since both codes are functionally identical, and the previously tested satisfactory code breaks like my work-in-progress. I expected the "On Error Resume Next" to force the loop to pass over the error. Any suggestions?
--Update--
Sample data in range("I2:I6") as follows:
1/21/15
1/21/15
1/21/15
1/23/15
1/27/15
Your code works properly on my Excel 2007, although I would rewrite it to enclose the entire loop within the on error resume next for efficiency.
I suspect you are seeing the errors now because of a mis-set macro option error break.
Check Tools/Options that you have not selected to Break on All Errors
Try getting rid of the On Error Goto 0 line. Take a look at this:
Difference between 'on error goto 0' and 'on error goto -1' -- VBA
It comes from Visual Basic 6, but works pretty much the same in VBA, it appears. Should work if you keep the On Error Resume Next line but eliminate the On Error Goto 0 line.

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

Simple VBA selection code error

I have this simple VBA code with which I want to change the background of the selected cells. Somehow the command Selection. that I learned before doesn't work. Could you help me with this code? I know the answer is probably stupid, but I can't seem to figure it out.
Sub set_background_color()
'Add background color to selected cells
Selection.Interior.Color = RGB(255, 0, 0)
End Sub
Thanks
EDIT: Sorry for the vague question, it's my first question on stack overflow so I didn't think of the importance of the type of error. It gives me the error "Compile error: Expected function or variable".
It is attached to a button, but even if I run it as a macro without button it gives me the same error.
EDIT 2: I'm running Excel 2011 on a Mac, until now it never gave me any compatibility issues in VBA. However this does not seem to work.
The problem is not within your code, it is within your cells.
Check the locked status of the cells and the protection status of the worksheet.
If your worksheet is not protected then try this
Sub set_background_color()
Dim r As Range
On Error Resume Next
Set r = Selection
On Error GoTo 0
If Not r Is Nothing Then
r.Interior.ColorIndex = 3
Else
MsgBox "Invalid Selection"
End If
End Sub

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.