billsmith
04-08-2008, 11:42
Buongiorno a tutti, :D
premetto che sono nuovo del forum e poco esperto di Vba for Excel .
Il mio problema è il seguente :cry: :
1) Ho un database su un file Excel (2007)
2) Alcuni campi di questo database contengono Date
3) Voglio poter filtrare un campo Data mediante una Macro Vba (ad esempio, poter filtrare tutte le date antecedenti il 30/06/2008)
4) Ho creato (con non poche difficoltà) una Macro Vba che nel momento in cui gli dico di filtrare tutte le date antecedenti il 30/06/2008 in realtà non filtra niente :confused:
Ovvero filtra solo le celle che contengono il segno "/" (slash)
5) Se però vado "manualmente" sul file Excel per filtrare tali date, nel primo campo del criterio di filtro trovo effettivamente la data "30/06/2008" e il criterio che la macro imposta è di estrarre tutto ciò che è minore o uguale ("<=").
Per maggiore chiarezza riporto sotto le righe della macro Vba che non mi filtrano le date
'----- ANCORA DA CAPIRE COME FARE FILTRARE LA DATA -------------
Application.Goto Reference:="First_Cell_Validità_fino_al"
ActiveSheet.Range("Database").AutoFilter Field:=65, _
Criteria1:="<=" & Trimestre_In_Corso, Operator:=xlAnd
'-----------------------------------------------------------------------------------
La variabile "Trimestre_In_Corso" l'ho definita poche righe prima così ...
'--------- Identifica il Trimestre in Corso ---------------------
If Numero_Trimestre = 1 Then Trimestre_In_Corso = "31/03/" & Current_Year _
Else If Numero_Trimestre = 2 Then Trimestre_In_Corso = "30/06/" & Current_Year _
Else If Numero_Trimestre = 3 Then Trimestre_In_Corso = "30/09/" & Current_Year _
Else If Numero_Trimestre = 4 Then Trimestre_In_Corso = "31/12/" & Current_Year
'----------------------------------------------------------------
Per precisazione "Current_Year" è una variabile Data che mi dà l'anno corrente.
Secondo Voi, come posso fare in modo che la macro restituisca la variabile "Trimestre_In_Corso" tale da diventare effettivamente un valore di filtro per le date? ... in effetti sembra che non la interpreti come una data ...
Ho sicuramente mancato qualcosa :muro: nel definire la variabile "Trimestre_In_Corso" affinchè nel comando Vba ...
Criteria1:="<=" & Trimestre_In_Corso
possa funzionare da filtro.
Grazie anticipatamente a chi vorrà aiutarmi :ave:
Billsmith
---------------------------------------------------------------------------------------------------------
Ho pensato di mettere TUTTA la macro che ho scritto ... forse può essere di aiuto a chi mi volesse dare una mano
--------------------------------------------------------------------------------------------------------
Sub Certificati_Scaduti()
'----------------------------------------------------------------
' Filtro Certificati Scaduti Macro
' Filtra la colonna "Validità fino al" per la visualizzazione
' dei Certificati Scaduti rispetto al Trimestre scelto
'----------------------------------------------------------------
'
Application.ScreenUpdating = False 'schermo falso
Application.Calculation = xlManual 'calcolo in manuale
'
'----- Leva filtro e scopre colonne -----------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Range("Zona_Nomi_Campi").Select
Selection.EntireColumn.Hidden = False
Range("Zona_Nomi_Campi").Select
'
'----- Leva il Filtro -------------------------------------------
If ActiveSheet.AutoFilterMode Then
isOn = "Attivata"
Selection.AutoFilter 'quindi la disattiva
GoTo Scopre_Colonne
Else
isOn = "Disattivata" 'non fa nulla perché è già disattivata
End If
'
'----- Scopre Tutte le Colonne ----------------------------------
Scopre_Colonne:
Rows("3:3").Select
Selection.EntireColumn.Hidden = False
Application.Goto Reference:="First_Cell_Fornitore"
'----- Modalità Scelta Trimestre --------------------------------
Modalità_Scelta_Trimestre:
Dim Message_Scelta_Trim, Titolo_Scelta_Trim, _
Default_Scelta_Trim, Valore_Scelta_Trimestre
'
Message_Scelta_Trim _
= "Vuoi che il programma" & Chr(13) _
& "identifichi il Trimestre" & Chr(13) _
& "1) in automatico oppure " & Chr(13) _
& "2) lo vuoi scegliere tu?" 'imposta il messaggio
Titolo_Scelta_Trim = "Scelta Trimestre" 'imposta il titolo
Default_Scelta_Trim = "1" 'imposta il valore predefinito
'
'-- Visualizza il messaggio, il titolo e il valore predefinito --
Valore_Scelta_Trimestre = InputBox(Message_Scelta_Trim, _
Titolo_Scelta_Trim, Default_Scelta_Trim)
'
'--------- Verifica il Valore della Scelta Trimestre ------------
If Valore_Scelta_Trimestre = 1 _
Then GoTo Trimestre_In_Automatico _
Else If Valore_Scelta_Trimestre = 2 _
Then GoTo Trimestre_In_Manuale _
Else GoTo Modalità_Scelta_Trimestre
'
'----- Scelta in Automatico del Trimestre -----------------------
Trimestre_In_Automatico:
Dim Numero_Trimestre As Variant
Dim Data_Odierna_Automatica 'Dichiara le variabili.
Data_Odierna_Automatica = Date
Numero_Trimestre = (DatePart("q", Data_Odierna_Automatica) - 1)
GoTo Gestione_Date: 'Salta alla Gestione Date
'
'----- Scelta in Manuale del Trimestre --------------------------
Trimestre_In_Manuale:
Dim Msg_Numero_Trimestre, Style_Numero_Trimestre, _
Title_Numero_Trimestre, Response_Numero_Trimestre
Style_Numero_Trimestre = vbOKOnly 'Definisce i pulsanti
Title_Numero_Trimestre = "Scegli il Trimestre" 'Definisce il titolo
'----------------------------------------------------------------
Chiedi_Numero_Trimestre:
Numero_Trimestre = InputBox("Inserire il N° del Trimestre" & Chr(13) _
& "che si vuole filtrare (1-4)")
If Numero_Trimestre = 1 _
Or Numero_Trimestre = 2 _
Or Numero_Trimestre = 3 _
Or Numero_Trimestre = 4 _
Then GoTo Gestione_Date _
Else _
'------------- Visualizza il messaggio --------------------------
Messaggio_Errore = ("Errore! Devi inserire un" & Chr(13) _
& "Numero compreso tra 1 e 4 !!" & Chr(13) _
& "Ripeti l'inserimento")
MsgBox Messaggio_Errore, vbOKOnly
GoTo Chiedi_Numero_Trimestre
'
'----------------------------------------------------------------
'--------- Gestione Date di Fine Trimestre ----------------------
Gestione_Date:
Dim Data_Odierna_Manuale, Current_Year, Trimestre_In_Corso
Data_Odierna_Manuale = Date
Current_Year = Format(Data_Odierna_Manuale, "yyyy")
'
'----------------------------------------------------------------
'--------- Identifica il Trimestre in Corso ---------------------
If Numero_Trimestre = 1 Then Trimestre_In_Corso = "31/03/" & Current_Year _
Else If Numero_Trimestre = 2 Then Trimestre_In_Corso = "30/06/" & Current_Year _
Else If Numero_Trimestre = 3 Then Trimestre_In_Corso = "30/09/" & Current_Year _
Else If Numero_Trimestre = 4 Then Trimestre_In_Corso = "31/12/" & Current_Year
'----------------------------------------------------------------
'----- Richiede lo Stabilimento Tower da Filtrare ---------------
Quale_Stabilimento_Filtrare:
Dim Stab_TWR As String
Dim Msg_Stab_TWR, Style_Stab_TWR, Title_Stab_TWR, Response_Stab_TWR
Style_Stab_TWR = vbOKOnly 'Definisce i pulsanti
Title_Stab_TWR = "Scelta Stabilimento Tower" 'Definisce il titolo
'----------------------------------------------------------------
Chiede_Nome_Stabilimento:
Stab_TWR = InputBox("Scegliere lo Stabilimento TOWER" & Chr(13) _
& "che si vuole filtrare tra i seguenti : " & Chr(13) _
& "NONE CASERTA MELFI", "NONE CASERTA MELFI")
'----------------------------------------------------------------
If Stab_TWR = "NONE" _
Or Stab_TWR = "CASERTA" _
Or Stab_TWR = "MELFI" _
Then GoTo End_Quale_Stabilimento_Filtrare _
Else _
'----------------------------------------------------------------
Msg_Stab_TWR = ("Devi scegliere fra uno dei 3" & Chr(13) _
& " stabilimenti : NONE, CASERTA, MELFI !!")
' ----------- Visualizza il messaggio ---------------------------
Response_Stab_TWR = MsgBox(Msg_Stab_TWR, Style_Stab_TWR, Title_Stab_TWR)
GoTo Chiede_Nome_Stabilimento
'----------------------------------------------------------------
End_Quale_Stabilimento_Filtrare:
'----------------------------------------------------------------
' -------- Leva il Filtro ---------------------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Range("Zona_Nomi_Campi").Select
Selection.EntireColumn.Hidden = False
' -------- Scopre tutte le Colonne ------------------------------
Rows("3:3").Select
Selection.EntireColumn.Hidden = False
Application.Goto Reference:="First_Cell_Fornitore"
'-------- Filtra il Trimestre -----------------------------------
Application.Goto Reference:="First_Cell_Trimestre"
ActiveSheet.Range("Database").AutoFilter Field:=14, _
Criteria1:=Numero_Trimestre
Application.Goto Reference:="First_Cell_Codice_Unico_MFG:" _
& "First_Cell_Percentuale_Assegnazione "
Selection.EntireColumn.Hidden = True
'-------- Filtra lo Stabilimento -------------------------------
Application.Goto Reference:="First_Cell_Fornitore"
ActiveSheet.Range("Database").AutoFilter Field:=2, _
Criteria1:=Stab_TWR
'-------- Filtra la Data in base al Trimestre In Corso ---------
'----- ANCORA DA CAPIRE COME FARE FILTRARE LA DATA -------------
Application.Goto Reference:="First_Cell_Validità_fino_al"
ActiveSheet.Range("Database").AutoFilter Field:=65, _
Criteria1:="<=" & Trimestre_In_Corso, Operator:=xlAnd
'
'-------- Poi inizia a nascondere le colonne inutili -----------
Application.Goto Reference:="First_Cell_Rapporto_Clientela:" _
& "First_Cell_Anno"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Codice_Unico_MFG:" _
& "First_Cell_VR_TWM"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_N__Stampa_Mappatura"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Trend"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Valutazione_Sistema:" _
& "First_Cell_Note_Osservazioni"
Selection.EntireColumn.Hidden = True
'---Torna alla First Cell Fornitore -----------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Application.Goto Reference:="First_Cell_Validità_fino_al"
Selection.EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub
premetto che sono nuovo del forum e poco esperto di Vba for Excel .
Il mio problema è il seguente :cry: :
1) Ho un database su un file Excel (2007)
2) Alcuni campi di questo database contengono Date
3) Voglio poter filtrare un campo Data mediante una Macro Vba (ad esempio, poter filtrare tutte le date antecedenti il 30/06/2008)
4) Ho creato (con non poche difficoltà) una Macro Vba che nel momento in cui gli dico di filtrare tutte le date antecedenti il 30/06/2008 in realtà non filtra niente :confused:
Ovvero filtra solo le celle che contengono il segno "/" (slash)
5) Se però vado "manualmente" sul file Excel per filtrare tali date, nel primo campo del criterio di filtro trovo effettivamente la data "30/06/2008" e il criterio che la macro imposta è di estrarre tutto ciò che è minore o uguale ("<=").
Per maggiore chiarezza riporto sotto le righe della macro Vba che non mi filtrano le date
'----- ANCORA DA CAPIRE COME FARE FILTRARE LA DATA -------------
Application.Goto Reference:="First_Cell_Validità_fino_al"
ActiveSheet.Range("Database").AutoFilter Field:=65, _
Criteria1:="<=" & Trimestre_In_Corso, Operator:=xlAnd
'-----------------------------------------------------------------------------------
La variabile "Trimestre_In_Corso" l'ho definita poche righe prima così ...
'--------- Identifica il Trimestre in Corso ---------------------
If Numero_Trimestre = 1 Then Trimestre_In_Corso = "31/03/" & Current_Year _
Else If Numero_Trimestre = 2 Then Trimestre_In_Corso = "30/06/" & Current_Year _
Else If Numero_Trimestre = 3 Then Trimestre_In_Corso = "30/09/" & Current_Year _
Else If Numero_Trimestre = 4 Then Trimestre_In_Corso = "31/12/" & Current_Year
'----------------------------------------------------------------
Per precisazione "Current_Year" è una variabile Data che mi dà l'anno corrente.
Secondo Voi, come posso fare in modo che la macro restituisca la variabile "Trimestre_In_Corso" tale da diventare effettivamente un valore di filtro per le date? ... in effetti sembra che non la interpreti come una data ...
Ho sicuramente mancato qualcosa :muro: nel definire la variabile "Trimestre_In_Corso" affinchè nel comando Vba ...
Criteria1:="<=" & Trimestre_In_Corso
possa funzionare da filtro.
Grazie anticipatamente a chi vorrà aiutarmi :ave:
Billsmith
---------------------------------------------------------------------------------------------------------
Ho pensato di mettere TUTTA la macro che ho scritto ... forse può essere di aiuto a chi mi volesse dare una mano
--------------------------------------------------------------------------------------------------------
Sub Certificati_Scaduti()
'----------------------------------------------------------------
' Filtro Certificati Scaduti Macro
' Filtra la colonna "Validità fino al" per la visualizzazione
' dei Certificati Scaduti rispetto al Trimestre scelto
'----------------------------------------------------------------
'
Application.ScreenUpdating = False 'schermo falso
Application.Calculation = xlManual 'calcolo in manuale
'
'----- Leva filtro e scopre colonne -----------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Range("Zona_Nomi_Campi").Select
Selection.EntireColumn.Hidden = False
Range("Zona_Nomi_Campi").Select
'
'----- Leva il Filtro -------------------------------------------
If ActiveSheet.AutoFilterMode Then
isOn = "Attivata"
Selection.AutoFilter 'quindi la disattiva
GoTo Scopre_Colonne
Else
isOn = "Disattivata" 'non fa nulla perché è già disattivata
End If
'
'----- Scopre Tutte le Colonne ----------------------------------
Scopre_Colonne:
Rows("3:3").Select
Selection.EntireColumn.Hidden = False
Application.Goto Reference:="First_Cell_Fornitore"
'----- Modalità Scelta Trimestre --------------------------------
Modalità_Scelta_Trimestre:
Dim Message_Scelta_Trim, Titolo_Scelta_Trim, _
Default_Scelta_Trim, Valore_Scelta_Trimestre
'
Message_Scelta_Trim _
= "Vuoi che il programma" & Chr(13) _
& "identifichi il Trimestre" & Chr(13) _
& "1) in automatico oppure " & Chr(13) _
& "2) lo vuoi scegliere tu?" 'imposta il messaggio
Titolo_Scelta_Trim = "Scelta Trimestre" 'imposta il titolo
Default_Scelta_Trim = "1" 'imposta il valore predefinito
'
'-- Visualizza il messaggio, il titolo e il valore predefinito --
Valore_Scelta_Trimestre = InputBox(Message_Scelta_Trim, _
Titolo_Scelta_Trim, Default_Scelta_Trim)
'
'--------- Verifica il Valore della Scelta Trimestre ------------
If Valore_Scelta_Trimestre = 1 _
Then GoTo Trimestre_In_Automatico _
Else If Valore_Scelta_Trimestre = 2 _
Then GoTo Trimestre_In_Manuale _
Else GoTo Modalità_Scelta_Trimestre
'
'----- Scelta in Automatico del Trimestre -----------------------
Trimestre_In_Automatico:
Dim Numero_Trimestre As Variant
Dim Data_Odierna_Automatica 'Dichiara le variabili.
Data_Odierna_Automatica = Date
Numero_Trimestre = (DatePart("q", Data_Odierna_Automatica) - 1)
GoTo Gestione_Date: 'Salta alla Gestione Date
'
'----- Scelta in Manuale del Trimestre --------------------------
Trimestre_In_Manuale:
Dim Msg_Numero_Trimestre, Style_Numero_Trimestre, _
Title_Numero_Trimestre, Response_Numero_Trimestre
Style_Numero_Trimestre = vbOKOnly 'Definisce i pulsanti
Title_Numero_Trimestre = "Scegli il Trimestre" 'Definisce il titolo
'----------------------------------------------------------------
Chiedi_Numero_Trimestre:
Numero_Trimestre = InputBox("Inserire il N° del Trimestre" & Chr(13) _
& "che si vuole filtrare (1-4)")
If Numero_Trimestre = 1 _
Or Numero_Trimestre = 2 _
Or Numero_Trimestre = 3 _
Or Numero_Trimestre = 4 _
Then GoTo Gestione_Date _
Else _
'------------- Visualizza il messaggio --------------------------
Messaggio_Errore = ("Errore! Devi inserire un" & Chr(13) _
& "Numero compreso tra 1 e 4 !!" & Chr(13) _
& "Ripeti l'inserimento")
MsgBox Messaggio_Errore, vbOKOnly
GoTo Chiedi_Numero_Trimestre
'
'----------------------------------------------------------------
'--------- Gestione Date di Fine Trimestre ----------------------
Gestione_Date:
Dim Data_Odierna_Manuale, Current_Year, Trimestre_In_Corso
Data_Odierna_Manuale = Date
Current_Year = Format(Data_Odierna_Manuale, "yyyy")
'
'----------------------------------------------------------------
'--------- Identifica il Trimestre in Corso ---------------------
If Numero_Trimestre = 1 Then Trimestre_In_Corso = "31/03/" & Current_Year _
Else If Numero_Trimestre = 2 Then Trimestre_In_Corso = "30/06/" & Current_Year _
Else If Numero_Trimestre = 3 Then Trimestre_In_Corso = "30/09/" & Current_Year _
Else If Numero_Trimestre = 4 Then Trimestre_In_Corso = "31/12/" & Current_Year
'----------------------------------------------------------------
'----- Richiede lo Stabilimento Tower da Filtrare ---------------
Quale_Stabilimento_Filtrare:
Dim Stab_TWR As String
Dim Msg_Stab_TWR, Style_Stab_TWR, Title_Stab_TWR, Response_Stab_TWR
Style_Stab_TWR = vbOKOnly 'Definisce i pulsanti
Title_Stab_TWR = "Scelta Stabilimento Tower" 'Definisce il titolo
'----------------------------------------------------------------
Chiede_Nome_Stabilimento:
Stab_TWR = InputBox("Scegliere lo Stabilimento TOWER" & Chr(13) _
& "che si vuole filtrare tra i seguenti : " & Chr(13) _
& "NONE CASERTA MELFI", "NONE CASERTA MELFI")
'----------------------------------------------------------------
If Stab_TWR = "NONE" _
Or Stab_TWR = "CASERTA" _
Or Stab_TWR = "MELFI" _
Then GoTo End_Quale_Stabilimento_Filtrare _
Else _
'----------------------------------------------------------------
Msg_Stab_TWR = ("Devi scegliere fra uno dei 3" & Chr(13) _
& " stabilimenti : NONE, CASERTA, MELFI !!")
' ----------- Visualizza il messaggio ---------------------------
Response_Stab_TWR = MsgBox(Msg_Stab_TWR, Style_Stab_TWR, Title_Stab_TWR)
GoTo Chiede_Nome_Stabilimento
'----------------------------------------------------------------
End_Quale_Stabilimento_Filtrare:
'----------------------------------------------------------------
' -------- Leva il Filtro ---------------------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Range("Zona_Nomi_Campi").Select
Selection.EntireColumn.Hidden = False
' -------- Scopre tutte le Colonne ------------------------------
Rows("3:3").Select
Selection.EntireColumn.Hidden = False
Application.Goto Reference:="First_Cell_Fornitore"
'-------- Filtra il Trimestre -----------------------------------
Application.Goto Reference:="First_Cell_Trimestre"
ActiveSheet.Range("Database").AutoFilter Field:=14, _
Criteria1:=Numero_Trimestre
Application.Goto Reference:="First_Cell_Codice_Unico_MFG:" _
& "First_Cell_Percentuale_Assegnazione "
Selection.EntireColumn.Hidden = True
'-------- Filtra lo Stabilimento -------------------------------
Application.Goto Reference:="First_Cell_Fornitore"
ActiveSheet.Range("Database").AutoFilter Field:=2, _
Criteria1:=Stab_TWR
'-------- Filtra la Data in base al Trimestre In Corso ---------
'----- ANCORA DA CAPIRE COME FARE FILTRARE LA DATA -------------
Application.Goto Reference:="First_Cell_Validità_fino_al"
ActiveSheet.Range("Database").AutoFilter Field:=65, _
Criteria1:="<=" & Trimestre_In_Corso, Operator:=xlAnd
'
'-------- Poi inizia a nascondere le colonne inutili -----------
Application.Goto Reference:="First_Cell_Rapporto_Clientela:" _
& "First_Cell_Anno"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Codice_Unico_MFG:" _
& "First_Cell_VR_TWM"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_N__Stampa_Mappatura"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Trend"
Selection.EntireColumn.Hidden = True
'----------------------------------------------------------------
Application.Goto Reference:="First_Cell_Valutazione_Sistema:" _
& "First_Cell_Note_Osservazioni"
Selection.EntireColumn.Hidden = True
'---Torna alla First Cell Fornitore -----------------------------
Application.Goto Reference:="First_Cell_Fornitore"
Application.Goto Reference:="First_Cell_Validità_fino_al"
Selection.EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub