MACRO VBA EXCEL

Anonimo
2021-06-16T16:18:23+00:00

Buonasera a tutti,

ho bisogno di un consiglio di qualche esperto (io purtroppo sono completamente digiuno di VBA). Questa la macro che ho scritto è questa

Sub SpostaTaskCompletati()

  Dim xRg As Range

  Dim T As Long

  Dim R As Long

T = Worksheets("DASHBOARD").UsedRange.Rows.Count

  Set xRg = Worksheets("DASHBOARD").Range("H1:H" & T)

  On Error Resume Next

  Application.ScreenUpdating = False

  For R = 1 To xRg.Count

     If CStr(xRg(R).Value) = "COMPLETATO" Then

     xRg(R).EntireRow.Delete

        If CStr(xRg(R).Value) = "COMPLETATO" Then

        R = R - 1

        End If

     End If

Next

Application.ScreenUpdating = True

End Sub

La macro funziona correttamente però avrei bisogno di implementarla facendo in modo che si colleghi ad una nuova cartella di lavoro. In pratica la macro dovrebbe poter fare questo (ovviamente io l'avvierei dalla cartella 1):

  1. vai nel foglio DASHBOARD della Cartella 1;
  2. cerca valore COMPLETATO nella colonna H;
  3. se trovi COMPLETATO cancella intera riga;
  4. vai nel foglio DASHBOARD2 della Cartella 2;
  5. cerca valore COMPLETATO nella colonna H;
  6. se trovi COMPLETATO cancella intera riga;

E' fattibile?

Grazie in anticipo.

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2021-06-18T19:50:45+00:00

Ciao Andrea,

Buonasera Norman. Funziona  tutto.

Grazie mille  soprattutto per  la  pazienza 😉.

Mi fa piacere che tu abbia risolto il problema e ti ringrazio per il cortese riscontro.

Per chiudere questo thread, e per aiutare altri con un simile problema, ti chiederei gentilmente di contrassegnare il codice come Risposta.

Alla prossima.

===

Regards,

Norman

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2021-06-18T09:13:01+00:00

Ciao Andrea,

la formula funziona perfettamente. Grazie mille!!!! Provandola però mi sono accorto di dover fare una modifica. Quando la macro va ad incollare nel foglio 2 deve incollare solo le colonne da A ad M. E' Possibile?

Certo, prova la seguente modifica del codice:

'========>>

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 

    Intersect(SH1.Columns("A:M"), rngDelete.EntireRow).Copy Destination:=destRng 

    rngDelete.EntireRow.Delete 

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 

    Intersect(SH2.Columns("A:M"), rngDelete2.EntireRow).Copy Destination:=destRng2 

    rngDelete2.EntireRow.Delete 

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

'<<========

===

Regards,

Norman

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento

16 risposte aggiuntive

Ordina per: Meno recente
  1. Anonimo
    2021-06-18T19:05:36+00:00

    Buonasera Norman. Funziona  tutto.

    Grazie mille  soprattutto per  la  pazienza 😉.

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Eliminata

    Questa risposta è stata eliminata a causa di una violazione del codice di comportamento. La risposta è stata segnalata manualmente o identificata tramite il rilevamento automatizzato prima dell'esecuzione dell'azione. Per ulteriori informazioni, fai riferimento al codice di comportamento.


    I commenti sono stati disattivati. Ulteriori informazioni

  3. Anonimo
    2021-06-21T11:05:29+00:00

    Buongiorno Norman,

    avrei nuovamente bisogno del tuo aiuto. La famosa macro funziona perfettamente finché non proteggo i fogli. Avrei bisogno che i fogli siano protetti per evitare che accidentalmente le formule possano essere eliminate. C'è un modo per dire alla macro togli la protezione, esegui, inserisci nuovamente la protezione?

    La macro per funzionare apre una seconda cartella di lavoro , può anche richiuderla?

    Grazie

    '========>>

    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"

    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("N" & .Rows.Count).End(xlUp).Row

    Set Rng1 = .Range("N1:N" & UR)

    End With

    With SH2

    UR = .Range("J" & .Rows.Count).End(xlUp).Row

    Set Rng2 = .Range("J1:J" & 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("ARCHIVIO TASK COMPLETATI")

    UR = .Range("N" & .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 WB1.Sheets("ARCHIVIO TASK COMPLETATI")

    UR = .Range("N" & .Rows.Count).End(xlUp).Row

    Set destRng2 = .Range("N" & UR + 1)

    End With

    Intersect(SH2.Columns("J:T"), rngDelete2.EntireRow).Copy Destination:=destRng2

    rngDelete2.EntireRow.Delete

    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

    '<<========

    La risposta è stata utile?

    0 commenti Nessun commento