Ciao ouardi03,
ho un problema che per le mie competenze di excel è insormontabile.
Dovrei eliminare TUTTI i valori dalla colonna A, se almeno uno di essi ha campo vuoto in B.
Esempio:
| andrea |
|
| mario |
scarpa |
| paolo |
mela |
| paolo |
|
| luca |
bicicletta |
| luca |
cappello |
In questo caso voglio eliminare sia la prima riga (andrea) che ha la colonna B vuota, che TUTTE le righe di paolo (sia la 3 che la 4), in quanto ha il campo B vuoto alla riga 4.
Il file originale ha 7500 righe e alcuni valori della colonna A che si ripetono anche 20-30 volte.
- Alt+F11 per aprire l'editor di VBA
- Alt+IMper inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim oDic As Object
Dim arrIn As Variant, arrOut() As Variant
Dim arrKeys As Variant
Dim sStr As String, aStr As String
Dim sMsg As String, sTitle As String, iButtons As Long
Dim i As Long, j As Long, k As Long
Dim LRow As Long, iCols As Long
Dim iCtr As Long
Dim UB As Long, UB2 As Long
Dim CalcMode As Long
Const sFoglio As String = "Foglio1" '<<=== Modifica
Const sColonne As String = "A:D" '<<=== Modifica
Const iRigaIntestazioni As Long = 3 '<<=== Modifica
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
With SH
LRow = LastRow(SH, .Columns("A:A"))
iCols = .Range(sColonne).Columns.Count
Set Rng = .Range("A" & iRigaIntestazioni + 1).Resize(LRow - iRigaIntestazioni, iCols)
End With
arrIn = Rng.Value
UB = UBound(arrIn)
UB2 = UBound(arrIn, 2)
ReDim arrOut(1 To UB, 1 To UB2)
Set oDic = CreateObject("Scripting.Dictionary")
oDic.CompareMode = vbTextCompare
With oDic
For i = 1 To UB
sStr = arrIn(i, 2)
If sStr = vbNullString Then
If Not .exists(sStr) Then
.Add Key:=arrIn(i, 1), Item:=Nothing
End If
End If
Next i
If .Count = 0 Then
sMsg = "Nessuna cella vuota trovata nella seconda colonna!"
sTitle = "Nessuna riga cancellata!"
iButtons = vbCritical
GoTo XIT
End If
arrKeys = .keys
For i = 1 To UB
aStr = arrIn(i, 1)
If Not .exists(aStr) Then
iCtr = iCtr + 1
For j = 1 To UB2
arrOut(iCtr, j) = arrIn(i, j)
Next j
End If
Next i
End With
If CBool(iCtr) Then
On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Rng
.ClearContents
.Resize(iCtr).Value = arrOut
End With
End If
sMsg = UB - iCtr & " Righe cancellate!"
sTitle = "REPORT"
iButtons = vbInformation
XIT:
Call MsgBox( _
Prompt:=sMsg, _
Buttons:=iButtons, _
Title:=sTitle)
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
End Function
'<<=========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel
- Salva il file con l’estensione xlsm
- Alt+F8 per aprire la finestra di gestione delle macro
- Seleziona
Tester | Esegui
===
Regards,
Norman
