Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从打开的工作簿中提取数据的图表_Vba_Excel - Fatal编程技术网

Vba 从打开的工作簿中提取数据的图表

Vba 从打开的工作簿中提取数据的图表,vba,excel,Vba,Excel,你能帮我查一下这个密码吗?代码不起作用,因为图表不出现。我试图绘制一个图表,从另一个打开的工作簿中提取数据。谢谢 Sub Macro2() ' 'Declarations Dim fileName As Variant Dim myFilePath As String Dim Wkbk As Variant myFilePath = "C:\Users\Wonggba\Desktop\y\" fileName = Dir(myFilePath)

你能帮我查一下这个密码吗?代码不起作用,因为图表不出现。我试图绘制一个图表,从另一个打开的工作簿中提取数据。谢谢

Sub Macro2()
'
 'Declarations
    Dim fileName As Variant
    Dim myFilePath As String
    Dim Wkbk As Variant


    myFilePath = "C:\Users\Wonggba\Desktop\y\"
    fileName = Dir(myFilePath)

    While fileName <> ""

        Debug.Print fileName

        Set Wkbk = Workbooks.Open(myFilePath & fileName)      'Open Workbook

        ThisWorkbook.ActiveChart.SeriesCollection.NewSeries
        ThisWorkbook.ActiveChart.FullSeriesCollection(1).Name = fileName
        ThisWorkbook.ActiveChart.FullSeriesCollection(1).XValues = "=[fileName]NPVExcelSheet1!$AY$4:$AY$45" 'The chart should use the data from the open Wkbk
        ThisWorkbook.ActiveChart.FullSeriesCollection(1).Values = "[fileName]NPVExcelSheet1!$AX$4:$AX$45"   'The chart should use the data from the open Wkbk

        Wkbk.Close SaveChanges:=True  'Close file and save changes

        fileName = Dir 'Set the fileName to the next file

    Wend


End Sub
Sub-Macro2()
'
"宣言",
变暗文件名作为变量
将myFilePath设置为字符串
Dim-Wkbk作为变体
myFilePath=“C:\Users\Wonggba\Desktop\y\”
fileName=Dir(myFilePath)
而文件名为“”
调试.打印文件名
设置Wkbk=Workbooks.Open(myFilePath&fileName)'打开工作簿
ThisWorkbook.ActiveChart.SeriesCollection.NewSeries
ThisWorkbook.ActiveChart.FullSeriesCollection(1)。名称=文件名
ThisWorkbook.ActiveChart.FullSeriesCollection(1).XValues=“=[fileName]NPVExcelSheet1!$AY$4:$AY$45””图表应使用打开的Wkbk中的数据
ThisWorkbook.ActiveChart.FullSeriesCollection(1).Values=“[fileName]NPVExcelSheet1!$AX$4:$AX$45”图表应使用打开的Wkbk中的数据
Wkbk.Close SaveChanges:=True'关闭文件并保存更改
fileName=Dir'将文件名设置为下一个文件
温德
端接头

以下两行将导致错误:

    ThisWorkbook.ActiveChart.FullSeriesCollection(1).XValues = "=[fileName]NPVExcelSheet1!$AY$4:$AY$45" 'The chart should use the data from the open Wkbk
    ThisWorkbook.ActiveChart.FullSeriesCollection(1).Values = "[fileName]NPVExcelSheet1!$AX$4:$AX$45"   'The chart should use the data from the open Wkbk
相反,它们应该是:

    ThisWorkbook.ActiveChart.FullSeriesCollection(1).XValues = "=[" & filename & "]NPVExcelSheet1!$AY$4:$AY$45" 'The chart should use the data from the open Wkbk
    ThisWorkbook.ActiveChart.FullSeriesCollection(1).Values = "[" & filename & "]NPVExcelSheet1!$AX$4:$AX$45"   'The chart should use the data from the open Wkbk

在原始代码中,您将文字“filename”放在图表值中,而不是变量
filename
的值。修改后的代码将
文件名的值连接到输入字符串中。

您需要将文件名变量连接到序列源的字符串中。我还添加了一系列关于代码潜在更改的其他观察结果

为了避免覆盖现有的系列1。您需要在循环期间引用新添加的系列

Option Explicit '<==Checks for declarations, spellings, type consistency
Public Sub PlotGraph() '<== Useful descriptive name
    Dim fileName As String '<== Correct type declarations
    Dim myFilePath As String
    Dim Wkbk As Workbook
    Dim counter As Long
    Dim myChart As Chart '<== Use a variable to reference your chart object
    Set myChart = ThisWorkbook.Worksheets("Sheet1").ChartObjects(1).Chart '<<Change as appropriate

    myFilePath = "C:\Users\Wonggba\Desktop\y\"
    fileName = Dir(myFilePath)

    While fileName <> vbNullString '<== vbNullString is faster for comparisons
        Debug.Print fileName
        Set Wkbk = Workbooks.Open(myFilePath & fileName) 'Open Workbook

        With myChart '<== Use a With statement to work with parent object and reduce code repetition
            .SeriesCollection.NewSeries '<== SeriesCollection is more robust
             counter = counter + 1
            .SeriesCollection(counter).Name = fileName
            .SeriesCollection(counter).Values = "[" & fileName & "]NPVExcelSheet1!$AX$4:$AX$45" 'The chart should use the data from the open Wkbk
            .SeriesCollection(counter).XValues = "[" & fileName & "]NPVExcelSheet1!$AY$4:$AY$45" 'The chart should use the data from the open Wkbk
        End With

        Wkbk.Close
        fileName = Dir                           'Set the fileName to the next file
    Wend
End Sub

Option Explicit'在tgsAm中完全封装代码对不起,我不明白你的意思。你在什么时候要求图表“出现”?你为什么要提出来?谢谢。但该图表似乎只绘制了一个excel工作簿数据,而没有绘制其他数据。或者在下一个循环之前删除图表上每个循环的绘图。我想做的是将每个循环的数据一起绘制在同一个图表中。你是第一次这样做的人,但回答每个费心回答你问题的人被认为是一种良好的礼仪。谢谢你提供的信息。我会记住的,不用担心。欢迎来到SO:-)感谢您抽出时间帮助编写代码。非常感谢。