Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
  • Avatar do usuário
  • Avatar do usuário
Avatar do usuário
Por AMORIM123
Posts Avatar
#30559
Salve, galera do forúm.

Tenho o seguinte código abaixo, cujo objetivo é identificar as linhas que possuem 18 caracteres e excluí-los , linha após linha, porém não está ocorrendo desta forma, quando depuro o código e ele chega em "linha = 2" na sequência já pula pra "end sub".
Código: Selecionar todos
Sub excluir_cnpj()

Dim linha As Integer
Dim plan As Worksheet


Set plan = Sheets("plan")
ActiveCell.Range("B2").Select
plan.Select

linha = 2

Do Until plan.Cells(linha, 2) = ""

If Len(ActiveCell) = 18 Then

    ActiveCell.Rows("1:1").EntireRow.Select
    Selection.Delete Shift:=xlUp
    ActiveCell.Select
    
    Exit Sub
    
Else

    linha = linha + 1
    
End If

Loop

End Sub

Segue anexo modelo da planilha para teste

Muito Obrigado a quem puder ajudar!
Você não está autorizado a ver ou baixar esse anexo.
Avatar do usuário
Por gfranco
Avatar
#30561
Boa tarde.
Teste o seguinte e veja se lhe atende.
Código: Selecionar todos
Sub Excluir()

Dim ulinha As Long
Dim w As Worksheet
Dim cel As Range
Dim tamanho As Long
Set w = Plan1
ulinha = w.Cells(w.Rows.Count, 2).End(3).Row


w.Select
w.Range("b2:b" & ulinha).Select


    For Each cel In Selection
    tamanho = VBA.Len(cel.Value)
    
        If tamanho = 18 Then
            cel.FormulaLocal = "=1"
        End If
        
    Next cel


w.Range("b2:b" & ulinha).SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete

End Sub
Por osvaldomp
#30565
Código: Selecionar todos
Sub ExcluiLinhasCNPJ()
 With Sheets("Página1")
  .AutoFilterMode = False
  .Range("A1:Q1").AutoFilter Field:=2, Criteria1:="<>"
  .Range("A2:Q" & Cells(Rows.Count, 2).End(3).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
  .AutoFilterMode = False
 End With
End Sub
Avatar do usuário
Por AMORIM123
Posts Avatar
#30566
gfranco, desde já obrigado pela atenção!

O objetivo seria excluir as linhas da coluna B que contenham 18 caracteres (quantidade de caracteres de um cnpj com mascara)....no código acima ele substituí por "1" e depois apaga de dentro da célula, porém preciso que a linha inteira seja excluida, restando apenas os cpf's suas respectivas linahs
Avatar do usuário
Por gfranco
Avatar
#30567
Boa tarde.
Certifique-se de ter copiado toda a rotina pois ela foi escrita exatamente para excluir a linha inteira.
Avatar do usuário
Por gfranco
Avatar
#30568
Segue o arquivo.
Você não está autorizado a ver ou baixar esse anexo.
#30569
AMORIM123
Boa tarde,

Veja se o código abaixo lhe ajuda...
Resumi ele pra que a execução seja mais rápida e mais simples o entendimento!!!
Código: Selecionar todos
Sub excluir_cnpj()
Dim linha As Integer, plan As Worksheet
Set plan = Sheets("Página1")
linha = plan.Cells(Rows.Count, 2).End(xlUp).Row

For i = 2 To linha
    If Len(plan.Cells(i, 2)) = 18 Then
        plan.Cells(i, 2).EntireRow.Delete
    End If
Next i
End Sub
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord