Script Ricerca

Anonimo
2022-11-16T16:34:19+00:00

Salve,

nella colonna C di Excel 2019 sono scritti i numeri dei libri che corrispondono ad un Oggetto di ricerca.

Ad esempio: - il foglio 'Armonia jazz' contiene singolarmente gli indici dei libri di armonia jazz.

  • Colonna A Oggetto dell'indice
  • Colonna B pag. del libro
  • Colonna C n. del libro

Nella VB della colonna C ho inserito questo script di ricerca del n. del libro:

'Private Sub TextLibro1_Change()

'Range("C2").AutoFilter field:=1, Criteria1:=TextLibro.Text & "*"

Range("C3").AutoFilter field:=3, Criteria1:="*" & TextLibro1.Text & "*"

If TextLibro1.Text = "" Then

Selection.AutoFilter

End If

End Sub

Ho costruito un campo nella 1' cella della colonna C.

Quando scrivo un numero del libro da cercare, alcune celle rispondono alla ricerca, altre invece no.

Ad esempio volendo cercare il n. 1340 la colonna C li mostra tutti; ma se immetto 1365 non mi compare nulla.

Ho sbagliato qualcosa oppure dovrei intervenire sulle celle? (le ho settate tutte sul Testo).

Gentilissimi.

Carlo - Verona

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

7 risposte

Ordina per: Più utili
  1. Anonimo
    2022-11-19T11:15:13+00:00

    Ciao Carlo,

    Norman, Ho inviato il file

    Ho ricevuto la tua email.

    Il tuo problema risiede nel fatto che valori come 1365 non sono stati inseriti come valori di testo e il tuo filtro sta cercando una stringa. Sfortunatamente, l'impostazione del formato su testo dopo l'inserimento dei dati non risolverà il problema; sarà necessario reinserire i dati dopo aver impostato il formato.

    Poiché questo sarebbe un lavoro noioso e lungo se eseguito manualmente, ho eseguuito la seguente procedura per correggere i tuoi dati:

    '========>>

    Option Explicit

    '-------->>

    Public Sub Tester()

    Dim SH As Worksheet

    Dim Rng As Range

    Dim arrin As Variant

    Dim LRow As Long, i As Long

    Const iPrima_Riga As Long = 3

    For Each SH In ThisWorkbook.Worksheets

    With SH

    LRow = LastRow(SH, .Columns("C"))

    If LRow > iPrima_Riga Then

    Set Rng = .Range("C" & iPrima_Riga).Resize(LRow - iPrima_Riga + 1)

    End If

    End With

    If Not Rng Is Nothing Then

    arrin = Rng.Value

    For i = LBound(arrin) To UBound(arrin)

    arrin(i, 1) = CStr(arrin(i, 1))

    Next i

    With Rng

    .NumberFormat = "@"

    .Value = arrin

    End With

    Set Rng = Nothing

    End If

    Next SH

    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

    '<<========

    Ora, inserendo 1365 o qualsiasi altra chiave di ricerca valida, filtrerà correttamente i tuoi dati.

    Ti ho inviato il file aggiornato.

    ===

    Regards,

    Norman

    Immagine

    Quindi, il tuo script agisce su tutte le colonne? Oppure solo colonna C?

    Le prossime volte che immetto dei dati, prima devo abilitare il tuo script per la proprietà testo? Oppure viene fatto in automatico rispetto dove l'hai inserito.

    (Il file funziona!)

    C.

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2022-11-17T18:53:08+00:00

    Ciao Carlo,

    Norman, Ho inviato il file

    Ho ricevuto la tua email.

    Il tuo problema risiede nel fatto che valori come 1365 non sono stati inseriti come valori di testo e il tuo filtro sta cercando una stringa. Sfortunatamente, l'impostazione del formato su testo dopo l'inserimento dei dati non risolverà il problema; sarà necessario reinserire i dati dopo aver impostato il formato.

    Poiché questo sarebbe un lavoro noioso e lungo se eseguito manualmente, ho eseguuito la seguente procedura per correggere i tuoi dati:

    '========>>

    Option Explicit

    '-------->>

    Public Sub Tester()

    Dim SH As Worksheet 
    
    Dim Rng As Range 
    
    Dim arrin As Variant 
    
    Dim LRow As Long, i As Long 
    
    Const iPrima\_Riga As Long = 3 
    
    For Each SH In ThisWorkbook.Worksheets 
    
        With SH 
    
            LRow = LastRow(SH, .Columns("C")) 
    
            If LRow &gt; iPrima\_Riga Then 
    
                Set Rng = .Range("C" & iPrima\_Riga).Resize(LRow - iPrima\_Riga + 1) 
    
            End If 
    
        End With 
    
        If Not Rng Is Nothing Then 
    
            arrin = Rng.Value 
    
            For i = LBound(arrin) To UBound(arrin) 
    
                arrin(i, 1) = CStr(arrin(i, 1)) 
    
            Next i 
    
            With Rng 
    
                .NumberFormat = "@" 
    
                .Value = arrin 
    
            End With 
    
            Set Rng = Nothing 
    
        End If 
    
    Next SH 
    

    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 &lt; minRow Then 
    
        LastRow = minRow 
    
    End If 
    

    End Function

    '<<========

    Ora, inserendo 1365 o qualsiasi altra chiave di ricerca valida, filtrerà correttamente i tuoi dati.

    Ti ho inviato il file aggiornato.

    ===

    Regards,

    Norman

    Immagine

    La risposta è stata utile?

    0 commenti Nessun commento