Ho una tabella in Excel denominata tblFruits
con 10 colonne e voglio eliminare tutte le righe in cui la colonna Fruit
contiene Apple
. Come posso fare questo?Come eliminare righe in un elenco di Excel Oggetto basato su criteri che utilizzano VBA?
6
A
risposta
7
le seguenti opere: sub
Private Sub deleteTableRowsBasedOnCriteria(tbl As ListObject, columnName As String, criteria As String)
Dim x As Long, lastrow As Long, lr As ListRow
lastrow = tbl.ListRows.Count
For x = lastrow To 1 Step -1
Set lr = tbl.ListRows(x)
If Intersect(lr.Range, tbl.ListColumns(columnName).Range).Value = criteria Then
'lr.Range.Select
lr.Delete
End If
Next x
End Sub
Il sub possono essere eseguiti in questo modo:
Dim tbl As ListObject
Set tbl = ThisWorkbook.Worksheets("Sheet1").ListObjects("tblFruits")
Call deleteTableRowsBasedOnCriteria(tbl, "Fruit", "Apple")
2
Bene, sembra che la proprietà .listrows è limitata a uno una lista riga o tutta la lista righe .
modo più semplice che ho trovato per aggirare questo è stato di:
Impostazione di una colonna con una formula che fa notare a me tutte le righe vorrei eliminare (potrebbe non essere necessario la formula, in questo caso)
Ordinamento ListObject su tale colonna specifica (preferibilmente facendo sì che il mio valore da cancellare sarebbe alla fine smistamento)
Recupero del indirizzo della serie di listrows I del del ete
Infine, cancellando l'intervallo recuperato, spostando le celle verso l'alto.
In questo specifico pezzo di codice:
Sub Delete_LO_Rows
Const ctRemove as string = "Remove" 'value to be removed
Dim myLO as listobject, r as long
Dim N as integer 'number of the listcolumn with the formula
Set myLo = Sheet1.ListObjects("Table1") 'listobject goes here
With myLO
With .Sort
With .SortFields
.Clear
.Add Key:=.HeaderRowRange(myLO.ListColumns(N)), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
On Error GoTo NoRemoveFound
r = Application.WorksheetFunction.Match(ctRemove, .ListColumns(.ListColumns.Count).DataBodyRange, 0)
Range(.parent.name & "!" & .DataBodyRange(r, 1).Address & ":" & .DataBodyRange(.ListRows.Count, .ListColumns.Count).Address).Delete xlShiftUp
'Added the .parent.name to make sure the address is on the correct sure, but it will fail if there are any spaces or characters on the sheet name that will make it need a pair of '.
'The error is just to skip these two lines in case the match returns an error. There's likely a better/cleaner way to do that.
NoRemoveFound:
End With
End sub
Speranza che aiuta ...
Problemi correlati
- 1. Come eliminare righe in Excel in base a criteri che utilizzano VBA?
- 2. eliminazione di righe vuote in Excel che utilizzano VBA
- 3. Trim celle che utilizzano VBA in Excel
- 4. VBA: conteggio delle righe in una tabella (oggetto elenco)
- 5. come ottenere un oggetto "Cell" in excel VBA
- 6. Come ottenere l'intervallo delle righe visibili dopo l'applicazione di un filtro avanzato in Excel (VBA)
- 7. Elimina tutte le righe duplicate Excel vba
- 8. Excel VBA - Rimuovi righe vuote nella tabella
- 9. appiattisci elenco/oggetto basato su record in dataframe
- 10. VBA Excel: errore di compilazione: oggetto richiesto?
- 11. oggetto di errore richiesto Excel VBA
- 12. VBA Excel: come eseguire il cast di un oggetto controllo generico in un oggetto ComboBox?
- 13. CopiaOrigin su Insert in Excel VBA
- 14. Funzione VBA di Excel che restituisce un array
- 15. Come scrivere una raccolta VBA su un foglio Excel
- 16. Come eliminare righe da un DataFrame panda basato su un'espressione condizionale
- 17. Memoria mancanza di Excel VBA
- 18. Come visualizzare parte di Excel su VBA Form
- 19. C# Elenco di ordinamento basato su un altro elenco
- 20. Bug in VBA DateValue() in VBA Excel?
- 21. Python che divide un elenco basato su una parola delimitatore
- 22. In loop su tutte le righe in una colonna di tabella, Excel-VBA
- 23. Come eliminare le righe da un data.frame, in base a un elenco esterno, utilizzando R?
- 24. Come eliminare le righe con openpyxl?
- 25. matrice in Excel VBA
- 26. Excel VBA equivalente di un vettore
- 27. Excel vba - xlDown
- 28. Sottoinsieme basato sull'elenco di stringhe che utilizzano grepl()?
- 29. Miscelazione del design basato su criteri con CRTP in C++
- 30. Come ottenere un oggetto da un elenco basato su IEqualityComparer <T>
duplicato: http://stackoverflow.com/questions/7648655/how-to-delete- righe-in-basati su Excel-on-criteri-using-vba –