Da stringa VBA a numero intero: utilizza la funzione CINT per la conversione dei dati

Da stringa VBA di Excel a numero intero

String e Integer sono tipi di dati comuni, ma spesso come parte della nostra analisi dei dati, potremmo voler convertire la variabile del tipo di dati stringa in un tipo di dati intero e ciò è possibile utilizzando la funzione "CINT" in VBA.

Sintassi della funzione CINT

"CINT" è classificato come una funzione di conversione del tipo di dati. Questa funzione converte l'espressione fornita in un tipo di dati Integer. Quando dico "Espressione", deve essere un valore numerico compreso nell'intervallo del tipo di dati Integer in VBA, ovvero tra -32768 e 32767.

Ora, guarda la sintassi della funzione CINT in VBA.

  • L'espressione è il valore o la variabile che contiene il valore da convertire in Tipo di dati intero.
Nota 1: solo quando si passa il valore dell'espressione deve essere compreso tra -32768 e 32767. In caso contrario, si verificherà un "Errore di flusso eccessivo", ovvero l'assegnazione del valore che è oltre il limite del tipo di dati. Nota 2: l' espressione deve essere un valore numerico. Se l'espressione è un valore di stringa o di testo, verrà generato un "Errore di runtime 13: tipo non corrispondente".

In questo articolo, ti mostreremo la conversione del tipo di dati intero da stringa a numero intero utilizzando la funzione "CINT".

Esempi di funzione CINT in VBA

Esempio 1

Eseguiamo il primo esempio di conversione di un numero di frazione in un tipo di dati intero.

Codice:

Sub CINT_Example1 () Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = 2564.589 IntegerResult = CInt (IntegerNumber) MsgBox IntegerResult End Sub
  • Nel codice precedente per la variabile "IntegerNumber", ho assegnato il valore "2564.589".
  • Nella riga successiva, ho applicato una funzione CINT. Quindi convertirà il valore fornito nel numero intero più vicino.

Quindi, abbiamo ottenuto il risultato: 2565 e il numero fornito è 2564,589. Quando il valore decimale è maggiore di 0,5, le funzioni CINT arrotondano per eccesso il numero al numero intero successivo. Quando il valore decimale è minore o uguale a 0,5 CINT, le funzioni arrotondano per difetto allo stesso numero intero.

Per un esempio, guarda il codice sottostante dove il numero è inferiore a 0,5

Codice:

Sub CINT_Example1 () Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = 2564 - 0.5 IntegerResult = CInt (IntegerNumber) MsgBox IntegerResult End Sub

Questo dovrebbe dare il risultato come 2564, non 2565.

Esempio n. 2

Ora proverò ad assegnare il numero che è superiore al limite del tipo di dati Integer.

Codice:

Sub CINT_Example2 () Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = 51456.785 IntegerResult = CInt (IntegerNumber) MsgBox IntegerResult End Sub

Ho assegnato il numero come "IntegerNumber = 51456.785" che è superiore al limite del limite del tipo di dati Integer. Ora se provo a eseguire il codice, verrà generato un errore di overflow in VBA.

Ogni volta che il numero fornito è maggiore del tipo di dati Integer, mostriamo la semplice finestra di messaggio che dice "Il numero è superiore al limite del tipo di dati Integer".

Codice:

Sub CINT_Example2() Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = 51456.785 On Error GoTo Message: IntegerResult = CInt(IntegerNumber) Message: If Err.Number = 6 Then MsgBox IntegerNumber & " is More Than the Limit of the Integer Data Type" End Sub

This will show the message like the below whenever the provided number is out of the range of Integer data type.

Example #3

Now I will try and assign the value which is not numeric.

Sub CINT_Example2() Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = "Hello" IntegerResult = CInt(IntegerNumber) MsgBox IntegerResult End Sub

I have assigned the value as “Hello” which is not numeric. This causes “Type Mismatch Error”.

Instead of showing the error like this, let’s put a simple information message box in front of the user. The below code will put the message box saying “The Provided Expression Value is Non-Numeric, So cannot be converted”.

Code:

Sub CINT_Example2() Dim IntegerNumber As String Dim IntegerResult As Integer IntegerNumber = "Hello" On Error GoTo Message: IntegerResult = CInt(IntegerNumber) Message: If Err.Number = 13 Then MsgBox IntegerNumber & " : The Provided Expression Value is Non-Numeric, So cannot be converted" End Sub

This will show the message box below.

Things to Remember

  • CINT converts value from another data type to an integer data type.
  • Integer can hold values from -32768 to 32767 anything above these numbers will cause an overflow error.
  • Solo il tipo di dati stringa contiene dati numerici che verranno convertiti in un tipo di dati intero.

Articoli consigliati

Questa è stata una guida a VBA String to Integer. Qui discutiamo esempi di stringa in numero intero in Excel VBA utilizzando la funzione CINT insieme ad esempi pratici e modello Excel scaricabile. Di seguito sono riportati alcuni articoli utili relativi a VBA:

  • VBA Split String in Array
  • Stringa VBA fino ad oggi
  • Confronto tra stringhe VBA
  • VBA Sostituisci stringa

Articoli interessanti...