首页 > 解决方案 > 如果不值之间不起作用。excel中的VBA

问题描述

我用 Stackoverflow 解决了很多问题,但我找不到答案。如果不是,那么状态就不是真的,即使它应该是。

dim Val as integer
    for i = 1 to 2
       Val=.Range("N" & i).Value 'Val is 0 for example

       Msgbox(Val) 'to debug what the value is and this example it is 0

       If not (300<=Val<=500) then 'this statement is never true even if Value=0
           'Do stuff
       End If

    next i

标签: excelif-statementbetweenvba

解决方案


试试这个:

for i = 1 to 2
   Val=.Range("N" & i).Value 'Value is 0 for example

   Msgbox(Val) 'to debug what the value is and this example it is 0

   If Val >= 300 And Val<=500 then ' check if Val is between 300 and 500
       'Do stuff
   End If

next i

将变量重命名为Val以区分它和.Value. 财产


推荐阅读