簡體   English   中英

Excel VBA嵌套For循環問題

[英]Excel VBA Nested For loop issue

我有一個包含60張紙的大型數據集,我需要從每張紙中分別提取三個值。 這些值將匯總在一張紙中,以便我可以大致了解數據。

我在VBA中參加了一次互聯網崩潰課程,嘗試編寫宏以避免手動執行此操作。 我的想法(能夠翻譯成代碼)是將每張紙上的三個單元格復制到2D數組的行中,因此最終得到一個[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的相應行中?

任何幫助將不勝感激!

For i = 1 To UBound(MeanTable, 1)需要將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM