gpt4 book ai didi

excel - 如何用vba创建多个图表

转载 作者:行者123 更新时间:2023-12-04 22:18:45 26 4
gpt4 key购买 nike

我需要一些帮助....我在 sheet1 中有以下代码:

Sheets("kips").Select

Dim i As Integer 'rows
Dim j As Integer 'columns

i = Cells(Rows.Count, 1).End(xlUp).Row

For j = 2 To 5
With ActiveSheet.Shapes.AddChart.Chart
.Parent.Name = "Chart_" & (j - 1)
.ChartType = xlColumnClustered
.SeriesCollection.NewSeries

With .SeriesCollection(1)
'.Name = "=" & ActiveSheet.Name & "!" & _
'Cells(1, j).Address
.XValues = "=" & ActiveSheet.Name & "!" & _
Range(Cells(2, 1), Cells(i, 1)).Address
.Values = "=" & ActiveSheet.Name & "!" & _
Range(Cells(2, j), Cells(i, j)).Address

End With

End With
Next j
而且我需要在另一张表中添加新图表,所以我尝试使用相同的代码:
Sheets("sheet2").Select

Dim i As Integer 'rows
Dim j As Integer 'columns

i = Cells(Rows.Count, 1).End(xlUp).Row

For j = 2 To 5
With ActiveSheet.Shapes.AddChart.Chart
.Parent.Name = "Chart_" & (j - 1)
.ChartType = xlColumnClustered
.SeriesCollection.NewSeries

With .SeriesCollection(1)
'.Name = "=" & ActiveSheet.Name & "!" & _
'Cells(1, j).Address
.XValues = "=" & ActiveSheet.Name & "!" & _
Range(Cells(2, 1), Cells(i, 1)).Address
.Values = "=" & ActiveSheet.Name & "!" & _
Range(Cells(2, j), Cells(i, j)).Address

End With

End With
Next j
是同一型号的表,但我需要把它放在另一张表中,这是我的表:
data
我做错了什么?
谢谢

最佳答案

使用工作表时,最好创建工作表变量,将它们分配给您正在使用的工作表,然后使用这些变量而不是通过它们的名称或“选择工作表 >> ActiveSheet”等来引用工作表

Dim i As Long 'use Long
Dim j As Long
Dim wsCht As Worksheet, wsData As Worksheet

Set wsData = ActiveSheet
Set wsCht = ThisWorkbook.Sheets("Sheet2")

i = wsData.Cells(Rows.Count, 1).End(xlUp).Row

For j = 2 To 5
With wsCht.Shapes.AddChart.Chart
.Parent.Name = "Chart_" & (j - 1)
.ChartType = xlColumnClustered
.SeriesCollection.NewSeries

With .SeriesCollection(1)
'.Name = "=" & wsData.Name & "!" & wsdata.Cells(1, j).Address
.XValues = "='" & wsData.Name & "'!" & _
wsData.Range(wsData.Cells(2, 1), wsData.Cells(i, 1)).Address
.Values = "='" & wsData.Name & "'!" & _
wsData.Range(wsData.Cells(2, j), wsData.Cells(i, j)).Address

End With

End With
Next j

关于excel - 如何用vba创建多个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66319569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com