ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Validata a variable (https://www.excelbanter.com/excel-programming/435401-validata-variable.html)

Alberto Ast[_2_]

Validata a variable
 
I need to validate a cell contains only numbers from the 3rd position to the
forth. I have below statement to convert to number but if there is an
alfanumeric value the macro fails.

myVal = Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4) * 1

How do I do it?

Jacob Skaria

Validata a variable
 
Hi again..

--You can use IsNumeric to check whether this part is numeric..
--If you mean 3rd position to the forth which mean 2 positions this should be
mid(string,3,2) instead of mid(string,3,4)


Dim myval As Variant
If IsNumeric(Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4)) Then
myVal = Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4) * 1
End If

'OR

Dim myval As Variant
With wb.Sheets("AOS Form")
If IsNumeric(Mid(.Range("Q2").Value, 3, 4)) Then
myVal = Mid(.Range("Q2").Value, 3, 4) * 1
End If
End With



If this post helps click Yes
---------------
Jacob Skaria


"Alberto Ast" wrote:

I need to validate a cell contains only numbers from the 3rd position to the
forth. I have below statement to convert to number but if there is an
alfanumeric value the macro fails.

myVal = Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4) * 1

How do I do it?


Alberto Ast[_2_]

Validata a variable
 
Thanks... you got them all right


"Jacob Skaria" wrote:

Hi again..

--You can use IsNumeric to check whether this part is numeric..
--If you mean 3rd position to the forth which mean 2 positions this should be
mid(string,3,2) instead of mid(string,3,4)


Dim myval As Variant
If IsNumeric(Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4)) Then
myVal = Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4) * 1
End If

'OR

Dim myval As Variant
With wb.Sheets("AOS Form")
If IsNumeric(Mid(.Range("Q2").Value, 3, 4)) Then
myVal = Mid(.Range("Q2").Value, 3, 4) * 1
End If
End With



If this post helps click Yes
---------------
Jacob Skaria


"Alberto Ast" wrote:

I need to validate a cell contains only numbers from the 3rd position to the
forth. I have below statement to convert to number but if there is an
alfanumeric value the macro fails.

myVal = Mid(wb.Sheets("AOS Form").Range("Q2").Value, 3, 4) * 1

How do I do it?



All times are GMT +1. The time now is 08:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com