For或While循环来查找数据并将其粘贴到正确的位置

这可能是一个简单的问题,但我一直无法find答案。 我想要做的是遍历一个工作表上出现在同一工作簿中的另一个工作表上的一列工作表名称。 对于每个名字,我想触发一个if then语句,从原始工作表中提取数据,然后将该数据粘贴到操作员名称旁边的单元格中。 我遇到的问题是我不知道如何设置循环。 我的代码在下面,但是我知道这是一团糟,因为我无法正确地获得循环variablesdefind(它给出了一个编译错误“下一个没有”现在)。 任何帮助是非常appriciated。

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 

声明你的范围variables作为范围:

 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