VBA Excel组合字符串并转换为图表对象

时间:2018-05-16 14:45:27

标签: vba excel-vba excel

我正在尝试构建一个遍历一行数据的函数,在函数中使用这些变量复制粘贴图形或范围之前,将每个单元格分配给变量。

我遇到的问题是将图表路径构建为字符串变量的组合,我可以从我的wb,sheet和chartName字符串变量创建图表路径作为字符串,但我正在努力然后把它变成图表对象。

使用Dim cht as Chart运行以下代码我收到错误(1)

  

"运行时错误' 91':对象变量或未设置黑色变量"

当我昏暗cht as String时,我收到错误(2)

  

"编译错误:ByRef参数类型不匹配"

这是我的代码 - 我已经评论错误位置

Sub RollingCode()

    Dim height As Double
    Dim weight As Double
    Dim gender As String
    Dim newWeight As Double
    Dim ImageType As String
    Dim wB As String
    Dim sH As String
    Dim NameRange As String
    Dim Sld As Integer
    Dim DataTyp As Integer
    Dim TopPos As Integer
    Dim LeftPos As Integer
    Dim WidthPos As Integer
    Dim HeightPos As Integer
    Dim rng As Range
    Dim cht As String 'WHEN CHART ERROR 1, WHEN STRING ERROR 2

    'Open powerpoint
    'Call Module2.OpenPowerpoint


    Dim lastRow As Integer
    lastRow = ThisWorkbook.Sheets("Graphs").Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To lastRow

        ImageType = Range("A" & i).Value
        wB = Range("B" & i).Value
        sH = Range("C" & i).Value
        NameRange = Range("D" & i).Value
        Sld = Range("E" & i).Value
        DataTyp = Range("F" & i).Value
        TopPos = Range("G" & i).Value
        LeftPos = Range("H" & i).Value
        WidthPos = Range("I" & i).Value
        HeightPos = Range("J" & i).Value

        If wB = "Active" Then
            Select Case ImageType

                   Case "chart", "Chart"

                        cht = "ThisWorkbook.Worksheets(" & sH & ").ChartObjects(" & NameRange & ").Chart" 'ERROR 1 HERE

                        'MsgBox cht
                        CopyPasteChartFull Sld, cht, LeftPos, TopPos, WidthPos, HeightPos 'ERROR 2 HERE

                   Case "Range", "range"
                        MsgBox "Range"
                   Case Else
                        MsgBox "Incorrect"
                   End Select

        Else
            Select Case ImageType

             'Irrelevant code here

            End If

    Next i

End Sub

总结一下,如何将字符串转换为图表对象,其中wb = active,sh =" Forecast"和NameRange =" Chart 1"

由于

0 个答案:

没有答案