Creating a Table out of .Values stored in an Array

杀马特。学长 韩版系。学妹 提交于 2019-12-11 15:12:44

问题


I'm new to working with 2D arrays, and would like to get better.

Currently everything is going well, my array is populating like I want it to, but I can't figure out how to get it into a table.

Right now I have each cell's value stored in it's corresponding array slot. So Range(B1:B4) is stored in IQRef(1, 2), IQRef(2, 2), IQRef(3, 2), IQRef(4, 2).

For what I'm doing, storing the Range directly is not an option. PowerPoint issues with pasting non-contiguous ranges...

So what I want to be able to do is create a table out of these values that I will be pasting into PowerPoint.

This was my failed attempt at "concatenating" all the individual values stored in the array: Set unionVariable = xlApp.Union(IQRef(1, 2), IQRef(2, 2), IQRef(3, 2), IQRef(4, 2), IQRef(1, 7), IQRef(2, 7), IQRef(3, 7), IQRef(4, 7)) It failed, obviously, because IQRef isn't a Range, and Union only takes ranges.

So my question is, how would I get the values I have in my array into a table?

Public Sub tableArray()

    'Timer start
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    StartTime = Timer

    'Create variables
    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    Dim ShRef As Excel.Worksheet
    Dim pptPres As Object
    Dim colNumb As Long
    Dim rowNumb As Long
    Dim i As Long
    Dim myShape As Object

    Excel.Application.DisplayAlerts = False

    ' Create new excel instance and open relevant workbook
    Set xlApp = New Excel.Application
    'xlApp.Visible = True 'Make Excel visible
    Set xlWB = xlApp.Workbooks.Open("filePath", True, False, , , , True, Notify:=False) 'Open relevant workbook
    If xlWB Is Nothing Then                      ' may not need this if statement. check later.
        MsgBox ("Error retrieving file, Check file path")
        Exit Sub
    End If

    'Find # of iq's in workbook
    Set ShRef = xlWB.Worksheets("Sheet1")
    colNumb = ShRef.Cells(1, ShRef.Columns.Count).End(xlToLeft).Column
    rowNumb = ShRef.Cells(ShRef.Rows.Count, 1).End(xlUp).Row

    Dim IQRef() As String
    Dim iCol As Long
    Dim iRow As Long

    ReDim IQRef(1 To rowNumb, 2 To colNumb)
    ' capture IQ refs locally
    For iRow = 1 To rowNumb
        For iCol = 2 To colNumb
            IQRef(iRow, iCol) = ShRef.Cells(iRow, iCol).Value
        Next iCol
    Next iRow
    'Make pptPres the ppt active
    Set pptPres = PowerPoint.ActivePresentation

    'Create variables for the slide loop
    Dim pptSlide As Slide
    Dim unionVariable

    Set unionVariable = xlApp.Union(IQRef(1, 2), IQRef(2, 2), IQRef(3, 2), IQRef(4, 2), IQRef(1, 7), IQRef(2, 7), IQRef(3, 7), IQRef(4, 7))

    For Each pptSlide In pptPres.Slides

        ' Copy table
        unionVariable.Copy                       ' copy unioneVariable that should be a table

        ActiveWindow.ViewType = ppViewNormal
        ActiveWindow.Panes(2).Activate

        Set myShape = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteHTML, Link:=msoFalse) 'Should paste unionVariable's table

        'Set position:
        myShape.Left = -200
        myShape.Top = 150 + i
        i = i + 150

    Next pptSlide

    xlWB.Close
    xlApp.Quit

    Excel.Application.DisplayAlerts = True

    'End Timer
    SecondsElapsed = Round(Timer - StartTime, 2)
    MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub

来源:https://stackoverflow.com/questions/46851310/creating-a-table-out-of-values-stored-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!