gus*_*ago 2 excel vba excel-vba
我想知道是否有办法根据特定工作表中的值,按下按钮在Excel中创建弹出图表?
最好的方法是能够在VBA中完成.
我一直在研究,但找不到任何真正的解决方案.
有什么建议?
您!你这个幸运的人!:p
由于我有空,我为你创建了一个基本版本.:)
要求:在用户表单中显示图表
逻辑:
假设:
我假设您的图表的数据范围在[Sheet1]中,看起来像这样.请相应修改代码.

准备用户表单

码
此代码位于用户表单代码区域中.我已对代码进行了评论,以便您在理解代码时不会遇到任何问题.如果你这样,还是回帖.
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
Run Code Online (Sandbox Code Playgroud)
输出:
运行用户窗体并按下命令按钮时,您将看到用户窗体中填充的图像.

| 归档时间: |
|
| 查看次数: |
16141 次 |
| 最近记录: |