Hello whenever I try to run the following code I get a runtime error 13 type mismatch and I dont know why or how to fix this.
The idea is that if a = 8 and the first digit in cell(i,c) = 6 or 4 then excel should so something.
Dim a as integer
Dim c as integer
If a = 8 And (CInt(Left(.Cells(i, c), 1)) = 6 Or CInt(Left(.Cells(i, c), 1)) = 4) Then```
If the content of the cell you are reading is either empty or don't start with a digit, CInt
will throw that runtime error 13.
You could use the Val
-function instead (will return 0 for non-numeric strings), or you could check for the character "6"
resp. "4"
instead. I also would suggest to use an intermediate variable to save the character you are checking, makes the code much more readable.
dim firstChr as string
firstChr = Left(.Cells(i, c))
If a = 8 And (val(firstChr) = 6 Or val(firstChr) = 4) Then
' or
If a = 8 And (firstChr = "6" Or firstChr = "4") Then
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.