Ciao Andrea,
Buongiorno Norman. Come sempre grazie mille per la disponibilità.
Ho provato la tua macro e ho questi problemi:
- se Cartel2 non è aperto il tutto non funziona (avrei bisogno che lavorasse anche se il file 2 è chiuso);
- in Cartel2 cancella correttamente la riga ma in Cartel1 no.
Poi ho dimenticato io di descriverti la macro. La riporto di seguito:
- vai nel foglio foglio 1 della Cartella 1;
- cerca valore COMPLETATO nella colonna H;
- se trovi COMPLETATO cancella intera riga;
- copia intera riga in foglio 2 della Cartella 1;
- vai nel foglio 1 della Cartella 2;
- cerca valore COMPLETATO nella colonna H;
- se trovi COMPLETATO cancella intera riga;
- copia intera riga in foglio 2 della Cartella 2;
Prova qualcosa del genere:
'========>>
Option Explicit
'-------->>
Public Sub SpostaTaskCompletati()
Dim WB1 As Workbook, WB2 As Workbook
Dim SH1 As Worksheet, SH2 As Worksheet
Dim destSH1 As Worksheet, destSH2 As Worksheet
Dim Rng1 As Range, Rng2 As Range
Dim destRng As Range, destRng2 As Range
Dim rngDelete As Range, rngDelete2 As Range
Dim rCell As Range
Dim UR As Long, UR2 As Long
Const sCartella1 As String = **"Andrea20210616.xlsm" '<<=== Modifica**
Const sCartella2 As String = **"Andrea20210616.xlsx" '<<=== Modifica**
Const sFoglio1 As String = **"DASHBOARD" '<<=== Modifica**
Const sFoglio2 As String = **"DASHBOARD2" '<<=== Modifica**
Const sFoglio\_Destinazione As String = **"Foglio2" '<<=== Modifica**
Const sFoglio2\_Destinazione As String = **"Foglio2" '<<=== Modifica**
Const sParola As String = **"COMPLETATO"**
Set WB1 = Workbooks(sCartella1)
If IsWorkbookOpen(sCartella2) Then
Set WB2 = Workbooks(sCartella2)
Else
Set WB2 = Workbooks.Open(WB1.Path & Application.PathSeparator & sCartella2)
End If
Set SH1 = WB1.Sheets(sFoglio1)
Set SH2 = WB2.Sheets(sFoglio2)
With SH1
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set Rng1 = .Range("H1:H" & UR)
End With
With SH2
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set Rng2 = .Range("H1:H" & UR)
End With
For Each rCell In Rng1.Cells
With rCell
If UCase(.Value) = sParola Then
If rngDelete Is Nothing Then
Set rngDelete = rCell
Else
Set rngDelete = Union(rngDelete, rCell)
End If
End If
End With
Next rCell
For Each rCell In Rng2.Cells
With rCell
If UCase(.Value) = sParola Then
If rngDelete2 Is Nothing Then
Set rngDelete2 = rCell
Else
Set rngDelete2 = Union(rngDelete2, rCell)
End If
End If
End With
Next rCell
If Not rngDelete Is Nothing Then
With WB1.Sheets("Foglio2")
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set destRng = .Range("A" & UR + 1)
End With
With rngDelete.EntireRow
.Copy Destination:=destRng
.Delete
End With
End If
If Not rngDelete2 Is Nothing Then
With WB2.Sheets("Foglio2")
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set destRng2 = .Range("A" & UR + 1)
End With
With rngDelete2.EntireRow
.Copy Destination:=destRng2
.Delete
End With
End If
End Sub
'-------->>
Public Function IsWorkbookOpen(WBName As String) As Boolean
On Error Resume Next
IsWorkbookOpen = CBool(Len(Excel.Application.Workbooks(WBName).Name))
End Function
'<<========
Potresti scaricare i miei due file di prova Qui
===
Regards,
Norman
