excel - VBA Excel 中的弹出图表

标签 excel vba

我想知道是否有一种方法可以根据特定工作表中找到的值,通过按下按钮在 Excel 中创建弹出图表?
最好的方法是能够在 VBA 中完成此操作。

我一直在研究但找不到任何真正的解决方案。

有什么建议吗?

最佳答案

你!你这个幸运儿! :p

由于我有空,我为您创建了一个基本版本。 :)

要求:在用户表单中显示图表

逻辑:

  1. 创建一个用户窗体并在其中放置一个图像控件和一个命令按钮。
  2. 确定图表的数据范围
  3. 添加临时表
  4. 在临时表中创建图表
  5. 将图表以 bmp 格式导出到用户的临时目录
  6. 使用该图像加载图像控件

假设:

我假设您的图表的数据范围位于 [Sheet1] 中,如下所示。请相应修改代码。

enter image description here

准备您的用户表单

enter image description here

代码

此代码位于用户窗体代码区域。我已经对代码进行了注释,以便您理解它时不会有任何问题。不过,如果您是这样,请回帖。

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim wsTemp As Worksheet
    Dim rng As Range
    Dim oChrt As ChartObject

    '~~> Set the sheet where you have the charts data
    Set ws = [Sheet1]

    '~~> This is your charts range
    Set rng = ws.Range("A1:B3")

    '~~> Delete the temp sheeet if it is there
    Application.DisplayAlerts = False
    On Error Resume Next
    ThisWorkbook.Sheets("TempOutput").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    '~~> Add a new temp sheet
    Set wsTemp = ThisWorkbook.Sheets.Add

    With wsTemp
        '~~> Give it a name so that we can delete it as shown above
        '~~> This is just a precaution in case `wsTemp.Delete` fails below
        .Name = "TempOutput"

        '~~~> Add the chart
        Set oChrt = .ChartObjects.Add _
            (Left:=50, Width:=300, Top:=75, Height:=225)

        '~~> Set the chart's source data and type
        '~~> Change as applicable
        With oChrt.Chart
            .SetSourceData Source:=rng
            .ChartType = xlXYScatterLines
        End With
    End With

    '~~> Export the chart as bmp to the temp drive
    oChrt.Chart.Export Filename:=TempPath & "TempChart.bmp", Filtername:="Bmp"

    '~~> Load the image to the image control
    Me.Image1.Picture = LoadPicture(TempPath & "TempChart.bmp")

    '~~> Delete the temp sheet
    Application.DisplayAlerts = False
    wsTemp.Delete
    Application.DisplayAlerts = True

    '~~> Kill the temp file
    On Error Resume Next
    Kill TempPath & "TempChart.bmp"
    On Error GoTo 0
End Sub

'~~> Function to get the user's temp path
Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function

输出:

当您运行用户窗体并按命令按钮时,您将看到图像填充在用户窗体中。

enter image description here

关于excel - VBA Excel 中的弹出图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18888473/

相关文章:

vba - Excel - VBA : make Match method less restrictive (by finding a word in a sentence)

vba - 合并、取消合并、重新合并宏

vba - 获取 VBA 中所有单元格更改的通知

windows - VBA、FileSystemObject、Windows 排序顺序

vba - 由于(可能)数据验证导致 Excel 崩溃

VBA 代码使 PPT 应用程序崩溃 - 原因不明

java - 如何从 Excel 或 ods 文件中读取分页行?

excel - 如何突出显示 Excel 电子表格中的计算字段?

java - 使用 Java Apache POI 在 Excel 中插入一行

vba - 在任何事件工作表而不是工作表 1 上运行的代码