2017-10-16 60 views
0

我正在研究一个将去我的工作簿,清理数据(添加列,更改单位等)的宏的宏。清理数据后没有问题,我试图在工作表上创建一个散点图。下面的代码留下了清理的东西,因为它是无关紧要的。我已经尝试了一些迭代,包括录制宏,这是我的最后一次尝试。原来来自不同excel文件的表单出现问题。每张纸都有相同的格式/组织结构,但是,它们每个都有不同的列长度(因为每个数据的长度取决于实验持续的时间长短)。有没有人有什么建议?Scatterplot w/VBA在Excel中的多个工作表

Sub Cleaning() 

    Application.ScreenUpdating = False 

    For Each sh In Worksheets 

     sh.Activate 
     'find column length for loop 
     Dim collength As Integer 
     collength = Cells(Rows.Count, "A").End(xlUp).Row 
     'plot curves 
     ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select 
     ActiveChart.SetSourceData Source:=Range(ActiveSheet.Name & "!$C$1:$C$" & collength, ActiveSheet.Name & "!$Q$1:$Q$" & collength) 

    Next sh 

    Application.ScreenUpdating = True 

End Sub 

在我的第二次尝试中,我尝试过......仍然没有运气。

Dim strx As String 
Dim stry As String 
Dim rngx As Range 
Dim rngy As Range 

strx = "=" & ActiveSheet.Name & "!$C$2:$C$" & collength 
stry = "=" & ActiveSheet.Name & "!$Q$2:$Q$" & collength 

Dim Chart1 As Chart 
    Set Chart1 = Charts.Add 
    With Chart1 
     .ChartType = xlXYScatter 
     .SeriesCollection.NewSeries 
     'Change to what your series should be called 
     .SeriesCollection(1).Name = "=""Values""" 
     .SeriesCollection(1).XValues = "=" & rngx 
     .SeriesCollection(1).Values = "=" & rngy 
    End With 

在我的第三次尝试,我记录一个宏并将其编辑成自动调整到活动工作表的列的长度,但是,我得到最后一行1004错误。

Sub plotting_test() 

Application.ScreenUpdating = False 
For Each sh In Worksheets 
    sh.Activate 
'find column length for loop 
    Dim collength As Integer 
    collength = Cells(Rows.Count, "A").End(xlUp).Row 
    '[B3].Value = collength 


    Range("C1").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Range("B1:B" & collength & ",Q1").Select 
    Range("Q1").Activate 
    Range(Selection, Selection.End(xlDown)).Select 
    ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select 
    ActiveChart.SetSourceData Source:=Range(_ 
     ActiveSheet.Name & "!$B$1:$B$" & collength & "," & ActiveSheet.Name & "!$Q$1:$Q$" & collength) 


Next sh 
Application.ScreenUpdating = True 

End Sub 
+0

参见[这](https://stackoverflow.com/questions/46594615/creating-a-scatter-plot-containing-series-dynamically-using-vba-in-microsoft-exc/46596948#46596948 ) –

回答

0

下没有产生任何错误,并制作根据您所提供的数据图:这基本上是你的代码反正

Sub mysub() 
Dim sh As Worksheet 

Application.ScreenUpdating = False 
For Each sh In Worksheets 
sh.Activate 
'find column length for loop 
Dim collength As Integer 
collength = Cells(Rows.Count, "A").End(xlUp).Row 
'[B3].Value = collength 

Range("C1").Select 
Range(Selection, Selection.End(xlDown)).Select 
Range("B1:B" & collength & ",Q1").Select 
Range("Q1").Activate 
Range(Selection, Selection.End(xlDown)).Select 
sh.Shapes.AddChart2(240, xlXYScatter).Select 
ActiveChart.SetSourceData Source:=Range(_ 
ActiveSheet.Name & "!$B$1:$B$" & collength & "," & ActiveSheet.Name & "!$Q$1:$Q$" & collength) 

Next sh 
Application.ScreenUpdating = True 

End Sub 

,减去子名称和点心SH作为工作表。

既然你正在通过sh循环,那么使用它来代替Activesheet是有意义的。所以sh.Name,会给你当前的表名,但是总的来说,你的代码工作。你能否提供更多关于你可能得到的你不期望的信息,以及可能的一些数据样本以进一步提供帮助?

+0

这最终给了我一些其他的错误。这次没有运气。 – remix090378

0

我终于明白了! 该代码进入每个工作表,清理数据并正确格式化,然后绘制所需系列。

Sub clean_and_graph() 

'start with the first sheet 
Application.ScreenUpdating = False 
For Each sh In Worksheets 
    sh.Activate 


'find the collength for the series entry 
    Dim collength As Integer 
    collength = Cells(Rows.Count, "A").End(xlUp).Row 
'clean up the data 
    'Inserting a Column at Column C 
     Range("C1").EntireColumn.Insert 
     [C1].Value = "time" 

     'eqn for first row in column 
     [C2].Value = "=if(B2>0,24*(B2-$B$2))" 
     'autofill rest of columns and format data to general 
     Range("C2:C" & collength).FillDown 
     Columns(3).NumberFormat = "General" 

    'create the chart as an object in the worksheet 
    Dim myChtObj As ChartObject 
    Set myChtObj = ActiveSheet.ChartObjects.Add _ 
     (Left:=100, Width:=375, Top:=75, Height:=225) 
    myChtObj.Chart.SetSourceData Source:=Sheets(ActiveSheet.Name).Range("C2:Q" & collength) 
    myChtObj.Chart.ChartType = xlXYScatterLines 


    With myChtObj.Chart 

     ' make an XY chart 
     .ChartType = xlXYScatterLines 

     ' remove extra series 
     Do Until .SeriesCollection.Count = 0 
      .SeriesCollection(1).Delete 
     Loop 
     With .SeriesCollection.NewSeries 
     .Name = ActiveSheet.Range("Q1") 
     .Values = ActiveSheet.Range("Q2:Q" & collength) 
     .XValues = ActiveSheet.Range("C2:C" & collength) 
     End With 
    End With 


'go to the next sheet 
    Next sh 
Application.ScreenUpdating = True 

End Sub 
相关问题