For if it doesn’t work

Asked

Viewed 60 times

-3

The code needs to test a two-sentence list. If he finds this phrase, he copies it and pastes it into the cell on the left, until he finds the next sentence. Everything works fine in the first For (For i), but in the second (For j) it doesn’t seem to work anymore.

base do excel

Sub o()

For i = 1 To 30
If Cells(i, 2) = "01 dormitório" Then Exit For
ActiveSheet.Paste
Selection.Offset(1, 0).Select
Next i

Selection.Offset(0, 1).Select
Selection.Copy
Selection.Offset(0, -1).Select

For j = 1 To 30
If Cells(j, 2) = "02 Dormitórios" Then Exit For

ActiveSheet.Paste
Selection.Offset(1, 0).Select
Next j

Selection.Offset(0, 1).Select
Selection.Copy
Selection.Offset(0, -1).Select

End Sub
  • In the description is "and glue in the cell before it" (previous is the immediately above?), but in the example screen, is fixed in the "A1" (GLUE HERE THE SENTENCE...). What is expected? If it is in the "A1", the "01 dormitory" will be superimposed by the "02 Dormitory", for example.

  • Wrong description. It’s "glue on the left cell"

1 answer

-1


The original code has some problems, I’ll rephrase and explain some points.

Example 01 - Following the original logic of a For for each test phrase:

Sub testeFraseExemplo1()

Dim linha As Integer 'declara a linha como inteiro

For linha = 1 To 30 'verificará da linha 1 até a 30
    If Cells(linha, 2) = "01 dormitório" Then 'verifica se é o texto esperado
        Cells(linha, 2).Copy Cells(linha, 1) 'copia a célula da linha do for coluna B, para a célula mesma linha coluna A
        Exit For 'encerra o for, pois já encontrou
    End If 'fecha if
Next linha 'próxima linha, se não foi encerrado no if verdadeiro

For linha = 1 To 30
    If Cells(linha, 2) = "02 Dormitórios" Then
        Cells(linha, 2).Copy Cells(linha, 1)
        Exit For
    End If
Next linha

End Sub

Note that you do not need to use another line name (as you did with i and j). You can reuse it, the For redefine to 1 To 30 (or another range you set when declaring it). The rest of the code is identical to the top block, changing the phrase only.

Example 02 - It is possible to use the same For, with more than one If. It is also possible to use the same If with more than one condition (test phrase).

Sub testeFraseTipoExemplo2()

Dim linha As Integer

For linha = 1 To 30
    If Cells(linha, 2) = "01 dormitório" Or _
        Cells(linha, 2) = "02 Dormitórios" Then 'pode ir acrescentando outros Or ou And
        Cells(linha, 2).Copy Cells(linha, 1)
    End If
Next linha

End Sub

Choosing one or the other depends on the final design, is related to maintenance, performance of having multiple For analyzing the same lines and etc. Having to indicate the second option, but everything depends.

  • Very good. Is there any way for each line tested it paste in the cell below the word copied?

  • Yes, it’s possible, but it’s likely to cause you mistakes. Imagine that line 06 has "01 dorm" and line 07 "02 dorms", then when For reading line 06 it will copy and overwrite the current content of line 07 by "copied". Now, when reading line 07 you will no longer have "02 Dorms". It doesn’t seem to make sense to implement this rule, but the code would be after the line it copies: Cells(line+1, 2) = "copied".

  • Thank you very much jean. Solved everything. Just one more thing. How many years to have this knowledge?

  • I wouldn’t say it’s a matter of years... But resilience and dedication. We always have something to learn (no philosophy, really). Good luck in studies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.