mercredi 26 octobre 2016

Loop as if condition

I have the following question.

In the below code i am trying to get my sub to run a loop after the if then statement, and then have the code go back to the first loop and start from the next i. In my below code,everything works fine, but when the condition in the first IF- statement is met, it starts the second loop, but then imidiately exits it again without running it.

So my question is, how do you make a loop after the then statement in a IF- statement?

Sub Sort3()

Dim i As Integer
Dim LastRow As Long
Dim lenght As String
Dim LastRow_2 As String
Dim L_text As Variant
Dim R_text As Variant
Dim M_text As Variant

  ThisWorkbook.Sheets("EQ_CLEAN").Select

 LastRow = Range("G" & Rows.Count).End(xlUp).Row

 LastRow_2 = Range("J" & Rows.Count).End(xlUp).Row

 For i = 2 To LastRow

 lenght = Range("G" & i)

  If Len(lenght) = 25 Then


     L_text = Left(Range("A" & i), 12)
     R_text = Right(Range("A" & i), 12)

        For x = 2 To Last_row_2

         On Error Resume Next
         n = Worksheets("EQ_CLEAN").Range("D1:D6000").Cells.SpecialCells(xlCellTypeConstants).Count

           If L_text <> Sheets("EQ_CLEAN").Range("J" & x) Then

              Sheets("EQ_CLEAN").Range(Cells(x, 1), Cells(x, 2)).Select
              Application.CutCopyMode = False
              Selection.Copy
              Range("D" & (n + 1)).Select
              Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
              SkipBlanks:=False, Transpose:=False

        End If
    Next x
    End If
  Next i
 End Sub

Aucun commentaire:

Enregistrer un commentaire