如果不是值之间是行不通的。 VBA 在 excel

If not value between does not work. VBA in excel

我已经使用 Whosebug 解决了很多问题,但我找不到这个问题的答案。 If not state is not true, ever but even when it should be.

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

试试这个:

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。 属性