Usare le funzioni sinistra, destra, media e len in Visual Basic, Applications Edition in Excel

Riepilogo

Questo articolo contiene esempi di come modificare le stringhe di testo usando le funzioni Left, Right, Mid e Len in Microsoft Visual Basic, Applications Edition in Microsoft Excel.

Ulteriori informazioni

Microsoft fornisce esempi di programmazione a scopo puramente illustrativo, senza alcuna garanzia di qualsiasi tipo, sia espressa che implicita, ivi incluse, senza limitazioni, le garanzie implicite di commerciabilità o idoneità per uno scopo particolare. In questo articolo si presuppone che l'utente conosca il linguaggio di programmazione in questione e gli strumenti utilizzati per creare ed eseguire il debug delle procedure. I tecnici di supporto tecnico Microsoft sono autorizzati a fornire spiegazioni in merito alla funzionalità di una particolare routine, ma in nessuno caso a modificare questi esempi per fornire funzionalità aggiuntive o a creare routine atte a soddisfare specifiche esigenze.

Per la procedura seguente viene illustrato l'uso delle funzioni Left, Right, Mid e Len di Microsoft Visual Basic, Applications Edition in Microsoft Excel:

  1. Creare una nuova cartella di lavoro di Excel vuota.

  2. Aprire la Editor di Visual Basic premendo ALT+F11.

  3. Nel menu Inserisci, fare clic su Modulo.

  4. Digitare la macro seguente nel nuovo foglio del modulo.

          Sub String_Len()
              ' Sets MyString.
              MyString = InputBox("Enter some text.")
              ' Displays length of string.
              MsgBox Prompt:="The length of the string is " & _
                  Len(MyString) & " characters."
          End Sub
    
          Sub String_Left()
              ' Sets MyString.
              MyString = InputBox("Enter some text.")
              StringLen = Len(MyString)
              Pos = InputBox("Please enter a number from 1 to " & StringLen)
              ' Takes the left number of specified characters.
              Result = Left(MyString, Pos)
              ' Displays the result.
              MsgBox Prompt:="The left " & Pos & " characters of """ & _
                  MyString & """ are: " & _
                  Chr(13) & Result
          End Sub
    
          Sub String_Right()
              ' Sets MyString.
              MyString = InputBox("Enter some text.")
              StringLen = Len(MyString)
              Pos = InputBox("Please enter a number from 1 to " & StringLen)
              ' Takes the right number of specified digits.
              Result = Right(MyString, Pos)
              ' Displays the result.
              MsgBox Prompt:="The right " & Pos & " characters of """ & _
                  MyString & """ are: " & _
                  Chr(13) & Result
          End Sub
    
          Sub String_Mid()
              ' Sets MyString.
              MyString = InputBox("Enter some text.")
              ' Sets starting position.
              StartPos = InputBox _
                  ("Give me a starting position (1 to " _
                  & Len(MyString) & ")")
              ' Determines length of string of text.
              StringLen = Len(MyString) - StartPos + 1
              ' Sets number of characters.
              NumChars = InputBox _
                  ("How many characters would you like? (From 1 to " & _
                  StringLen & ")")
              MsgBox prompt:="The result is: " & _
                  Mid(MyString, StartPos, NumChars)
          End Sub
    
    

Per visualizzare un esempio delle funzioni Left, Right, Mid e Len, usare una delle procedure seguenti, come appropriato per la versione di Excel in esecuzione:

  • In Microsoft Office Excel 2007 fare clic sulla scheda Sviluppo , fare clic su Macro nel gruppo Codice , selezionare la macro per la funzione desiderata e quindi fare clic su Esegui
  • In Microsoft Office Excel 2003 e nelle versioni precedenti di Excel fare clic su Macro nel menu Strumenti , selezionare la macro per la funzione desiderata e quindi fare clic su Esegui.

Riferimenti

Per altre informazioni su queste funzioni, digitare il testo seguente in un foglio del modulo:

  • Len, funzione
  • Right
  • Sinistra
  • Metà

Evidenziare la funzione su cui si vogliono altre informazioni e quindi premere F1.