如何在VBA Excel中将文本更改为数字

如何在VBA Excel中将文本更改为数字,vba,excel,types,compiler-errors,Vba,Excel,Types,Compiler Errors,我想将单元格内容转换为数值,以便比较值 If Cells(r, c) >= Cells(157, c) Then Cells(i, c) = Cells(r, 3) 我需要关于如何做这件事的帮助 Sub comp_above_median() For c = 6 To 19 i = 160 For r = 2 To 155 If Cells(r, c) >= Cells(157, c) Then Cells(i, c) = Cells(r, 3)

我想将单元格内容转换为数值,以便比较值

If Cells(r, c) >= Cells(157, c) Then Cells(i, c) = Cells(r, 3)
我需要关于如何做这件事的帮助

Sub comp_above_median()

For c = 6 To 19
 i = 160

    For r = 2 To 155
        If Cells(r, c) >= Cells(157, c) Then Cells(i, c) = Cells(r, 3)
        If Cells(r, c) >= Cells(157, c) Then i = i + 1

    Next r

Next c

End Sub
我收到运行时错误13:行的类型不匹配

If Cells(r, c) >= Cells(157, c) Then Cells(i, c) = Cells(r, 3)

您必须结束if语句,并在另一行中编写if过程:

    If Cells(r, c) >= Cells(157, c) Then 
        Cells(i, c) = Cells(r, 3)
        i = i + 1
    End if
试试这个

    Sub comp_above_median()
    For c = 6 To 19
     i = 160
        For r = 2 To 155
            If Val(Cells(r, c)) >= Val(Cells(157, c)) Then
            Cells(i, c) = Cells(r, 3)
            i = i + 1
            End If
        Next r
    Next c
    End Sub

这个数字是字符串吗?如果是这样,您可以使用CInt()。@lowpar我不确定,如何检查?@lowpar添加CInt()后,条件没有给出正确的结果,它总是给出正确的结果。下次,将值放入msgbox以查看它是什么,在尝试和做简单的算术运算后,看看它是否是一个数字。是的,我很糟糕,我错过了它。我尝试了同样的方法,但条件行总是返回true,如果CInt(Cells(r,c))>=CInt(Cells(157,c)),那么你能尝试
Val
而不是
CInt