Question

Thanks to a previous post and some excellent direction I was able to create successfully working code for my needs. However, the table/data has changed a bit and I have not yet reached my ultimate goal of iterating the paste function based on another cell's value. I have seen this post on SO but it has not helped me. I found other resources on using loops in Excel VBA and created the very simple Do While code you see below.

My problem: I cannot get the code to stop looping when it reaches the value from Column F. It finds the first available case and outputs the correct information endlessly without stopping at n value and moving on.

Here is a look at the data table:

     A    B   C        D     E    F
 R1 Name Num Status   #Orig #Act #Rem
 R2 ABC  032 Complete 22    0    11
 R3 LMN  035 In Prog  25    21   4

Here is my code:

Sub Copy_Pending_Status()

Dim srcrange As Range
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim i As Integer

Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("SIS Agregate")
Set ws2 = wb.Worksheets("Center Detail")
Set srcrange = ws2.Range("C2:C61")

For Each Row In srcrange.Cells
    Select Case Row.Value
        Case "In Progress"
            Do While i <= Row.Offset(0, 3).Value
                Set LastCell = ws1.Cells(ws1.Rows.Count, "C").End(xlUp).Offset(1)
                LastCell.Offset(0, 0).Value = Row.Offset(0, -2).Value
                LastCell.Offset(0, 12).Value = Row.Offset(0, -1).Value
                LastCell.Offset(0, 4).Value = "Not Yet Scanned"
            Loop

        Case "Complete"
            Do While i <= Row.Offset(0, 3).Value
                Set LastCell = ws1.Cells(ws1.Rows.Count, "C").End(xlUp).Offset(1)
                LastCell.Offset(0, 0).Value = Row.Offset(0, -2).Value
                LastCell.Offset(0, 12).Value = Row.Offset(0, -1).Value
                LastCell.Offset(0, 4).Value = "Purged"
            Loop
    End Select
Next Row

End Sub

I have tried setting the Do While i <= Row.Offset(0, 3).Value to (0,3) (0,4) and (0,6) as I am not sure if it is counting the number of columns from "A" as one, "C" as one, or "C" as zero and offset is three (to the right).

I want to note that eliminating the Do While and Loop lines of code from each case make this function correctly, but only once per row, per case - as opposed to however many times Column F indicates.

Have I oversimplified this and am missing some obvious key information? What change do I need to make to get this little loop to function correctly?

Était-ce utile?

La solution

Nothing within your loops changes the value of i, and so the "Do While" conditions are always met. You need to iterate through values of i so that eventually the conditions will not be met. At the end of each loop, add:

i = i + 1

This will allow it to count how many times the loop has been run and stop after a set number of iterations based on Column F.

You'll also want to re-set i to zero in between each row. Do this between "End Select" and "Next Row":

End Select i = 0 Next Row

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top