2012-10-09 36 views
0

我完全不熟悉宏和VB,所以很感激任何幫助。 我在這種形式的excel數據:如何在Excel中重複固定行數的宏代碼?

Clip PSNR-codec1  PSNR-codec2  Bit Rates 
Video1 29.426086  29.220891  94357 
Video1 31.207342  31.703124  322832 
Video1 34.474587  34.255598  633468 
Video1 36.445279  35.479189  936961 
Video1 39.093937  36.4768   1539093 
Video2 41.156012  37.295318  326742 
Video2 43.355358  37.684239  604494 
Video2 29.95337  29.30644  1218206 
Video2 32.040252  30.837518  1809751 
Video2 34.194409  32.774954  2387549 
Video3 35.495356  33.806537  1567065 
Video3 36.395173  34.544676  2173151 
Video3 37.077718  35.234943  3094348 
Video3 35.681498  36.036972  3240981 
Video3 171.661771  83.104314  3355959 
Video4 171.247791  96.978608  5103370 
Video4 185.239286  128.064048  6636778 
Video4 189.115735  115.418461  8150015 
Video4 185.35225  154.3189011  2345629 

我的要求是在一個單獨的片創建類型「XYScatterSmooth」的圖表爲每個視頻。該圖應該在X軸上具有比特率,在Y軸上具有PSNR。展望未來,我們也會收集更多視頻的數據。那麼,如何編寫一個宏,將重複這些步驟爲每個視頻(即循環應重複每5行5行的數據是固定的數量。)

Excel版本時間:2010年

回答

0

這與工作對我來說您提供的樣本數據。

將您的所有數據放在工作表1上,刪除其他工作表。您的數據需要從第2行開始(如上所述)。宏會問你視頻的總數(我覺得這比循環更容易,直到一個空的單元格),然後在新的表格上創建圖表。它命名工作表Video1,Video2等。

讓我知道它是否適合你。

Private Sub BitRateCharts() 

' Data should be on Sheet 1. Delete all other sheets. 


    Dim nVideoNum As Integer 
    nVideoNum = 1 

    Dim n As Integer 
    n = 1 

    Dim nStart As Integer 
    nStart = 2 

    Dim nLast As Integer 
    nLast = 6 

    Dim nVideos As Integer 
    nVideos = InputBox("How many videos?") 

    Dim nSheetNum 
    nSheetNum = ActiveSheet.Index 

    Do Until n > nVideos 

    nSheetNum = ActiveSheet.Index 
     If nSheetNum = ThisWorkbook.Sheets.Count Then 
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Video" & nVideoNum 
     Else 
     End If 

      ActiveSheet.Shapes.AddChart.Select 
      ActiveChart.ChartType = xlXYScatterSmoothNoMarkers 
      ActiveChart.SetSourceData Source:=Range("Sheet1!$B$" & nStart & ":$D$" & nLast & "") 
      ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$D$" & nStart & ":$D$" & nLast & "" 
      ActiveChart.SeriesCollection(1).Values = "=Sheet1!$B$" & nStart & ":$B$" & nLast & "" 
      ActiveChart.SeriesCollection(2).XValues = "=Sheet1!$D$" & nStart & ":$D$" & nLast & "" 
      ActiveChart.SeriesCollection(2).Values = "=Sheet1!$C$" & nStart & ":$C$" & nLast & "" 
      ActiveChart.SeriesCollection(1).Name = "=""PSNR-Codec1""" 
      ActiveChart.SeriesCollection(2).Name = "=""PSNR-codec2""" 

      n = n + 1 

      nVideoNum = nVideoNum + 1 

      nStart = nStart + 5 
      nLast = nLast + 5 
    Loop 

    MsgBox ("Macro Complete.") 

Exit Sub 

ErrMsg: 

    MsgBox ("Error encountered. Macro could not complete.") 


End Sub 
+0

非常感謝!這對我有效.. – PerlDev5

+0

太棒了!很高興我能幫上忙。 – tmoore82

+0

還有一個問題:在上面的例子中,我知道第一張紙的名字 - 'Sheet1'。但現在我正在寫一個更通用的代碼,我不知道第一張表的名稱,所以我使用宏Name1 = ActiveWorkbook.Worksheets(1).Name然後如何使用變量「Name1」在循環語句「ActiveChart.SetSourceData Source:= Range(」Sheet1!$ B $「&nStart&」:$ D $「&nLast&」「)中選擇範圍? – PerlDev5