IsNumeric giving error if variable is a text

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. 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?

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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
try this update to your code & see if helps

VBA Code:
Public Sub MyInputBox()
    Dim MyInputText As String
    Dim MyInput     As Variant
   
    'Caixa para input do user quando se carrega no botão
    Do
        MyInput = InputBox("Escrever número Do novo mês", "Iniciar novo mês", "Número Do novo mês")
        'cancel pressed
        If StrPtr(MyInput) = 0 Then Exit Sub
    Loop Until Val(MyInput) >= 1 And Val(MyInput) <= 12
   
    'Pega no que foi inserido e checa se é número e se está entre 1 e 12
   
    MyInputText = StrConv(MonthName(Val(MyInput), False), vbProperCase)
   
    Call ResetTable
   
    Range("C3").Value = MyInputText
   
End Sub

Dave
 
Upvote 0
Another method:
VBA Code:
Public Sub MyInputBox()
    Dim MyInput As String
    Dim NumericInput As Boolean
    Dim MyInputText As String
   
    '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 Then
        If MyInput >= 1 And MyInput <= 12 Then
            MyInputText = StrConv(MonthName(MyInput, False), vbProperCase)
            Call ResetTable
            Range("C3").Value = MyInputText
        Else
            GoTo errHand
        End If
    Else
        GoTo errHand
    End If
   
    Exit Sub
errHand:
    MsgBox "Número de mês mal introduzido"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top