用Chat GPT 教寫excel vba 指令,快速製作分析圖表 make Chart quickly
用Chat GPT 教寫excel vba 指令,快速製作分析圖表 make Chart quickly
如果我想將一份問卷( 假設是長的)逐題製作分析圖表,需要不少時間。
今次我用ChatGPT幫忙快速製作分析圖表,嘗試省卻時間。
以下是一份模擬問卷,共有4題。
Sure, I'll provide you with VBA code to create a 3D pie chart that aggregates the answers for different questions. In this example, I'll assume the questions are listed in column A (starting from A2), and the corresponding answers are in columns B to F for each question. The data range for each question will be determined dynamically. Here's the code:
vbSub CreatePieCharts()
Dim ws As Worksheet
Dim chartDataRange As Range
Dim chartDestination As Range
Dim chartTitle As String
Dim lastRow As Long
Dim questionRange As Range
Dim question As Range
' Set the worksheet where the data is located
Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with the actual sheet name
' Find the last row of data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Set the range containing the questions (assumes each question is in a separate row)
Set questionRange = ws.Range("A2:A" & lastRow)
' Loop through each question and create a pie chart
For Each question In questionRange
chartTitle = question.Value
' Determine the data range for the current question
Dim firstDataRow As Long
firstDataRow = question.Row
Dim lastDataRow As Long
lastDataRow = ws.Cells(firstDataRow, ws.Columns.Count).End(xlToLeft).Column
' Set the data range for the pie chart
Set chartDataRange = ws.Range(ws.Cells(firstDataRow, "B"), ws.Cells(firstDataRow, lastDataRow))
' Set the destination range for the chart
Set chartDestination = ws.Cells(firstDataRow, lastDataRow + 2) ' Places the chart two columns to the right of the data
' Create the 3D pie chart
CreatePieChart3D ws, chartDataRange, chartDestination, chartTitle
Next question
End Sub
Sub CreatePieChart3D(ws As Worksheet, dataRange As Range, destination As Range, chartTitle As String)
Dim cht As ChartObject
Dim chartData As Range
' Define the chart data (including the category names and values)
Set chartData = Union(dataRange.Offset(0, -1), dataRange)
' Add a new chart to the worksheet
Set cht = ws.ChartObjects.Add(Left:=destination.Left, Width:=375, Top:=destination.Top, Height:=225)
' Set chart properties
With cht.Chart
.ChartType = xl3DPie
.SetSourceData Source:=chartData
.HasTitle = True
.ChartTitle.Text = chartTitle
End With
End Sub
Please make sure to replace "Sheet1" with the actual name of the sheet containing your data. Also, ensure that the data is arranged as described, with questions in column A and answers in columns B to F for each question. This code will loop through each question, create a 3D pie chart for its corresponding answers, and place the chart two columns to the right of the data for each question.
如要在excel 用VBA巨集功能: 先要在選項-->自訂功能區-->剔選開發人員選項。
留言
張貼留言