简体   繁体   中英

Excel VBA Loop That Not Stops Based On Activecell Value

I have a loop that needs to stop when activecell value is "BBSE", but it passes the cell and continues the loop. someone can help me with that? I cut rows from table in one workbbok and paste it to another. before the list in column FI have many blank cells, and because of that I am usind xldown. Here is the relevant code:

'Illuminators Worksheet
OP_wb.Activate
Range("F2").End(xlDown).Select


Do Until ActiveCell.Value = "BBSE"
    OP_wb.Activate
    Worksheets("Optic Main").Activate
    Range("F2").End(xlDown).Select
    Selection.EntireRow.Cut
    Demand_WB.Activate
    Worksheets("Illuminators").Activate
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveSheet.Paste


Loop

Here is where I want to stop the loop in the red circle:

在此处输入图片说明

this is why I am using END.xlDown

在此处输入图片说明

If I understand what you are trying to achieve correctly, I believe the following will achieve it:

Dim startRow As Long
Dim endRow As Long
With OP_wb.Worksheets("Optic Main")
    startRow = .Range("F2").End(xlDown).Row
    endRow = .Columns("F").Find(What:="BBSE", LookIn:=xlValues, LookAt:=xlWhole).Row
    .Rows(startRow & ":" & endRow).Cut
End With
With Demand_WB.Worksheets("Illuminators")
    .Range("A" & .Rows.Count).End(xlUp).Offset(1).Insert Shift:=xlDown
End With

May be try like this...

'Mentioning Starting Row Here
x = 2

Do
    'x refers to Row and F refer to column name
    With Cells(x, "F")
        'Exiting Do Loop once it finds the matching value using If statement
        If .Value = "BBSE" Then Exit Do
        OP_wb.Activate
        Worksheets("Optic Main").Activate
        .EntireRow.Cut
        Demand_WB.Activate
        Worksheets("Illuminators").Activate
        Range("A" & Rows.Count).End(xlUp).Offset(1).Select
        ActiveSheet.Paste
    End With
    'Incrementing row number here to move on to next row
    x = x + 1
Loop

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