我的Excel的VBA控件不是完全的死循环,而是希望某单元格的内容更变(数据有效性)或按另一个控件按钮来决定几时暂停。
现在运行的时候占据了所有的资源,就是不接受响应了好像有些Excel游戏能解决这种资源占用的问题,有谁能帮我解决吗?

解决方案 »

  1.   

    For i = 1 To 100000000 '假设你有个很大的循环
     ...
     DoEvents ' 加上这个
    Next
      

  2.   

    就是让一个程序每秒执行一次
    然后其他时间让该 xls接受响应
      

  3.   

    (1)可以使用定时器
    (2)DoEvents + Sleep + 时间判断实现延时
      

  4.   


    用sleep的时候是不是接受响应呀
      

  5.   

    Function tradetime()
        Dim nowtime As Single
        
      nowtime = (Now() - Cells(2, 6)) * 24
      
      if (nowtime<=9) or (nowtime>=15.5) or ((nowtime>11.5) and( nowtime<13))
      then tradetime=false
      Else: tradetime = True
      End If
      
    End Functionif和then两行总是红色
      

  6.   

    发现5楼的问题是then else要写在同一行,这太不舒服了吧
      

  7.   


    Function tradetime()
      Dim nowtime As Single
        
      nowtime = (Now() - Cells(2, 6)) * 24
       
      if (nowtime<=9) or (nowtime>=15.5) or ((nowtime>11.5) and( nowtime<13)) then
        tradetime=false
      Else 
        tradetime = True
      End If
       
    End Function
      

  8.   

    控制循环的频率.比如,在循环体内增加一句sleep 1,则理论上这个循环最多每秒1000次,如果这种频率足够你使用,就行了.这样还能减少CPU占用.这与游戏的FPS限制是一样的,要是不限制,CPU占用也会非常高,而且也没必要.
      

  9.   

    这是语法格式的要求,你只能‘遵守规则’。你想分行写,就照这样:
    Function tradetime()
        Dim nowtime As Single
        nowtime = (Now() - Cells(2, 6)) * 24
        If (nowtime <= 9) Or (nowtime >= 15.5) Or ((nowtime > 11.5) And (nowtime < 13)) _
            Then
                tradetime = False
            Else
                tradetime = True
        End If
    End Function
      

  10.   

    我一般的处理方法是在循环中假如一个DoEvents,这样就可以防止程序假死。
      

  11.   

    各位好,我的是Vista noSP 和Office2007
    在sleep的时候仍然是无法响应
      

  12.   

    office2007没用过, 不过DoEvents应该可以解决问题吧
      

  13.   

    每次循环都用DoEvents将控制权交还系统
      

  14.   

    对,我用了和sleep和Doevents,虽然图标还是在计算的样子,但是如果点到另一个停止的按钮是有用的
    等东西做完,大概周三吧,来给大家结贴,谢谢大家的热心帮助。