Search code examples
vbapowerpoint

Shorthand to Create an Array of Integers n1-n2


Consider the following intuitive VBA command, which selects the first 10 slides of a PPT presentation:

ActivePresentation.Slides.Range(Array(1,2,3,4,5,6,7,8,9,10)).Select

I would like to express this in more succinct form. For example

-- psuedo-code
ActivePresentation.Slides.Range(CreateRange(1,10)).Select

How can you do this with the VBA available in Powerponit?


Solution

  • No such build-in function in VBA, but it's quite simple to create your own:

    Function createRange(fromVal As Long, toVal As Long) As Long()
        ReDim a(fromVal To toVal) As Long
        Dim i As Long
        For i = fromVal To toVal: a(i) = i: Next
        createRange = a
    End Function
    

    ... and voilà, your pseudo-code is no longer a pseudo-code