Condividi tramite

Vba Excel - Il carattere "*"

Anonimo
2016-09-15T13:23:48+00:00

Buongiorno a tutti,

ho uno stramaledetto file che arriva da Sap che contiene dei record così strutturati :

AAA                     100

BBB                      100

AAXXX                 200

* tot.liv1               400

e via dicendo

Con del codice Vba dovrei trovare quelle righe che iniziano con asterisco , uso la funzione sotto riportata che mi dovrebbe restituire il numero di riga dove trova l’asterisco, nell’esempio sopra riportato il numero riga 4

Private Function pFindRowPos(sText As Variant, _

Optional SearchDirection As XlSearchDirection = xlNext, _

Optional SearchOrder As XlSearchOrder = xlByRows) As Long

Dim lResult As Long, oRg As Range

'Cambiare LookAt in xlWhole per ricerca esatta e non parziale

Set oRg = Cells.Find(What:=sText, LookIn:=xlValues, _

LookAt:=xlPart, SearchOrder:=SearchOrder, _

SearchDirection:=SearchDirection, _

MatchCase:=False, SearchFormat:=False)

If Not oRg Is Nothing Then lResult = oRg.Row

pFindRowPos = lResult

Set oRg = Nothing

End Function

La funzione però mi restituisce la riga 1 , anche se del carattere asterisco non c’è traccia. Anche se faccio la classica funzione Cerca dal menù excel mi prende la prima riga e se continuo si ferma in tutte le righe come se fosse presente in tutto il foglio.

Ma è un carattere speciale e va cercato in qualche altro modo ?

Grazie

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

  1. Anonimo
    2016-09-15T13:57:50+00:00

    Ciao IL CREM,

    ho uno stramaledetto file che arriva da Sap che contiene dei record così strutturati :

    AAA                     100

    BBB                      100

    AAXXX                 200

    * tot.liv1               400

    e via dicendo

    Con del codice Vba dovrei trovare quelle righe che iniziano con asterisco , uso la funzione sotto riportata che mi dovrebbe restituire il numero di riga dove trova l’asterisco, nell’esempio sopra riportato il numero riga 4

    Private Function pFindRowPos(sText As Variant, _

    Optional SearchDirection As XlSearchDirection = xlNext, _

    Optional SearchOrder As XlSearchOrder = xlByRows) As Long

    Dim lResult As Long, oRg As Range

    'Cambiare LookAt in xlWhole per ricerca esatta e non parziale

    Set oRg = Cells.Find(What:=sText, LookIn:=xlValues, _

    LookAt:=xlPart, SearchOrder:=SearchOrder, _

    SearchDirection:=SearchDirection, _

    MatchCase:=False, SearchFormat:=False)

    If Not oRg Is Nothing Then lResult = oRg.Row

    pFindRowPos = lResult

    Set oRg = Nothing

    End Function

    La funzione però mi restituisce la riga 1 , anche se del carattere asterisco non c’è traccia. Anche se faccio la classica funzione Cerca dal menù excel mi prende la prima riga e se continuo si ferma in tutte le righe come se fosse presente in tutto il foglio.

    Ma è un carattere speciale e va cercato in qualche altro modo ?

    Grazie

    Per cercare un carattere jolly, è necessario fa precedere il jolly da un carattere tilde (~). Quindi, prova qualcosa del genere:

    '=========>>

    Option Explicit

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

    Public Sub Tester()

        Dim sStr As String

        sStr = "~*"

        MsgBox pFindRowPos(sStr)

    End Sub

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

    Private Function pFindRowPos(sText As Variant, _

                                 Optional SearchDirection As XlSearchDirection = xlNext, _

                                 Optional SearchOrder As XlSearchOrder = xlByRows) As Long

        Dim lResult As Long, oRg As Range

        'Cambiare LookAt in xlWhole per ricerca esatta e non parziale

        Set oRg = Cells.Find(What:=sText, LookIn:=xlValues, _

                             LookAt:=xlPart, SearchOrder:=SearchOrder, _

                             SearchDirection:=SearchDirection, _

                             MatchCase:=False, SearchFormat:=False)

        If Not oRg Is Nothing Then lResult = oRg.Row

        pFindRowPos = lResult

        Set oRg = Nothing

    End Function

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento

4 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2016-09-19T14:13:48+00:00

    Ciao IL CREM,

    Ultima cosa sempre su questa problematica , la funzione pfindRows mi parte sempre dall'inizio, quindi se dopo la riga 3 c'è un altro asterisco si ferma sempre alla 3.

    C'è un modo per fare praticamente il trova successivo ?

    Tra vari approcci possibili, e dato che solo i numeri di riga sono di interesse, prova qualcosa del genere:

    '=========>>

    Option Explicit

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

    Public Sub Tester()

        Dim WB As Workbook

        Dim SH As Worksheet

        Dim searchRng As Range, foundRng As Range

        Dim rCell As Range, rRow As Range

        Dim arrOut() As Variant

        Dim aStr As String, bStr As String

        Dim sMsg As String, ibuttons As Long, sTitle As String

        Dim iCtr As Long, iRow As Long, iRows As Long

        Const sStr As String = "~*"                             

        Const sIntervallo As String = "A1:D100"               '<<=== Modifica

        Set WB = ThisWorkbook

        Set SH = WB.Sheets("Foglio1")

        Set searchRng = SH.Range(sIntervallo)

        iRows = searchRng.Rows.Count

        aStr = Replace(sStr, "~", vbNullString)

        Do

            With searchRng

                Set searchRng = .Offset(iRow).Resize(iRows - iRow)

            End With

            Set rCell = searchRng.Find(What:=sStr, _

                                       LookIn:=xlValues, _

                                       LookAt:=xlPart, _

                                       SearchDirection:=xlNext, MatchCase:=False)

            If Not rCell Is Nothing Then

                iCtr = iCtr + 1

                ReDim Preserve arrOut(1 To iCtr)

                iRow = rCell.Row

                arrOut(iCtr) = iRow

            End If

        Loop While Not rCell Is Nothing And iRow <> iRows

        If CBool(iCtr) Then

            bStr = Join(arrOut, vbNewLine)

            sMsg = "La stringa di ricerca " _

                 & aStr _

                 & " e' stato trovato nelle seguenti righe:" _

                 & vbNewLine & bStr

            ibuttons = vbInformation

            sTitle = "REPORT"

        Else

            sMsg = "La stringa di ricerca " _

                 & aStr _

                 & " non e' stato trovato nell'intervallo " _

                 & sIntervallo & "!!"

            ibuttons = vbCritical

            sTitle = "NON TROVATA!"

        End If

        Call MsgBox( _

             Prompt:=sMsg, _

             Buttons:=ibuttons, _

             Title:=sTitle)

    End Sub

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-09-19T09:05:42+00:00

    Ultima cosa sempre su questa problematica , la funzione pfindRows mi parte sempre dall'inizio, quindi se dopo la riga 3 c'è un altro asterisco si ferma sempre alla 3.

    C'è un modo per fare praticamente il trova successivo ?

    Grazie

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2016-09-15T16:13:38+00:00

    Ciao IL CREM,

    Perfetto grazie mille

    Ti ringrazio per il cortese riscontro.

    Per chiudere questo thread, vorrei chiederti gentilmente di segnare la mia risposta come Risposta. In questo modo, tu aiuterai anche coloro che potessero cercare soluzioni ai problemi simili negli archivi della Community.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2016-09-15T15:55:23+00:00

    Perfetto grazie mille

    La risposta è stata utile?

    0 commenti Nessun commento