Condividi tramite

Con vba excel come posso con il metodo find ottenere la prima occorrenza di Aprile in una colonna di date ?

Anonimo
2023-05-21T17:42:28+00:00

Con vba excel come posso con il metodo find ottenere la prima occorrenza di Aprile in una colonna di date ? Non conosco la data precisa da cercare (potrebbe essere 01/04/2023 oppure 08/04/2023). A me serve sapere qual'e' la prima cella che contiene una data di Aprile 2023 Forse find non è il metodo più appropriato? Grazie dell'aiuto.

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
2023-05-21T18:17:48+00:00

Ciao

Sono Sneha e sarei felice di aiutarti con la tua domanda. Ci scusiamo per l'inconveniente causato.

Per farlo, prova a seguire il codice: -

Sub FindFirstApril() Dim ws come foglio di lavoro Dim rng come intervallo Attenua foundCell come intervallo Dim searchValore come data

Per prima cosa, identifichiamo il foglio di lavoro con cui stai lavorando. ' Please replace "Sheet1" with the name of your actual sheet. Set ws = ThisWorkbook.Sheets("Foglio1")

' Next, specify the range of dates you want to search within. You can modify "A1:A100" to fit your specific range requirements. Imposta rng = ws. Intervallo("A1:A100")

' Ora, determiniamo il valore di ricerca - aprile dell'anno in corso. searchValue = DateSerial(Year(Date), 4; 1)

' We'll use the Find method to locate the first occurrence of April within the range. The LookIn parameter is set to search within the cell values, and LookAt is set to find an exact match. Imposta foundCell = rng. Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)

Dopo la ricerca, controlleremo se è stata trovata una corrispondenza. Se non foundCell non è nulla, allora If a match is found, we'll display a message box with the address of the cell. MsgBox "La prima occorrenza di aprile nell'intervallo specificato si trova nella cella " & foundCell.Address & "." Altro Se non viene trovata alcuna corrispondenza, verrà visualizzato un messaggio che indica che April non è presente nell'intervallo. MsgBox "Aprile non viene trovato nell'intervallo specificato." Fine Se Fine sub

Assicurati di sostituire "Foglio1" con il nome effettivo del foglio di lavoro con cui stai lavorando. Inoltre, regola l'intervallo "A1:A100" in modo che corrisponda all'intervallo in cui si trovano le date.

Se hai altre domande o hai bisogno di assistenza con qualsiasi cosa, non esitare a farmelo sapere. Sarà mio piacere assistervi.

Migliori saluti Sneha

Restituisci alla comunità. Aiuta la persona successiva con questo problema indicando se questa risposta ha risolto il tuo problema. Fare clic su Sì o No nella parte inferiore.

Questa risposta è stata tradotta automaticamente. Di conseguenza, potrebbero esserci errori grammaticali o espressioni strane.

La risposta è stata utile?

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

