这可能是一个简单的问题,但我一直无法找到答案。我尝试做的是在同一工作簿中另一个工作表上显示的一个工作表上迭代一列操作符名称。对于每个名称,我想触发if then语句从原始工作表中提取数据,然后将该数据粘贴到运算符名称旁边的单元格中。我遇到的问题是我不知道如何设置循环。我的代码在下面,但我知道它是一团糟,因为我无法正确定义循环变量(它给出了编译错误"接下来没有#34;现在)。任何帮助都很受欢迎。
Sub OperatorScrap()
Dim str_dateMin As String
Dim str_dateMax As String
Dim dateMin As Date
Dim dateMax As Date
Dim lastRow As Long
Dim subTotal As Double
Dim lookupDate As Date
Dim subTotal2
Dim OpRange
Dim Orange As Variant
Dim OpName
Dim ScrapRange
Dim ScrapR As Variant
OpRange = "B32:B" & Range("B" & Cells.Rows.Count).End(xlUp).Row
ScrapRange = "C32:C" & Range("C" & Cells.Rows.Count).End(xlUp).Row
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
subTotal = 0
subTotal2 = 0
str_dateMin = InputBox("Input beginning date, mm/dd/yyyy:")
str_dateMax = InputBox("Input end date, mm/dd/yyyy:")
dateMin = CDate(str_dateMin)
dateMax = CDate(str_dateMax)
For ScrapR = 1 To ScrapRange
For Orange = 1 To OpRange
Do While OpRange = "daniel"
OpRange = OpRange + 1
For lRow = 2 To lastRow
lookupDate = Sheets("Sheet1").Cells(lRow, "AR").Value
OpName = "Daniel"
If dateMin <= lookupDate And lookupDate <= dateMax And Sheets("sheet1").Cells(lRow, "A").Value
_= OpName Then
subTotal = subTotal + Sheets("Sheet1").Cells(lRow, "AV").Value
subTotal2 = subTotal2 + Sheets("Sheet1").Cells(lRow, "N").Value
End If
Next lRow
Next Orange
If subTotal2 <> 0 Then
Sheets("Scrap").Activate
Range("c32").Value = (subTotal) / subTotal2
End If
If subTotal2 = 0 Then
ActiveSheet.Range("B32").EntireRow.Delete
End If
Next ScrapR
subTotal = 0
subTotal2 = 0
Loop
End Sub
答案 0 :(得分:1)
将范围变量声明为范围:
Dim OpRange as Range
Dim ScrapRange as Range
Dim i as Long 'Use this as a counter in the loops
如此分配它们:
Set OpRange = Range("B32:B" & Range("B" & Cells.Rows.Count).End(xlUp).Row)
使用Offset
方法指定ScrapRange:这是OpRange
右侧的一列:
Set ScrapRange = OpRange.Offset(0,1)
然后:
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
subTotal = 0
subTotal2 = 0
str_dateMin = InputBox("Input beginning date, mm/dd/yyyy:")
str_dateMax = InputBox("Input end date, mm/dd/yyyy:")
dateMin = CDate(str_dateMin)
dateMax = CDate(str_dateMax)
'Since OpRange and ScrapRange are same size, you can iterate them in parallel with
' a counter variable, "i"
For i = 1 To OpRange.Cells.Count
'You can remove these two lines, just use them to debug
MsgBox OpRange.Cells(i, 1).Value
MsgBox ScrapRange.Cells(i, 1).Value
'I think this is what you need:
' sets the OpName based on the currenty "i" cell in the iteration of OpRange
' so each iteration of "i" will give a different value based on the OpRange
OpName = OpRange.Cells(i, 1).Value
'This iterates another worksheet and does your lookup:
For lRow = 2 To lastRow
lookupDate = Sheets("Sheet1").Cells(lRow, "AR").Value
If dateMin <= lookupDate And _
lookupDate <= dateMax And _
Sheets("sheet1").Cells(lRow, "A").Value = OpName Then
subTotal = subTotal + Sheets("Sheet1").Cells(lRow, "AV").Value
subTotal2 = subTotal2 + Sheets("Sheet1").Cells(lRow, "N").Value
End If
Next lRow
If subTotal2 <> 0 Then
Sheets("Scrap").Range("c32").Value = (subTotal) / subTotal2
End If
If subTotal2 = 0 Then
Sheets("Scrap").Range("B32").EntireRow.Delete
End If
'Reset your subtotals:
subTotal = 0
subTotal2 = 0
Next i