Condividi:        

[EXCEL] Eliminare i fogli

Vuoi potenziare i tuoi documenti Word? Non sai come si fa una macro in Excel? Devi creare una presentazione in PowerPoint?
Oppure sei passato a OpenOffice e non sei sicuro di come lavorare al meglio?

Moderatori: Anthony47, Flash30005

[EXCEL] Eliminare i fogli

Postdi thunder78 » 18/11/07 18:22

Ciao a tutti

Questito: Come faccio ad eliminare dei fogli di lavoro che non mi servono?
Per esempio ho alcuni (foglio1, foglio2, foglio3,ecc.) che devo far sparire dalla cartella di lavoro excel.
Servirebbe quindi una macro che ricerca sulla cartella excel dei fogli con questi nomi e li togliesse automaticamente. Inoltre, in mancanza di uno di questi fogli, vorrei che la macro lo ricercasse ugualmente e che non trovandolo passasse all'eliminazione dei rimanenti fogli.

ES. Foglio1, Foglio3 (manca il foglio 2). La macro lo ricerca lo stesso per eliminarlo, e continua ad eliminare i fogli rimanenti.

Come fare?
Grazie. Ciao. 8)
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Sponsor
 

Postdi Anthony47 » 19/11/07 01:17

Se il tuo bisogno è "eliminare alcuni fogli", prova questa macro:
Codice: Seleziona tutto
Sub DelFoglioN()
For I = ActiveWorkbook.Worksheets.Count To 1 Step -1
Sheets(I).Select
If Left(Sheets(I).Name, 6) = "Foglio" Then Sheets(I).Delete
Next I
End Sub


Usala con accortezza, perche’ essa passa tutti i fogli del file e cancella tutti quelli che hanno il nome che comincia per “Foglio”; l’ istruzione Sheets(I).Select e’ messa per dare maggiore evidenza di che cosa si va a cancellare.
Come sempre, prima di vedere gli effetti 2 copie di backup.

Ciao.
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Postdi thunder78 » 19/11/07 11:47

Ok funziona. Solamente mi serviva effettuare un check su molti fogli, quindi sull'istruzione

Codice: Seleziona tutto
If Left(Sheets(I).Name, 6) = "Foglio" Then Sheets(I).Delete


Ho sostituito il valore 6 con 50.

Devo dire che per il momento non ho avuto problemi.

Grazie mille

Ciao.
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi thunder78 » 19/11/07 12:12

Scusa rettifico.
Premetto che ho associato questa macro a un pulsante
Ho inserito questo codice e l'ho ripetuto più volte per diversi fogli in questo modo:

Codice: Seleziona tutto
For I = ActiveWorkbook.Worksheets.Count To 1 Step -1
Sheets(I).Select
If Left(Sheets(I).Name, 50) = "M.Palocco" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "C.so Italia" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "C.Colombo" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "C.so Italia" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Estensi" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "O.Pacifico" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Oriolo" Then Sheets(I).Delete


Ma la mcro blocca la sua esecuzione ogni volta su una riga diversa
C'è qualcosa di sbagliato?

Grazie.
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi Anthony47 » 19/11/07 13:22

Cioe' la macro va in errore? quale messaggio di errore? se vai in Debug, quale riga e' evidenziata (un paio di casi)? il foglio attivo al momento dell' errore e' uno di quelli che dovrebbe essere cancellato?

Ciao.
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Postdi thunder78 » 19/11/07 14:37

Il foglio attivo non è uno di quelli che dovrebbero essere cancellati perchè la macro è associata ad un pulsante che si trova su un altro foglio.

La macro si interrompe su C.so Italia oppure O.Pacifico, anche se il foglio con quel nominativo non è presente.

Come faccio a superare il problema?

Grazie.
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi Anthony47 » 19/11/07 15:26

Quindi la macro va in errore? quale messaggio di errore?

E, in debug, e' evidenziata la parte sx della riga (If) o la parte destra (Then)?

Puoi anche postare l' intera macro?

Ciao.
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Postdi thunder78 » 19/11/07 15:36

Codice: Seleziona tutto
Sub ELIMINA()

' ELIMINA Macro
' Macro registrata il 19/11/2007 da m.bono
'

For I = ActiveWorkbook.Worksheets.Count To 1 Step -1
Sheets(I).Select
If Left(Sheets(I).Name, 50) = "M.Palocco" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "C.so Italia" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "C.Colombo" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Estensi" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "O.Pacifico" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Oriolo" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "P.Medici" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Pontina Pomezia" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "S.Palomba Agrostemmi" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Saliceti" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Faustiniana-Tiburtina" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Francisci-T.Rossa" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Vignaccia" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Val Cannuta" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Altre sedi" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Totale" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Altre Classi" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "GOLD" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "SILVER" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "BRONZE" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "STANDARD" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Altre Giacenze" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Hardware" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Da Pianificare" Then Sheets(I).Delete
If Left(Sheets(I).Name, 50) = "Pianificati" Then Sheets(I).Delete
Next I
End Sub


Questa è la macro. Di solito è la parte sinistra che va in debug.
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi Anthony47 » 19/11/07 16:29

"quale messaggio di errore?"
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Postdi thunder78 » 19/11/07 17:09

nessuno va in debug e basta
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi thunder78 » 19/11/07 21:06

Allora il messaggio è il seguente

Errore di Run time '9':
Indice non incluso nell'intervallo

la parte dell'istruzione che viene evidenziata in giallo è la seguente:

Codice: Seleziona tutto
If Left(Sheets(I).Name, 50) = "Oriolo" Then


Spero di aver descritto chiaramente l'anomalia

Grazie.
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17

Postdi Anthony47 » 19/11/07 22:01

Ok, il messaggio spiega tutto.
Il foglio(I) si chiamava "O.Pacifico" ed e' stato eliminato nella riga precedente...
Modifica come segue:

Codice: Seleziona tutto
For I = ActiveWorkbook.Worksheets.Count To 1 Step -1
Sheets(I).Select
SName = Sheets(I).name
If SName = "M.Palocco" Then Sheets(I).Delete
If SName  = "C.so Italia" Then Sheets(I).Delete
If SName  = "C.Colombo" Then Sheets(I).Delete
'
'etc etc
'
If SName  = "Pianificati" Then Sheets(I).Delete
Next I
End Sub


Ho tolto anche il Left, che aveva senso nella prima interpretazione (eliminare Foglio1, Foglio3, etc cioe' i fogli che avevano una stessa radice), perche' mi pare che devi lavorare sul nome completo del foglio.

Ciao, fai sapere.
Avatar utente
Anthony47
Moderatore
 
Post: 19196
Iscritto il: 21/03/06 16:03
Località: Ivrea

Postdi thunder78 » 19/11/07 22:34

Come sempre mi hai dato un validissimo aiuto.

Grazie!
thunder78
Utente Junior
 
Post: 45
Iscritto il: 30/04/07 22:17


Torna a Applicazioni Office Windows


Topic correlati a "[EXCEL] Eliminare i fogli":


Chi c’è in linea

Visitano il forum: systemcrack e 44 ospiti