range Object persistence problem

jwalkerday

New Member
Joined
May 1, 2018
Messages
18
Hello,


The following is attempting to iterate an array (2D String array) and use the values in order to create new powerpoint slides-

Code:
Dim rng as range
        Dim loopI As Integer
        For loopI = 0 To arrayLength - 1 'array starts at 0 so 1


            'Add a slide to the Presentation
              Set ppSlide = ppPresentation.Slides.Add(slideDataArray(loopI, 1), slideDataArray(loopI, 2)) 'slide number and slide setting (blank, title etc)
            
            'Copy Range from Excel
             Set rng = ActiveWorkbook.Sheets("BD Dashboard").range(slideDataArray(loopI, 0)) '0 is excel named range


            'Copy Excel Range
              rng.Copy
              
            'Paste to PowerPoint and position
              ppSlide.Shapes.PasteSpecial DataType:=2  '2 = ppPasteEnhancedMetafile
              Set ppShape = ppSlide.Shapes(ppSlide.Shapes.Count)
              
            'Clear The rng object
              Set rng = Nothing
              
              'scale
              ppShape.Height = slideDataArray(loopI, 3)
              ppShape.Width = slideDataArray(loopI, 4)
            
            'Set position:
            ppShape.Left = slideDataArray(loopI, 5)
            ppShape.Top = slideDataArray(loopI, 6)
         Next



The array is populated correctly and all of the settings work.


The problem is the rng object (range) which uses a named range from the array is supposed to copy the range to the rng object.


It copies the first range in the array but not the subsequent ones in the loop even though the loop is working correctly and it works for all the other settings.


So I end up with multiple copies of the first named range as new slides.


It's like the rng object can only be set once and not get reused. Initially I thought that adding Set rng = Nothing would clear the object reference but that did not resolve the issue.


Is there something extra you need to do in order to reuse a range object in the way I am attempting (in combination with a named range from the spreadsheet)


Thanks,
walker
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You don’t have an on error resume next line do you?

What you’ve written should work, but if there’s an error finding the named range, and you’ve set errors to resume next, you’d get the behaviour you’re experiencing
 
Upvote 0
You don’t have an on error resume next line do you?

Good call- yes I did!

I've removed that line and now I get the following error-

VBA Runtime Error 1004 “Application-defined or Object-defined error”


On this line-
Code:
             Set rng = ActiveWorkbook.Sheets("BD Dashboard").range(slideDataArray(loopI, 0)) '0 is slide named range

I've stepped through the code and the array is supplying the next named range correctly.


Could it be something to do with using the activeWorkbook function?


Has using the paste function into powerpoint released control of the active work object etc?


I tried changing that line to-


Code:
Set rng = Workbooks("MonthlyReport.xlsm").Worksheets("BD Dashboard").range(slideDataArray(loopI, 0)) '0 is slide named range

Still failed with the same error.


So maybe it is something to do with the rng object?


Something I noticed online is the most of the source code defines an rng like this-


Dim rng As Range


But when I type that into VBA it gets auto corrected to-


Dim rng As range


So a lower case r on the variable type.


Could this be related?
 
Upvote 0
Fixed it by using extra name object-

Code:
        Dim namedRange As Name

            Set namedRange = ActiveWorkbook.Names(slideDataArray(loopI, 0))
            Set rng = namedRange.RefersToRange
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top