2016-08-02 192 views
0

我有一個很大的數據集,其中有60張表格,我需要從中分別提取每個表格的三個值。這些值將彙總在一張表中,以便我可以對數據進行概述。Excel VBA嵌套For循環問題

我參加了VBA的互聯網速成課程,嘗試編寫一個宏以避免必須手動執行此操作。我的想法(我能夠翻譯成代碼)是將每張紙上的三個單元格複製到二維數組的行中,所以我最終會得到一個[60x3]矩陣,可以將其複製到新創建的表單中。 」。 (據我所知,這是非常低效的,但它是目前我能想到的最好的。)

該代碼運行但產生不良結果:矩陣只包含來自最終工作表的值的三元組。我真正需要從我的宏中,它從sheet1複製三個值並將它們粘貼到MeanTable(1,:),然後從sheet2複製三個值並將它們粘貼到MeanTable(2,:)等等。我相信這是因爲我的第一個嵌套循環是垃圾,所以我一直在嘗試不同的循環和添加循環(當然還有網絡搜索),但到目前爲止我還沒有能夠解決它。

Sub copy_to_one_sheet() 'copy sample means from each sheet to Means 
Application.ScreenUpdating = False 

Dim ws As Worksheet 
Dim NumSheets As Integer 
Dim NumSamples As Integer 
Dim MeanTable() As Long 'store sample means in this 2D array, its size defined by number of sheets and samples per sheet 
NumSheets = Application.Sheets.Count 'count number of sheets 
NumSamples = 3 'number of samples per sheet (hardcoded for now) 
ReDim MeanTable(NumSheets, 1 To NumSamples) 'MeanTable will be filled with sample means 

'============================================ 
'= copy sample means per sheet to MeanTable = 
'============================================ 
For i = 1 To UBound(MeanTable, 1) 'copy sample means from fixed columns per sheet to individual rows of Table array 

    For Each ws In ThisWorkbook.Worksheets 'go through sheets 

     MeanTable(i, 1) = ws.Cells(Rows.Count, 3).End(xlUp).Offset(-3, 0).Value 
     MeanTable(i, 2) = ws.Cells(Rows.Count, 10).End(xlUp).Offset(-3, 0).Value 
     MeanTable(i, 3) = ws.Cells(Rows.Count, 17).End(xlUp).Offset(-3, 0).Value 

    Next ws 
Next i 
'============================================= 
'= create Sheet("Means") and paste MeanTable = 
'============================================= 
With ThisWorkbook 
    Set Dst = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 'create new worksheet 
    Dst.Name = "Means" 'worksheet name 
    With Sheets("Means") 
     For k = 1 To UBound(MeanTable, 1) 
      For l = 1 To NumSamples 
       Cells(k, l).Value = MeanTable(k, l) 'paste Table variable with sample means to new worksheet ("Means") 
      Next l 
     Next k 
    End With 
End With 
End Sub 

我的問題是:如何才能讓我的循環週期,通過各表在工作簿中並複製值MeanTable的相應行的三元組,之前移動到下一個表?

任何幫助將不勝感激!

回答

0

For i = 1 To UBound(MeanTable, 1)需要用計數器i = i + 1來代替。使用Range.Resize來填充數組中的一個範圍。

Sub copy_to_one_sheet()        'copy sample means from each sheet to Means 
    Application.ScreenUpdating = False 

    Dim ws As Worksheet 
    Dim NumSheets As Integer 
    Dim NumSamples As Integer 
    Dim MeanTable() As Long       'store sample means in this 2D array, its size defined by number of sheets and samples per sheet 
    NumSheets = Application.Sheets.Count    'count number of sheets 
    NumSamples = 3         'number of samples per sheet (hardcoded for now) 
    ReDim MeanTable(1 To NumSheets, 1 To NumSamples) 'MeanTable will be filled with sample means 

    For Each ws In ThisWorkbook.Worksheets   'go through sheets 
     i = i + 1 
     MeanTable(i, 1) = ws.Cells(Rows.Count, 3).End(xlUp).Offset(-3, 0).Value 
     MeanTable(i, 2) = ws.Cells(Rows.Count, 10).End(xlUp).Offset(-3, 0).Value 
     MeanTable(i, 3) = ws.Cells(Rows.Count, 17).End(xlUp).Offset(-3, 0).Value 

    Next ws 

    With ThisWorkbook 
     Set Dst = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 'create new worksheet 
     Dst.Name = "Means"       'worksheet name 
     With Sheets("Means") 
      .Range("A1").Resize(UBound(MeanTable, 1), UBound(MeanTable, 2)).Value = MeanTable 

     End With 
    End With 
End Sub