gpt4 book ai didi

arrays - VBA从数组中定义一个范围

转载 作者:行者123 更新时间:2023-12-04 21:36:28 32 4
gpt4 key购买 nike

所以我有一个 1000 行和 10 列的二维数组。我想为 10 列中的每一列创建 10 个范围,然后将它们用作系列来制作图表。我的问题是,如何从数组中定义一个范围,而不使用整个数组,只使用其中的一列?

Sub ChartLord()

Dim rows As Long
Dim columns As Integer
Dim mychart As Chart
Dim data As Range
Dim dataset() As Double

rows = ShData.Cells(ShData.rows.Count, 1).End(xlUp).Row
columns = ShData.Cells(1, ShData.columns.Count).End(xlToLeft).Column

'set array range (includes column titles and xAxix column)
dataset = ShData.Range(ShData.Cells(1, 1), ShData.Cells(rows, columns))



For Z = 0 To 10

Set data = ?
Set mychart = shtCharts.Shapes.AddChart2(200, xlColumnClustered, 50 + 300 * Z, 50, 300, 200, 5).Chart


Next Z



End Sub

最佳答案

以下会将您的范围值收集到 2D 范围中,然后将第二列切成新的 2D 数组。后者只是 1 比 1 的“列”宽。

Dim rws As Long, cols As Long, d As Long
Dim dataset() As Variant, subdataset() As Variant
Dim ShData As Worksheet

Set ShData = Worksheets("Sheet4")

rws = ShData.Cells(ShData.rows.Count, 1).End(xlUp).Row
cols = ShData.Cells(1, ShData.columns.Count).End(xlToLeft).Column

'set array range (includes column titles and xAxix column)
dataset = ShData.Range(ShData.Cells(1, 1), ShData.Cells(rws, cols))

subdataset = Application.Index(dataset, 0, 2) '<~~second column
For d = LBound(subdataset, 1) To UBound(subdataset, 1)
Debug.Print subdataset(d, 1)
Next d

ShData.Cells(1, "Z").Resize(UBound(subdataset, 1), UBound(subdataset, 2)) = subdataset

最后一个操作将剥离列的值放回工作表中,从 Z1 开始。

关于arrays - VBA从数组中定义一个范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36434104/

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