优化循环/嵌套循环
Optimizing loop / nested loop
我正在通过循环工作,自然而然地从一个具有挑战性的循环开始!我有一个包含多个 sheet 的工作簿。每个 sheet 都有完成一个“小部件”的操作。我正在尝试遍历一系列单元格并按日期搜索以找到匹配的日期。如果该日期匹配,我想在该列的第 7 行添加标准时间。我能够通过蛮力代码完成这项工作,并为每一列复制和粘贴我的循环。我真的不想对每个选项卡上的所有列执行此操作。
我确定有一种方法可以使用我的最后一行和最后一列的计数器来执行嵌套循环,所以一旦我在一列中完成循环,它就会移动到下一列。我只是不确定如何到达那里。希望对此有所帮助!谢谢!
编辑:本质上我想做的是从 I12 开始,循环到列的底部寻找日期,然后计算我看到的次数,将 PPC 小时数加起来 (I7)。然后,移动到 J12,循环到列的底部,移动到 K12,循环到底部,为 Assy 添加小时数。等等...
Sub Resource_Overview()
'Summary of daily tasks by worktype
'Declare the variables we'll need
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim i As Double 'for counters, using double to add up decimals
Dim Assy, Solder, QC, Weld, Test, PPC As Double 'variables to hold std hours total
Dim a As Long
Assy = 0#
Solder = 0#
QC = 0#
Weld = 0#
Test = 0#
PPC = 0#
'Find Last Row and Column
Set sht = ActiveSheet
Set StartCell = Range("I12")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
Set sht = ActiveSheet
Set StartCell = Range("I12")
For i = 12 To LastRow
If Range("I" & i).Value = 44154 Then
If Range("I" & 1) = "Assy" Then
Assy = Assy + Range("I7").Value
ElseIf Range("I" & 1) = "Solder" Then
Solder = Solder + Range("I7").Value
ElseIf Range("I" & 1) = "Weld" Then
Weld = Weld + Range("I7").Value
ElseIf Range("I" & 1) = "Test" Then
Test = Test + Range("I7").Value
ElseIf Range("I" & 1) = "PPC" Then
PPC = PPC + Range("I7").Value
ElseIf Range("I" & 1) = "QC" Then
QC = QC + Range("I7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("J" & i).Value = 44154 Then
If Range("J" & 1) = "Assy" Then
Assy = Assy + Range("J7").Value
ElseIf Range("J" & 1) = "Solder" Then
Solder = Solder + Range("J7").Value
ElseIf Range("J" & 1) = "Weld" Then
Weld = Weld + Range("J7").Value
ElseIf Range("J" & 1) = "Test" Then
Test = Test + Range("J7").Value
ElseIf Range("J" & 1) = "PPC" Then
PPC = PPC + Range("J7").Value
ElseIf Range("J" & 1) = "QC" Then
QC = QC + Range("J7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("K" & i).Value = 44154 Then
If Range("K" & 1) = "Assy" Then
Assy = Assy + Range("K7").Value
ElseIf Range("K" & 1) = "Solder" Then
Solder = Solder + Range("K7").Value
ElseIf Range("K" & 1) = "Weld" Then
Weld = Weld + Range("K7").Value
ElseIf Range("K" & 1) = "Test" Then
Test = Test + Range("K7").Value
ElseIf Range("K" & 1) = "PPC" Then
PPC = PPC + Range("K7").Value
ElseIf Range("K" & 1) = "QC" Then
QC = QC + Range("K7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("L" & i).Value = 44154 Then
If Range("L" & 1) = "Assy" Then
Assy = Assy + Range("L7").Value
ElseIf Range("L" & 1) = "Solder" Then
Solder = Solder + Range("L7").Value
ElseIf Range("L" & 1) = "Weld" Then
Weld = Weld + Range("L7").Value
ElseIf Range("L" & 1) = "Test" Then
Test = Test + Range("L7").Value
ElseIf Range("L" & 1) = "PPC" Then
PPC = PPC + Range("L7").Value
ElseIf Range("L" & 1) = "QC" Then
QC = QC + Range("L7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("M" & i).Value = 44154 Then
If Range("M" & 1) = "Assy" Then
Assy = Assy + Range("L7").Value
ElseIf Range("M" & 1) = "Solder" Then
Solder = Solder + Range("M7").Value
ElseIf Range("M" & 1) = "Weld" Then
Weld = Weld + Range("M7").Value
ElseIf Range("M" & 1) = "Test" Then
Test = Test + Range("M7").Value
ElseIf Range("M" & 1) = "PPC" Then
PPC = PPC + Range("M7").Value
ElseIf Range("M" & 1) = "QC" Then
QC = QC + Range("M7").Value
End If
End If
Next i
Sheets("Sheet1").Select
Range("B2") = PPC
Range("B3") = Assy
Range("B4") = Solder
Range("B5") = QC
End Sub
所以你可以建立你的范围:
Range(A1:D1) -> Range(Cells(A1), Cells(D1)) ->
Range(Cells(row number, column number), Cells(row number, column number)) ->
Range(Cells(1, 1), Cells(1, 4))
如果范围是“A1”。我们可以写 Range(Cells(1, 1), Cells(1, 1))
或使用单元格引用 Cells(1,1)
.
要用循环构建范围,您可以用代表循环值的字母替换一些数字,即 column/row 数字。
未经测试,但我想您会明白逻辑:
Sub Resource_Overview()
'Summary of daily tasks by worktype
'Declare the variables we'll need
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim i As, j As Long 'I always use long
Dim Assy, Solder, QC, Weld, Test, PPC As Double 'variables to hold std hours total
Dim a As Long
Assy = 0#
Solder = 0#
QC = 0#
Weld = 0#
Test = 0#
PPC = 0#
'Find Last Row and Column
Set sht = ActiveSheet
Set StartCell = Range("I12")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
Set sht = ActiveSheet
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 = 44154 Then
If Cells(1,i).Value = "Assy" Then
Assy = Assy + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Solder" Then
Solder = Solder + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Weld" Then
Weld = Weld + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Test" Then
Test = Test + Cells(7,i).Value
ElseIf Cells(1,i).Value = "PPC" Then
PPC = PPC + Cells(7,i).Value
ElseIf Cells(1,i).Value = "QC" Then
QC = QC + Cells(7,i).Value
End If
End If
Next j
Next i
Sheets("Sheet1").Range("B2") = PPC
Sheets("Sheet1").Range("B3") = Assy
Sheets("Sheet1").Range("B4") = Solder
Sheets("Sheet1").Range("B5") = QC
End Sub
我正在通过循环工作,自然而然地从一个具有挑战性的循环开始!我有一个包含多个 sheet 的工作簿。每个 sheet 都有完成一个“小部件”的操作。我正在尝试遍历一系列单元格并按日期搜索以找到匹配的日期。如果该日期匹配,我想在该列的第 7 行添加标准时间。我能够通过蛮力代码完成这项工作,并为每一列复制和粘贴我的循环。我真的不想对每个选项卡上的所有列执行此操作。
我确定有一种方法可以使用我的最后一行和最后一列的计数器来执行嵌套循环,所以一旦我在一列中完成循环,它就会移动到下一列。我只是不确定如何到达那里。希望对此有所帮助!谢谢!
编辑:本质上我想做的是从 I12 开始,循环到列的底部寻找日期,然后计算我看到的次数,将 PPC 小时数加起来 (I7)。然后,移动到 J12,循环到列的底部,移动到 K12,循环到底部,为 Assy 添加小时数。等等...
Sub Resource_Overview()
'Summary of daily tasks by worktype
'Declare the variables we'll need
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim i As Double 'for counters, using double to add up decimals
Dim Assy, Solder, QC, Weld, Test, PPC As Double 'variables to hold std hours total
Dim a As Long
Assy = 0#
Solder = 0#
QC = 0#
Weld = 0#
Test = 0#
PPC = 0#
'Find Last Row and Column
Set sht = ActiveSheet
Set StartCell = Range("I12")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
Set sht = ActiveSheet
Set StartCell = Range("I12")
For i = 12 To LastRow
If Range("I" & i).Value = 44154 Then
If Range("I" & 1) = "Assy" Then
Assy = Assy + Range("I7").Value
ElseIf Range("I" & 1) = "Solder" Then
Solder = Solder + Range("I7").Value
ElseIf Range("I" & 1) = "Weld" Then
Weld = Weld + Range("I7").Value
ElseIf Range("I" & 1) = "Test" Then
Test = Test + Range("I7").Value
ElseIf Range("I" & 1) = "PPC" Then
PPC = PPC + Range("I7").Value
ElseIf Range("I" & 1) = "QC" Then
QC = QC + Range("I7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("J" & i).Value = 44154 Then
If Range("J" & 1) = "Assy" Then
Assy = Assy + Range("J7").Value
ElseIf Range("J" & 1) = "Solder" Then
Solder = Solder + Range("J7").Value
ElseIf Range("J" & 1) = "Weld" Then
Weld = Weld + Range("J7").Value
ElseIf Range("J" & 1) = "Test" Then
Test = Test + Range("J7").Value
ElseIf Range("J" & 1) = "PPC" Then
PPC = PPC + Range("J7").Value
ElseIf Range("J" & 1) = "QC" Then
QC = QC + Range("J7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("K" & i).Value = 44154 Then
If Range("K" & 1) = "Assy" Then
Assy = Assy + Range("K7").Value
ElseIf Range("K" & 1) = "Solder" Then
Solder = Solder + Range("K7").Value
ElseIf Range("K" & 1) = "Weld" Then
Weld = Weld + Range("K7").Value
ElseIf Range("K" & 1) = "Test" Then
Test = Test + Range("K7").Value
ElseIf Range("K" & 1) = "PPC" Then
PPC = PPC + Range("K7").Value
ElseIf Range("K" & 1) = "QC" Then
QC = QC + Range("K7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("L" & i).Value = 44154 Then
If Range("L" & 1) = "Assy" Then
Assy = Assy + Range("L7").Value
ElseIf Range("L" & 1) = "Solder" Then
Solder = Solder + Range("L7").Value
ElseIf Range("L" & 1) = "Weld" Then
Weld = Weld + Range("L7").Value
ElseIf Range("L" & 1) = "Test" Then
Test = Test + Range("L7").Value
ElseIf Range("L" & 1) = "PPC" Then
PPC = PPC + Range("L7").Value
ElseIf Range("L" & 1) = "QC" Then
QC = QC + Range("L7").Value
End If
End If
Next i
For i = 12 To LastRow
If Range("M" & i).Value = 44154 Then
If Range("M" & 1) = "Assy" Then
Assy = Assy + Range("L7").Value
ElseIf Range("M" & 1) = "Solder" Then
Solder = Solder + Range("M7").Value
ElseIf Range("M" & 1) = "Weld" Then
Weld = Weld + Range("M7").Value
ElseIf Range("M" & 1) = "Test" Then
Test = Test + Range("M7").Value
ElseIf Range("M" & 1) = "PPC" Then
PPC = PPC + Range("M7").Value
ElseIf Range("M" & 1) = "QC" Then
QC = QC + Range("M7").Value
End If
End If
Next i
Sheets("Sheet1").Select
Range("B2") = PPC
Range("B3") = Assy
Range("B4") = Solder
Range("B5") = QC
End Sub
所以你可以建立你的范围:
Range(A1:D1) -> Range(Cells(A1), Cells(D1)) ->
Range(Cells(row number, column number), Cells(row number, column number)) ->
Range(Cells(1, 1), Cells(1, 4))
如果范围是“A1”。我们可以写 Range(Cells(1, 1), Cells(1, 1))
或使用单元格引用 Cells(1,1)
.
要用循环构建范围,您可以用代表循环值的字母替换一些数字,即 column/row 数字。
未经测试,但我想您会明白逻辑:
Sub Resource_Overview()
'Summary of daily tasks by worktype
'Declare the variables we'll need
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim i As, j As Long 'I always use long
Dim Assy, Solder, QC, Weld, Test, PPC As Double 'variables to hold std hours total
Dim a As Long
Assy = 0#
Solder = 0#
QC = 0#
Weld = 0#
Test = 0#
PPC = 0#
'Find Last Row and Column
Set sht = ActiveSheet
Set StartCell = Range("I12")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column - 3 '-3 columns to not count need date or ECD info
Set sht = ActiveSheet
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 = 44154 Then
If Cells(1,i).Value = "Assy" Then
Assy = Assy + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Solder" Then
Solder = Solder + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Weld" Then
Weld = Weld + Cells(7,i).Value
ElseIf Cells(1,i).Value = "Test" Then
Test = Test + Cells(7,i).Value
ElseIf Cells(1,i).Value = "PPC" Then
PPC = PPC + Cells(7,i).Value
ElseIf Cells(1,i).Value = "QC" Then
QC = QC + Cells(7,i).Value
End If
End If
Next j
Next i
Sheets("Sheet1").Range("B2") = PPC
Sheets("Sheet1").Range("B3") = Assy
Sheets("Sheet1").Range("B4") = Solder
Sheets("Sheet1").Range("B5") = QC
End Sub