从另一个工作簿中用范围填充SeriesCollection

如何解决从另一个工作簿中用范围填充SeriesCollection

我正在计划一个用户表单,该表单将使用其他文件中的数据生成图形。我试图使用另一个工作簿中的数据填充Chart1中的seriescollection。虽然,我的程序导致空seriescollection。下面是该程序的代码(某些部分已删除,因为它们与问题无关)。

Dim ChtsBig(),openWb As Workbook,genWb As Workbook
Set genWb = ActiveWorkbook
If ListBox1.ListCount = 0 Then MsgBox ("Select files!")
ReDim Preserve ChtsBig(1 To ListBox1.ListCount)
For i = 1 To ListBox1.ListCount
    fileWb = ListBox1.List(i - 1,1)
    Set openWb = Application.Workbooks.Open(Filename:=fileWb,ReadOnly:=True)
    Set sht1 = openWb.Worksheets(1)
    Set sht2 = openWb.Worksheets("Cycles")
    Set ChtsBig(i) = genWb.Charts.Add
    With ChtsBig(i)
        .Name = "Cell " & Left(ListBox1.List(i - 1,0),4)
        .ChartType = xlXYScatterSmoothNoMarkers
        j = 1
        k = 1
        For curr_cyc = TextBox7.Value To TextBox8.Value Step TextBox9.Value
            Do
                If sht2.Cells(1 + j,1) = curr_cyc Then
                    cyc_row = 1 + j
                    found = True
                End If
                j = j + 1
            Loop Until found = True Or sht2.Cells(2 + j,1) = Empty
            If found = True Then
                .SeriesCollection.NewSeries
                .SeriesCollection(k).Name = "Cycle " & curr_cyc & " Charge"
                .SeriesCollection(k).XValues = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,4),16),sht1.Cells(3 + sht2.Cells(cyc_row,5),16))
                .SeriesCollection(k).Values = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,9),9))
                Numpoint1 = .SeriesCollection(k).Points.Count
                .SeriesCollection(k).Points(Numpoint1).MarkerStyle = xlMarkerStyleTriangle
                .SeriesCollection.NewSeries
                .SeriesCollection(k + 1).Name = "Cycle " & curr_cyc + 1 & " Discharge"
                .SeriesCollection(k + 1).XValues = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,6),7),16))
                .SeriesCollection(k + 1).Values = sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,9))
                .SeriesCollection(k + 1).MarkerStyle = xlMarkerStyleNone
                .SeriesCollection(k + 1).Border.LineStyle = xlDash
                Numpoint2 = .SeriesCollection(k + 1).Points.Count
                .SeriesCollection(k + 1).Points(Numpoint2).MarkerStyle = xlMarkerStyleDiamond
                currentSeriesColorindex = (k + 1) / 2 + 40
                If (k + 1) / 2 + 40 < 57 Then
                    currentSeriesColorindex = (k + 1) / 2 + 40
                Else
                    currentSeriesColorindex = (k + 1) / 2 + 32
                End If
                .SeriesCollection(k).Points(Numpoint1).MarkerForegroundColorIndex = currentSeriesColorindex
                .SeriesCollection(k).Points(Numpoint1).MarkerBackgroundColorIndex = currentSeriesColorindex
                .SeriesCollection(k + 1).Points(Numpoint2).MarkerForegroundColorIndex = currentSeriesColorindex
                .SeriesCollection(k + 1).Points(Numpoint2).MarkerBackgroundColorIndex = currentSeriesColorindex
                .SeriesCollection(k + 1).Border.ColorIndex = currentSeriesColorindex
                .SeriesCollection(k).Border.ColorIndex = currentSeriesColorindex
            End If
            found = False
            k = k + 2
        Next
        .HasTitle = False
        .Axes(xlValue,xlPrimary).MinimumScale = 2.5
        .Axes(xlValue,xlPrimary).MaximumScale = 4.5
        .Axes(xlCategory,xlPrimary).MinimumScale = 0.0001
        .Axes(xlCategory).TickLabels.NumberFormat = "#0,0"
        .Axes(xlCategory).HasTitle = True
        .Axes(xlCategory).AxisTitle.Caption = "Charge / Ah"
        .Axes(xlValue).HasTitle = True
        .Axes(xlValue).AxisTitle.Caption = "Voltage / V"
        .Axes(xlCategory).AxisTitle.Font.Bold = False
        .Axes(xlValue).AxisTitle.Font.Bold = False
        .Legend.IncludeInLayout = False
        .Legend.Interior.Color = RGB(255,255,255)
        .Axes(xlCategory).HasMajorGridlines = True
        .Axes(xlCategory).MajorGridlines.Border.Color = RGB(160,160,160)
        .Axes(xlCategory).MajorGridlines.Border.LineStyle = xlDash
        .Axes(xlValue).MajorGridlines.Border.Color = RGB(160,160)
        .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDash
    End With
    openWb.Close SaveChanges:=False
Next

在调试此范围时:sht1.Range(sht1.Cells(3 + sht2.Cells(cyc_row,16))为空,.SeriesCollection(k).XValues在Value2中具有正确的值。

澄清一下,sht1拥有图表的原始数据,但已分为多个块(例如,一个数据集位于行250至500之间)。 Sht2包含有关这些块的位置的信息。

如果有帮助,这是图形的外观:Generated Graph

解决方法

此模式:

If found = True Then
    .SeriesCollection.NewSeries
    .SeriesCollection(k).Name = "Cycle " & curr_cyc & " Charge"

容易出现一些问题,因为它取决于是否存在预先存在的系列。由于NewSeries返回添加的序列,因此会更可靠:

Dim s1 As Series

If found = True Then
    Set s1 = .SeriesCollection.NewSeries() 'get a reference on creation
    s1.Name = "Cycle " & curr_cyc & " Charge"

每当添加新图表以确保excel都没有为您“自动添加”任何系列时,这都是一个好主意,方法是在开始添加系列之前检查SeriesCollection.Count

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res