Excel 在嵌套循环中嵌套循环

Excel 在嵌套循环中嵌套循环,excel,vba,loops,nested-loops,Excel,Vba,Loops,Nested Loops,更新:我可以在ResourceRequirements选项卡(第二张图片)上强制执行我要做的事情,并为每个日期将代码分成几个部分。这是可行的,但很笨重。仍然在寻找一种方法,使用循环遍历ResourceNeeds选项卡上的日期,而不是为我正在搜索的每个日期使用多段代码 我有一段代码,它在一系列单元格中循环,搜索特定的日期,根据工作类型为日期出现的次数添加小时数。例如,宏在I12:M15中搜索12/1/2020(文本格式44166),如果它看到了这一点,它将循环遍历作业类型并添加与该步骤相关的小时数

更新:我可以在ResourceRequirements选项卡(第二张图片)上强制执行我要做的事情,并为每个日期将代码分成几个部分。这是可行的,但很笨重。仍然在寻找一种方法,使用循环遍历ResourceNeeds选项卡上的日期,而不是为我正在搜索的每个日期使用多段代码

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

第一张图片是我正在循环的那张纸 第二张图片是我想从中提取日期并在完成后粘贴摘要的工作表

当前代码:当前代码仅为1个特定日期设置,并粘贴在表1上

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

我将从应用本文中的指针开始:选择工作表,然后使用没有限定工作表对象的范围/单元格,这会导致代码脆弱,当它不能按预期工作时,很难调试。谢谢你,你提供的链接中有很多好东西。我不确定它是否能帮我解决问题,但肯定能帮我清理一些代码:)我发布该链接的主要目的是,如果你不编写一堆不适用这些指导原则的代码,就很难遵循,因此人们(包括我在内)往往不愿意深入研究并尝试“修复”它。