10 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2023-05-25T20:14:39+00:00

    Ciao Luca,

    Nel frattempo, prova qualcosa del genere:

    '========>>

    Option Explicit

    '-------->>

    Public Sub Trova_Prima_Data_In_Mese()

    Dim SH  As Worksheet 
    
    Dim Rng As Range, rFoundCell As Range 
    
    Dim arrIn As Variant, arrMese As Variant, arrMese\_Ordinato 
    
    Dim sMsg As String 
    
    Dim myDate As Variant 
    
    Dim iMonth As Long, LRow As Long, iRow As Long 
    
    Dim pRow As Long, uRow As Long 
    
    Dim UB As Long, UB2 As Long 
    
    Const sFoglio As String = **"Foglio1"**                     **'<<=== Modifica** 
    
    Const sColonna\_Ricerca As String = **"A:A"            '<<=== Modifica** 
    
    iMonth = Application.InputBox(Prompt:="Immetti il numero del mese", Title:="Mese di Ricerca", Default:=Month(Date), Type:=1) 
    
    Set SH = ThisWorkbook.Sheets(sFoglio) 
    
    With SH 
    
        LRow = LastRow(SH, .Range(sColonna\_Ricerca)) 
    
        Set Rng = SH.Range(sColonna\_Ricerca).Resize(LRow) 
    
    End With 
    
    arrIn = Rng.Value 
    
    arrMese = Evaluate("=Filter(" & Rng.Address & ", Month(" & Rng.Address & ") =" & iMonth & ", """")") 
    
    arrMese\_Ordinato = Evaluate("=Sort(Filter(" & Rng.Address & ", Month(" & Rng.Address & ") =" & iMonth & ", """"))") 
    
    On Error Resume Next 
    
    UB = UBound(arrMese) 
    
    UB2 = UBound(arrMese, 2) 
    
    On Error GoTo 0 
    
    If UB2 = 1 Then 
    
    iRow = aRow(SH, CDate(arrMese\_Ordinato(1, 1)), Rng) 
    
      pRow = aRow(SH, CDate(arrMese(1, 1)), Rng) 
    
      uRow = aRow(SH, CDate(arrMese(UB, 1)), Rng) 
    
        sMsg = "La prima data nel mese " & MonthName(iMonth) & " = " & Format(arrMese\_Ordinato(1, 1), "dd/mm/yyyy") \_ 
    
        & vbNewLine \_ 
    
        & "Questa data si trova nella Cella " & SH.Cells(iRow, Rng.Column).Address(0, 0) \_ 
    
        & vbNewLine & vbNewLine \_ 
    
        & vbNewLine \_ 
    
        & "La prima data del mese " & MonthName(iMonth) & " che si trova nell'elenco = " & Format(arrMese(1, 1), "dd/mm/yyyy") \_ 
    
        & vbNewLine \_ 
    
        & "Questa data si trova nella Cella " & SH.Cells(pRow, Rng.Column).Address(0, 0) \_ 
    
        & vbNewLine & vbNewLine \_ 
    
        & "L'ultima data del mese " & MonthName(iMonth) & " che si trova nell'elenco = " & Format(arrMese(UB, 1), "dd/mm/yyyy") \_ 
    
        & vbNewLine \_ 
    
        & "Questa data si trova nella Cella " & SH.Cells(uRow, Rng.Column).Address(0, 0) 
    
        Else 
    
        sMsg = "Nessuna data trovata per il mese " & MonthName(iMonth) 
    
    End If 
    
    Call MsgBox(Prompt:=sMsg, \_ 
    
        Buttons:=vbInformation, \_ 
    
        Title:="REPORT") 
    

    End Sub

    '--------->>

    Public Function LastRow(SH As Worksheet, _

    Optional Rng As Range, \_ 
    
    Optional minRow As Long = 1) 
    
    If Rng Is Nothing Then 
    
        Set Rng = SH.Cells 
    
    End If 
    
    On Error Resume Next 
    
    LastRow = Rng.Find(What:="\*", \_ 
    
        After:=Rng.Cells(1), \_ 
    
        LookAt:=xlPart, \_ 
    
        LookIn:=xlFormulas, \_ 
    
        SearchOrder:=xlByRows, \_ 
    
        SearchDirection:=xlPrevious, \_ 
    
        MatchCase:=False).Row 
    
    On Error GoTo 0 
    
    If LastRow < minRow Then 
    
        LastRow = minRow 
    
    End If 
    

    End Function

    '--------->>

    Public Function aRow(SH As Worksheet, _

    dDate As Date, oRng As Range) 
    
    On Error Resume Next 
    
    aRow = oRng.Find(What:=dDate, \_ 
    
        After:=oRng.Cells(1), \_ 
    
        LookAt:=xlWhole, \_ 
    
        LookIn:=xlValues, \_ 
    
        SearchOrder:=xlByRows, \_ 
    
        SearchDirection:=xlNext, \_ 
    
        MatchCase:=False).Row 
    
    On Error GoTo 0 
    

    End Function

    '<<========

    Potresti scaricare iol mio file di prova Luca20230525.xlsm

    Eseguendo questa procedura e selezionando aprile, ovvero 4, ottengo i seguenti risultati:

     [![](https://learn-attachment.microsoft.com/api/attachments/65c565cd-591e-4381-ab33-ea0b77aae08c?platform=QnA"https://learn-attachment.microsoft.com/api/attachments/50510cce-38d1-4af6-92c9-1d96623569ff?platform=QnA" rel="ugc nofollow">Immagine

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2023-05-25T16:33:39+00:00

    Ciao Luca,

    Benissimo Norman, grazie 1000, funziona a meraviglia, e mi viene da chiederti: (ho già fatto qualche prova ma non ci sono riuscito), nello stesso codice è possibile inserire un'istruzione per trovare anche la riga dell'ultima occorrenza presente nella colonna sempre relativa allo stesso mese in esame, affinchè otteniamo: (pr = prima riga con data ??/iMonth/2023 ) e (ur = ultima riga con data ??/iMonth/2023).

    Con riferimento all'immagine seguente, e supponendo che aprile sia selezionato come mese di interesse, ti chiederei cortesemente di confermare che desideri ora anche ottenere gli indirizzi delle due celle evidenziati in giallo.

       [![](https://learn-attachment.microsoft.com/api/attachments/925d0074-eecc-4a89-8151-2b24305bc258?platform=QnA"https://learn-attachment.microsoft.com/api/attachments/50510cce-38d1-4af6-92c9-1d96623569ff?platform=QnA" rel="ugc nofollow">Immagine

    La risposta è stata utile?

    0 commenti Nessun commento