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

时间:2018-07-06 23:09:33

标签: excel vba excel-vba

您能帮忙检查一下此代码吗?由于图表未显示,因此代码无法正常工作。我正在尝试绘制从另一个打开的工作簿中提取数据的图表。谢谢。

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

2 个答案:

答案 0 :(得分:0)

以下两行将导致错误:

    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 :(得分:0)

您需要将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

您可能希望将x轴设置在循环之外,然后只需将新系列添加到现有图表即可。