简体   繁体   中英

For or While loop to find data and paste it into correct location

This is probably a simple questions but I have been unable to find the answer. What I'm trying to do is iterate through a column of operator names on one worksheet that appear on another worksheet in the same workbook. For each name I want to trigger a if then statement that pulls data from the original worksheet then pastes that data in the cell next to the operator's name. The problem I'm having is that I don't really know how to set up the loops. My code is below, but I know that it is a mess because I cannot get the loop variables defind correctly (it gives a compile error "next without for" right now). Any help is greatly 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

Declare your range variables as ranges:

Dim OpRange as Range
Dim ScrapRange as Range
Dim i as Long 'Use this as a counter in the loops

Assign them thusly:

Set OpRange = Range("B32:B" & Range("B" & Cells.Rows.Count).End(xlUp).Row)

Assign ScrapRange using the Offset method: this is one column right of OpRange :

Set ScrapRange = OpRange.Offset(0,1)

Then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM