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
Por Wieniawski
Posts Avatar
#33521
Olá!

Preciso por favor de um código VBA para exclusão de linhas de acordo com determinado critério, ou seja, que toda linha cuja coluna B conste valores acima de 8000000000 sejam excluídas.

Alguém poderia me ajudar?

Grato!
Avatar do usuário
Por gfranco
Avatar
#33529
Boa tarde.
Veja se te ajuda.

Código: Selecionar todos
Option Explicit

Sub excluir()

Dim w As Worksheet
Dim lastRowData As Long
Dim i As Long
Dim valor As Variant
Dim counter As Long



Set w = ActiveSheet
counter = 0
lastRowData = w.Cells(w.Rows.Count, 2).End(3).Row

If lastRowData < 2 Then GoTo finaliza

valor = InputBox("Informe valor para exclusão", "Atenção")


If Not VBA.IsNumeric(valor) Then

MsgBox "Um valor não numérico foi informado" & _
VBA.Chr(13) & "Processo abortado"
GoTo finaliza
End If


valor = valor + 0

    For i = 1 To lastRowData
    
        If w.Cells(i, 2) > valor Then
        
            w.Cells(i, 2) = ""
            counter = counter + 1
        End If
    
    Next i

    If counter > 0 Then
    
        w.Range("b1:b" & lastRowData).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
    
finaliza:

Set w = Nothing

End Sub
Avatar do usuário
Por gfranco
Avatar
#33530
Nota:
A planilha ativa (no momento da execução do código acima) deve ser a planilha alvo da exclusão.
Avatar do usuário
Por gfranco
Avatar
#33547
Sugiro, então, postar um exemplo (idêntico ao original) para uma melhor escrita.
Avatar do usuário
Por gfranco
Avatar
#33550
Para esse último modelo postado, eu utilizaria o seguinte código:
Código: Selecionar todos
Sub excluir()

Dim w As Worksheet
Dim lastRowData As Long
Dim i As Long
Dim valor As Variant
Dim counter As Long



Set w = ActiveSheet
counter = 0
lastRowData = w.Cells(w.Rows.Count, 2).End(3).Row

If lastRowData < 2 Then GoTo finaliza

valor = 8000000000



    For i = 2 To lastRowData
   
        If w.Cells(i, 2) > valor Then
       
            w.Cells(i, 2) = ""
            counter = counter + 1
        End If
   
    Next i

    If counter > 0 Then
   
        w.Range("b1:b" & lastRowData).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
   
finaliza:

Set w = Nothing

End Sub

#33551
Código: Selecionar todos
Sub ExcluiLinhas()
 With ActiveSheet
  .AutoFilterMode = False
  .Range("A1:D" & Cells(Rows.Count, 1).End(3).Row).AutoFilter 2, ">8000000000"
  .Range("A2:D" & Cells(Rows.Count, 1).End(3).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
  .AutoFilterMode = False
 End With
End Sub
#33552
gfranco escreveu:Para esse último modelo postado, eu utilizaria o seguinte código:
Código: Selecionar todos
Sub excluir()

Dim w As Worksheet
Dim lastRowData As Long
Dim i As Long
Dim valor As Variant
Dim counter As Long



Set w = ActiveSheet
counter = 0
lastRowData = w.Cells(w.Rows.Count, 2).End(3).Row

If lastRowData < 2 Then GoTo finaliza

valor = 8000000000



    For i = 2 To lastRowData
   
        If w.Cells(i, 2) > valor Then
       
            w.Cells(i, 2) = ""
            counter = counter + 1
        End If
   
    Next i

    If counter > 0 Then
   
        w.Range("b1:b" & lastRowData).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
   
finaliza:

Set w = Nothing

End Sub

Agora ficou perfeito, muito obrigado gfranco!
#33553
osvaldomp escreveu:
Código: Selecionar todos
Sub ExcluiLinhas()
 With ActiveSheet
  .AutoFilterMode = False
  .Range("A1:D" & Cells(Rows.Count, 1).End(3).Row).AutoFilter 2, ">8000000000"
  .Range("A2:D" & Cells(Rows.Count, 1).End(3).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
  .AutoFilterMode = False
 End With
End Sub
Também deu certo desta forma, muito obrigado osvaldomp!
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