Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Andrea,
Si devo sbloccare i fogli TASK e anche i fogli ARCHIVIO TASK COMPLETATI di entrambe le cartelle.
La macro per operare apre il secondo file, può anche chiuderlo?
Allora, sostituisci il codice con 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 = **"TT SEGRETERIA.xlsm"**
Const sCartella2 As String = **"TT DIVISIONI.xlsm"**
Const sFoglio1 As String = **"TASK"**
Const sFoglio2 As String = **"TASK"**
Const sFoglio\_Destinazione As String = **"ARCHIVIO TASK COMPLETATI"**
Const sFoglio2\_Destinazione As String = **"ARCHIVIO TASK COMPLETATI"**
Const sParola As String = **"COMPLETATO"**
Const sPassword1 As String = **"Pippo" '<<=== Modifica**
Const sPassword2 As String = **"Pluto" '<<=== Modifica**
Set WB1 = Workbooks(sCartella1)
On Error GoTo XIT
Application.ScreenUpdating = False
If IsWorkbookOpen(sCartella2) Then
Set WB2 = Workbooks(sCartella2)
Else
Set WB2 = Workbooks.Open(WB1.Path & Application.PathSeparator & sCartella2)
End If
With WB1
Set SH1 = .Sheets(sFoglio1)
Set destSH1 = .Sheets(sFoglio\_Destinazione)
End With
With WB2
Set SH2 = .Sheets(sFoglio2)
Set destSH2 = .Sheets(sFoglio2\_Destinazione)
End With
With SH1
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set Rng1 = .Range("H1:H" & UR)
.Unprotect Password:=sPassword1
End With
With SH2
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set Rng2 = .Range("H1:H" & UR)
.Unprotect Password:=sPassword2
End With
destSH1.Unprotect Password:=sPassword1
destSH2.Unprotect Password:=sPassword2
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(sFoglio\_Destinazione)
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set destRng = .Range("A" & UR + 1)
End With
Intersect(SH1.Columns("A:M"), rngDelete.EntireRow).Copy Destination:=destRng
rngDelete.EntireRow.Delete
End If
If Not rngDelete2 Is Nothing Then
With WB2.Sheets(sFoglio2\_Destinazione)
UR = .Range("H" & .Rows.Count).End(xlUp).Row
Set destRng2 = .Range("A" & UR + 1)
End With
Intersect(SH2.Columns("A:M"), rngDelete2.EntireRow).Copy Destination:=destRng2
rngDelete2.EntireRow.Delete
End If
SH1.Protect Password:=sPassword1
SH2.Protect Password:=sPassword2
destSH1.Protect Password:=sPassword1
destSH2.Protect Password:=sPassword2
WB1.Save
WB2.Close SaveChanges:=True
Call MsgBox(Prompt:="Fatto", \_
Buttons:=vbInformation, \_
Title:="REPORT")
XIT:
Application.ScreenUpdating = True
End Sub
'-------->>
Public Function IsWorkbookOpen(WBName As String) As Boolean
On Error Resume Next
IsWorkbookOpen = CBool(Len(Excel.Application.Workbooks(WBName).Name))
End Function
'<<========
===
Regards,
Norman