Funzione InputBox VBA - Come creare InputBox e memorizzare i valori?

InputBox VBA di Excel

VBA InputBox è una funzione incorporata utilizzata per ottenere un valore dall'utente, questa funzione ha due argomenti principali in cui uno è l'intestazione per la casella di input e un altro è la domanda per la casella di input, la funzione della casella di input può memorizzare solo i tipi di dati di input che può contenere variabile.

Spesso in Excel, utilizziamo i dati che sono già presenti nel foglio Excel. A volte abbiamo bisogno anche di qualche tipo di dati di input dagli utenti. Soprattutto in VBA, l'input dell'utente è spesso richiesto.

Utilizzando InputBox, possiamo ottenere i dati dall'utente e utilizzarli per il nostro scopo. Un InputBox chiederà all'utente di inserire il valore visualizzando InputBox.

Sintassi

  • Prompt: questo non è altro che il messaggio all'utente tramite una casella di input.
  • Titolo: qual è il titolo della casella di immissione?
  • Predefinito: qual è il valore predefinito della casella di input? Questo valore viene visualizzato nell'area di digitazione della casella di immissione.

Questi tre parametri sono abbastanza buoni in Excel. Ignora gli altri 4 parametri opzionali. Per comprendere questa sintassi, guarda lo screenshot qui sotto.

Come creare InputBox in VBA?

Ok, passiamo subito alla praticità. Segui i passaggi seguenti per creare la tua prima casella di input.

Passaggio 1: vai a VBE (Visual Basic Editor) e inserisci un nuovo modulo.

Passaggio 2: fare doppio clic sul modulo inserito e creare un nome macro.

Passaggio 3: inizia a digitare la parola "InputBox" e vedrai le opzioni correlate.

Passaggio 4: seleziona la casella di input e dai spazio, e vedrai la sintassi della casella di input.

Passaggio 5: fornire il messaggio "Immettere il nome".

Passaggio 6: digita il titolo della casella di input come "Informazioni personali".

Passaggio 7: digita il valore predefinito "Digita qui".

Passaggio 8: abbiamo finito. Esegui questo codice e vedi la tua prima casella di input.

Memorizza il valore di InputBox nelle celle

Ora passeremo attraverso il processo di memorizzazione dei valori nelle celle. Segui i passaggi seguenti.

Passaggio 1: dichiarazione della variabile come variante.

Codice:

Sub InputBox_Example () Dim i As Variant End Sub

Step 2: For this variable, assign the value through the inputbox.

Code:

Sub InputBox_Example() Dim i As Variant i = InputBox("Please Enter Your Name", "Personal Information", "Type Here") End Sub

Note: Once the input box comes to the right of the equal sign, we need to enter the arguments or syntax in brackets like our regular formulas.

Step 3: Now, whatever the value typed in the input box, we need to store it in cell A1. So for this, write the code as Range (“A1”).Value = i

Code:

Sub InputBox_Example() Dim i As Variant i = InputBox("Please Enter Your Name", "Personal Information", "Type Here") Range("A1").Value = i End Sub

Ok, we are done. Let’s run this code now by pressing the F5 key, or you can also run the code manually, as shown in the below screenshot.

As soon as you run this code, we will see the inputbox.

Type the name and click on Ok.

As soon as you type the name and click on OK, you will see the inputbox value in cell A1.

Note: We can store any value from the inputbox if the variable is defined properly. In the above example, I have defined the variable as a Variant, which can hold all types of data.

For example, now I have changed the variable type to Date.

Now run the code and type other than the date.

Click on ok and see what the response is.

We got the error value as Type mismatch. Since we have declared the variable data type as DATE, we cannot store anything other than DATE with an inputbox.

Now enter the date and see what happens.

As soon as you type the date and then click on OK and see what the response is.

Since we have entered the correct value, we got the result in the cell.

Validation of Input from User

You know what we can actually allow users to enter only specific value i.e., allow the user to enter only text, only number, only logical values, etc.

To perform this task, we need to use the method Application.InputBox.

Let’s look at the syntax of the Application.InputBox.

  • Prompt: This is nothing but the message to the user through an input box.
  • Title: What is the title of the input box?
  • Default: What is the default value of the input box? This value appears in the typing area of the input box.
  • Left: What should be the x position of the input box in the current window?
  • Top: What should be the y position of the inputbox in the current window?

To start this, inputbox declare variable and assign the value to a variable.

Now to assign value to start the word Application.

After the word Application, put a dot (.) and type Inputbox.

Select the input box and open the bracket.

As usual, enter Prompt, Title, and Default Value.

Now ignore left, top, help file, help context ID by typing 5 commas (,).

Here Type means what should be the input string. Below are the validations available.

Quindi, di conseguenza, seleziona il tuo tipo. Ho selezionato 1 come parametro, cioè solo numeri.

Ora esegui il codice e il tipo di valore del testo.

Fare clic su OK e vedere cosa succede.

Dice che il numero non è valido. Quindi possiamo inserire solo numeri in questa casella di input.

Cose da ricordare

  • Abbiamo bisogno di una variabile per memorizzare il valore dato dalla casella di input.
  • Se stai usando InputBox senza il metodo Application, dovresti essere perfetto riguardo al tipo di dati variabile.
  • Utilizza il tipo di dati Variant, che può contenere qualsiasi tipo di tipo di dati e archivio.

Articoli interessanti...