在嵌套循环内嵌套循环

时间:2020-10-09 13:59:22

标签: excel vba loops nested-loops

更新:我能够蛮横地执行我想做的事情,并在ResourceNeeds选项卡上的每个日期将代码分成几部分(第二张图片)。这可行,但是笨拙。仍在寻找一种使用循环在ResourceNeeds选项卡上的日期中循环的方法,而不是在我要搜索的每个日期中包含多个代码段。

我有一段代码循环遍历一系列单元格,搜索特定日期,并根据作业类型添加小时数以显示该日期出现的次数。例如,宏在I12:M15中搜索以查找12/1/2020(文本格式44166),如果看到的是该宏,则将循环浏览作业类型并添加与该步骤关联的小时数。宏完成后,它将该日期和那些作业类型的总和放在Sheet1上。我想扩展日期搜索并添加另一个循环,使其根据我在Sheet1 B1:H1中输入的日期搜索7天。我已经尝试了一些方法,但是无法使其引用到Sheet1,然后返回所有数据和循环返回到Sheet。

第一张图片是我正在浏览的工作表 第二张图片是我要从中提取日期并在摘要完成后粘贴摘要的表格。

Tab with data I am looping through Sheet1 where I want the sum to end up and where the dates need to be pulled from

当前代码:当前代码仅针对1个特定日期设置并粘贴到Sheet1上。

Public Function SubAssyArray() As Variant
'Set up the array of sheets (tabs) that we will loop through in the routines below.
'To increase speed, limit the number of tabs in the array to only those you are using as in the example below:
    SubAssyArray = Array("Widget1", "Widget2")
    Dim y As Variant
    End Function
Sub Resource_Overview() 'Summary of daily tasks by worktype
'Don't update the screens as the Macro runs.
Application.ScreenUpdating = False
Application.StatusBar = "Macro is running..."
'Turn off autocalculation to speed things up
Application.Calculation = xlCalculationManual

'Declare the variables we'll need
Dim ws As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim LastColumnP As Long 'this is for pulling resouce totals, used to count number of dates to SUM work type
Dim StartCell As Range
Dim i, j As Double  'for counters, using double to add up decimals
Dim Assy, Solder, QC, PPC As Double 'variables to hold std hours total
Dim rn As Worksheet 'declaring ResourceNeeds sheet as variable
Set rn = Worksheets("ResourceNeeds")

'Need to change the date format to comma style for the loop to search for the dates correclty, we will change the format back to date once loop completes
For Each y In SubAssyArray
Sheets(y).Activate
Set ws = ActiveSheet
Set StartCell = Range("I12")
With ws
    LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
    LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
    ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).Style = "Comma"
End With
Next y
Set y = Nothing

'Make sure counters are set to 0
Assy = 0#
Solder = 0#
QC = 0#
PPC = 0#

'Find Last Row and Column
For Each y In SubAssyArray
Sheets(y).Activate
 Set ws = ActiveSheet
 Set StartCell = Range("I12")
 LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
 LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
    For i = 9 To LastColumn 'Set from which column number you want the loop to start from
        For j = 12 To LastRow
            If Cells(j, i).Value = rn.Cells(1, 4) Then
                If Cells(1, i).Value = "Assy" Then
                    Assy = Assy + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "Solder" Then
                    Solder = Solder + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "QC" Then
                    QC = QC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "PPC" Then
                    PPC = PPC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                End If
            End If
        Next j
    Next i
Next y
        
'Paste results from loops
rn.Cells(2, 2) = PPC
rn.Cells(3, 2) = Assy
rn.Cells(4, 2) = Solder
rn.Cells(5, 2) = QC


'Clear counters for next loop
Assy = 0#
Solder = 0#
QC = 0#
PPC = 0#

'Find Last Row and Column
For Each y In SubAssyArray
Sheets(y).Activate
 Set ws = ActiveSheet
 Set StartCell = Range("I12")
 LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
 LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
    For i = 9 To LastColumn 'Set from which column number you want the loop to start from
        For j = 12 To LastRow
            If Cells(j, i).Value = rn.Cells(1, 4) Then
                If Cells(1, i).Value = "Assy" Then
                    Assy = Assy + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "Solder" Then
                    Solder = Solder + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "QC" Then
                    QC = QC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "PPC" Then
                    PPC = PPC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                End If
            End If
        Next j
    Next i
Next y
        
'Paste results from loops
rn.Cells(2, 3) = PPC
rn.Cells(3, 3) = Assy
rn.Cells(4, 3) = Solder
rn.Cells(5, 3) = QC


'Clear counters for next loop
Assy = 0#
Solder = 0#
QC = 0#
PPC = 0#

'Find Last Row and Column
For Each y In SubAssyArray
Sheets(y).Activate
 Set ws = ActiveSheet
 Set StartCell = Range("I12")
 LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
 LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
    For i = 9 To LastColumn 'Set from which column number you want the loop to start from
        For j = 12 To LastRow
            If Cells(j, i).Value = rn.Cells(1, 4) Then
                If Cells(1, i).Value = "Assy" Then
                    Assy = Assy + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "Solder" Then
                    Solder = Solder + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "QC" Then
                    QC = QC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                ElseIf Cells(1, i).Value = "PPC" Then
                    PPC = PPC + Cells(7, i).Value 'looking at 7th row down to add up the STD HRs
                End If
            End If
        Next j
    Next i
Next y
        
'Paste results from loops
rn.Cells(2, 4) = PPC
rn.Cells(3, 4) = Assy
rn.Cells(4, 4) = Solder
rn.Cells(5, 4) = QC


'Clear counters for next loop
Assy = 0#
Solder = 0#
QC = 0#
PPC = 0#

'Need to change the date format to date format
For Each y In SubAssyArray
Sheets(y).Activate
Set ws = ActiveSheet
Set StartCell = Range("I12")
With ws
    LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
    LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
    ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).NumberFormat = "mm/dd/yy;@"
End With
Next y
Set y = Nothing

'Progress
rn.Select
Application.StatusBar = "Macro is complete..."
Application.StatusBar = False
Application.Calculation = xlCalculationAutomatic
MsgBox "The Macro has finished running."
End Sub

0 个答案:

没有答案