gpt4 book ai didi

VBA确定 "for each loops"中对象的顺序

转载 作者:行者123 更新时间:2023-12-03 00:09:13 25 4
gpt4 key购买 nike

如何控制图表在 foreach 循环中的显示顺序?现在图表是随机选择的;图2、图7、图5等

我应该重命名预定义的图表名称吗?如果是这样,我该怎么做?

    ...
'Loop charts
ThisWorkbook.Activate
Dim objCht As Object
Dim i As Integer
i = 1
For Each objCht In Worksheets(Detailed.Name).ChartObjects

With objCht '.Chart
ThisWorkbook.Activate
.Copy
dataFile.Activate
Select Case i
Case 1
Application.Goto Range("B2")
Case 2
Application.Goto Range("B17")
Case 3
Application.Goto Range("R17")
Case 4
Application.Goto Range("J17")
Case 5
Application.Goto Range("N17")
Case 6
Application.Goto Range("J2")
Case Else
MsgBox "Hmmmm...."
End Select
ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
End With
i = i + 1

Next objCht
...

最佳答案

这是你想要的吗?我假设图表是按顺序命名的。即 1 到任意数字 (Charts.Count)

Dim ws As Worksheet
Dim objChart As ChartObject
Dim i As Long

Set ws = ThisWorkbook.Sheets("YourSheetName")

For i = 1 To ws.ChartObjects.Count
Debug.Print ws.ChartObjects("Chart " & i).Chart.Name

With ws.ChartObjects("Chart " & i).Chart
'~~> Do what you want here
End With
Next i

关于VBA确定 "for each loops"中对象的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332554/

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