如何在 VBA 循环中创建不同的变量名称

Posted

技术标签:

【中文标题】如何在 VBA 循环中创建不同的变量名称【英文标题】:How to create different variables names while in a loop in VBA 【发布时间】:2021-12-24 08:09:52 【问题描述】:

我希望我的代码更简洁。我正在使用数千个文件,这些文件的顺序始终相同,并且无法更改这些文件中列的顺序。

这是我的代码中混乱的部分:

'...

ActiveSheet.ChartObjects("Graph 1").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$L$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$L$24:$L$4523"
ActiveSheet.ChartObjects("Graph 2").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$K$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$K$24:$K$4523"
ActiveSheet.ChartObjects("Graph 3").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$E$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$E$24:$E$4523"
ActiveSheet.ChartObjects("Graph 4").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$B$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$B$24:$B$4523"
ActiveSheet.ChartObjects("Graph 5").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$U$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$U$24:$U$4523"
ActiveSheet.ChartObjects("Graph 6").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$AB$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$AB$24:$AB$4523"
ActiveSheet.ChartObjects("Graph 7").Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$I$23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'!$I$24:$I$4523"
'...

我不能使用 for 循环,因为与图关联的值不按顺序排列(即图 1 的值不在 A 中,图 2 的值不在 B 中)。我过去使用 Python 并使用过字典,但我不知道如何使用 VBA。

我试过了:

Dim Graph As Variant
Dim StringGraph As String

StringGraph = CStr(Graph)

Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")

dic.Add "1", "L"
dic.Add "2", "K"
dic.Add "3", "E"
dic.Add "4", "B"
dic.Add "5", "U"
dic.Add "6", "AB"
dic.Add "7", "I"

For Each Graph In dic.Keys
    ActiveSheet.ChartObjects("Graph" & StringGraph).Activate
    ActiveChart.FullSeriesCollection(1).Name = "='BL-remove'!$ & dict(Graph) & $23"
    ActiveChart.FullSeriesCollection(1).XValues = "='BL-remove'!$A$24:$A$4523"
    ActiveChart.FullSeriesCollection(1).Values = "='BL-remove'! _
      dict(Graph) & $24: $ dict(Graph)& $4523"
Next Graph

我知道我已经接近答案了,但也许有更好的方法。

【问题讨论】:

【参考方案1】:

试试这样的:

    Dim Graph As Variant
    Dim dic As Object, col As String
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic.Add "1", "L"
    dic.Add "2", "K"
    dic.Add "3", "E"
    dic.Add "4", "B"
    dic.Add "5", "U"
    dic.Add "6", "AB"
    dic.Add "7", "I"
    
    For Each Graph In dic.Keys
        col = dic(Graph)
        With ActiveSheet.ChartObjects("Graph" & Graph).Chart.FullSeriesCollection(1)
            .Name = "='BL-remove'!$" & col & "$23"
            .XValues = "='BL-remove'!$A$24:$A$4523"
            .Values = "='BL-remove'!" & col & "$24:$" & col & "$4523"
        End With
    Next Graph

【讨论】:

你的代码真的很棒!你让我开心,谢谢!

以上是关于如何在 VBA 循环中创建不同的变量名称的主要内容,如果未能解决你的问题,请参考以下文章

创建动态 VBA 变量名

在具有不同变量名称的循环中保存

VBA 使用变量表名设置记录

EXCEL VBA 里面如何动态获取字符串转换为变量名?

如何在Excel VBA中定义一个在窗体控件和模块中均能使用的变量?

vba - 使用具有“动态创建名称”的变量