在用户表单中图形作为图像不更新

时间:2016-01-19 13:47:25

标签: excel-vba graph userform vba excel

我创建了以下宏,它从两个图形中“打印”两个图像并以用户形式输出。您可以在三个下拉栏中首先选择一个类别,然后选择子类别(然后显示这些子类别)。出于某种原因,当我更改类别和子类别时,左图像/图表正在更新。但是,当我更改右图像/图形的子类别时,它没有被更新。理想情况下,我想将两个图像/图形合并到一个图形中以便于比较。我怎样才能做到这一点?

    Option Explicit


Private Sub UpdateGraph(rowHeader$, colHeader$, chartName$)
    If rowHeader = "" Or colHeader = "" Then Exit Sub

    Dim ch As Chart
    Dim oFind As Range
    Dim row%, col%
    Dim imgPath$, img As Image

    START

    With shData
        Set oFind = .Range("C:C").Find(rowHeader, , xlValues, xlWhole)
        If oFind Is Nothing Then
            MsgBox "Header " & rowHeader & " not found in column C", vbCritical
            Exit Sub
        End If
        row = oFind.row

        Set oFind = .Range("2:2").Find(colHeader, , xlValues, xlWhole)
        If oFind Is Nothing Then
            MsgBox "Header " & colHeader & " not found in row 2", vbCritical
            Exit Sub
        End If
        col = oFind.Column
    End With

    imgPath = ThisWorkbook.Path & Application.PathSeparator & "TempChart.gif"

    With shHiddenChart
        .Range("H_ColHeader").Value = colHeader
        .Range("H_" & chartName).Value = rowHeader

        shData.Range(shData.Cells(row, col + 5), shData.Cells(row, col + 20)).Copy
        .ListObjects("tbl" & chartName).DataBodyRange.PasteSpecial xlPasteValues

        Set ch = .ChartObjects(chartName).Chart
        ch.Export imgPath, "GIF"
    End With

    Set img = IIf(chartName = "Chart1", imgGraph1, imgGraph2)
    img.Picture = LoadPicture(imgPath)

    On Error Resume Next
    Kill imgPath
    On Error GoTo 0

    FINISH
End Sub

Private Sub btnClose_Click()
    Unload Me
End Sub

Private Sub dd1_Change()
    Call UpdateGraph(dd2.Value, dd1.Value, "Chart1")
    Call UpdateGraph(dd3.Value, dd1.Value, "Chart2")
End Sub

Private Sub dd2_Change()
    Call UpdateGraph(dd2.Value, dd1.Value, "Chart1")
End Sub

Private Sub dd3_Change()
    Call UpdateGraph(dd3.Value, dd1.Value, "Chart2")
End Sub

Private Sub UserForm_Initialize()
    With shConfig
        Call FillListBox(.ListObjects("tblDD1").DataBodyRange, dd1)
        Call FillListBox(.ListObjects("tblDD2").DataBodyRange, dd2)
        Call FillListBox(.ListObjects("tblDD2").DataBodyRange, dd3)
    End With
End Sub

    Private Sub FillListBox(rng As Range, lBox As ComboBox)
    Dim cell As Range

    lBox.Clear
    For Each cell In rng.Cells
        lBox.AddItem cell.Value
    Next
End Sub

0 个答案:

没有答案