2017-06-21 114 views
0

我想將單元格內容轉換爲數值,以便我可以比較值。如何在VBA Excel中將文本更改爲數字

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) 
+0

數字是一個字符串嗎?如果是這樣,你可以使用CInt()。 – Lowpar

+0

@lowpar我不確定,如何檢查? –

+0

@lowpar在添加了cint()之後,條件沒有給出正確的結果,它總是在下一次給出正確的 –

回答

0

試試這個

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 
+0

我試過這個相同的東西,但條件行總是返回true如果CInt(Cells(r,c))> = CInt(Cells(157,c))然後 –

+0

你可以試試'Val'而不是'CInt'? – Pspl

+0

@pspl Val一個工作 –

0

你必須結束,如果語句和寫另一行中的if程序:

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

是的,我錯了,我錯過了它 –

相關問題