Beneindias
Board Regular
- Joined
- Jun 21, 2022
- Messages
- 120
- Office Version
- 365
- Platform
- Windows
- MacOS
Hey guys,
I have an excel file where I have a button to start a new month.
When the button is pressed, it opens an InputBox, where the user has to insert month number.
In vba, I have code to check if the input is a number, anf if that number is between 1 and 12.
When I type a number, everything works as expected. If it's between 1 and 12, it puts the month name in the cell, if it's less than 1 or more than 12, it shows the msgbox.
My problem is when I type letters.
It presents the error "Type mismatch" in "If NumericInput And MyInput >= 1 And MyInput <= 12 Then"
I want the letters to trigger the MsgBox like when the number is not between 1 and 12.
Can anybody help me with this, please?
Thank you all
I have an excel file where I have a button to start a new month.
When the button is pressed, it opens an InputBox, where the user has to insert month number.
In vba, I have code to check if the input is a number, anf if that number is between 1 and 12.
When I type a number, everything works as expected. If it's between 1 and 12, it puts the month name in the cell, if it's less than 1 or more than 12, it shows the msgbox.
My problem is when I type letters.
It presents the error "Type mismatch" in "If NumericInput And MyInput >= 1 And MyInput <= 12 Then"
I want the letters to trigger the MsgBox like when the number is not between 1 and 12.
Can anybody help me with this, please?
VBA Code:
Public Sub MyInputBox()
Dim MyInput As String
Dim NumericInput As Boolean
'Caixa para input do user quando se carrega no botão
MyInput = InputBox("Escrever número do novo mês", "Iniciar novo mês", "Número do novo mês")
NumericInput = IsNumeric(MyInput)
'Pega no que foi inserido e checa se é número e se está entre 1 e 12
If NumericInput And MyInput >= 1 And MyInput <= 12 Then
MyInputText = StrConv(MonthName(MyInput, False), vbProperCase)
Call ResetTable
Range("C3").Value = MyInputText
Else
MsgBox "Número de mês mal introduzido"
End If
End Sub
Thank you all