将数据添加到散布图中,每个图都带有自己的x轴范围

在添加为系列设置x轴范围的行之前,可以select和绘制任何文件组合。 如果在下一组要打印的文件中select了已经在打印图上的文件作为除新文件夹中的第一个文件之外的那一行(最后的“* * * *”),则会发生错误。 如果这条线被评论,问题就会消失。 没有每个系列都有自己的x轴值,有些数据不能正确绘图(也有一个csv文件的示例)。 被绘制的文件是170kB的csv文件。 可以提供工作表和文件。

Sub GetDataAndDisplayChart() Dim vFile, vFiles Dim iFirst As Integer Dim lRow As Long Dim rXValues As Range Dim rYValues As Range Dim iSer As Integer Dim WkShName As String Dim StartWkShCnt As Integer StartWkShCnt = Worksheets.Count ' get a set of CSV files to chart vFiles = Application.GetOpenFilename("csv files (*.csv), *.csv", , "Select files to chart", , True) If TypeName(vFiles) = "Boolean" Then Exit Sub Application.ScreenUpdating = False iFirst = ThisWorkbook.Sheets.Count + 1 ' import each selected CSV file to a new sheet in this workbook For Each vFile In vFiles Workbooks.OpenText vFile, xlWindows, DataType:=xlDelimited, comma:=True ' move the sheet into this workbook ActiveSheet.Move after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) Next Worksheets(iFirst).Select Application.ScreenUpdating = True ' confirm the range to plot Set rXValues = Range("$A$3:$A$10002") Set rYValues = Range("$B$3:$B$10002") Worksheets("Chart").Select With Worksheets("Chart").ChartObjects(1).Chart ' add the first set of data to the existing chart (can have problems if you delete the old data first) .SeriesCollection.Add Union(rXValues, rYValues), serieslabels:=True, categorylabels:=True, Replace:=True .SeriesCollection(.SeriesCollection.Count).Name = Worksheets(iFirst).Range("A1") ' delete the old curves that were already on the chart For iSer = .SeriesCollection.Count - 1 To 1 Step -1 .SeriesCollection(iSer).Delete Next ' add the new data from the addtional files For iSer = iFirst + 1 To Worksheets.Count .SeriesCollection.Add Worksheets(iSer).Range(rYValues.Address) WkShName = Worksheets(iSer).Name .SeriesCollection(iSer - StartWkShCnt).XValues = "= " & WkShName & "!" & rXValues.Address '**** .SeriesCollection(.SeriesCollection.Count).Name = Worksheets(iSer).Range("A1") Next End With ' delete the older worksheets that were already here Application.DisplayAlerts = False For iSer = iFirst - 1 To 1 Step -1 If Worksheets(iSer).Name <> "Chart" Then Worksheets(iSer).Delete Next End Sub