2017-02-07 84 views
0

數我有下面的代碼檢查山坳K代表「星期日」的日期和「時間」,並與上校M.減去剩餘時間和比較VBA

這是什麼代碼做數字比較? :

例如,如果Col K中的日期/時間是2/5/2017 18:00:00,它應該減去剩餘的剩餘時間,即一天中的0.6小時結束,col中的數字M.如果Col M中的值大於1,則減法後應突出顯示,或者如果減法後小於1,則應將其着色爲紅色。

問題:

  1. 的代碼並在紅色不變色,如果在山口M中的值是在1.5,1.6,1.7 etc..Only的範圍,如果超過> = 2時,它開始着色在紅色..我該如何解決這個問題?
  2. 當前有兩個過程定義爲通過和失敗。我如何結合這一點?

    Sub MinusSunday() 
    Dim r, LastRow, RemainingDay As Double 
    
    LastRow = Range("M:O").Cells(Rows.Count, "A").End(xlUp).Row 
    
    Application.ScreenUpdating = False 
    
        For r = 2 To LastRow 
         RemainingDay = 0 
    
        If Weekday(Range("K" & r).Value, vbSunday) = 1 Then 
          RemainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h"))/24, 1) 
    
         If InStr(1, Range("O" & r).Text, "Pass", vbTextCompare) > 0 Then 
    
          If Range("M" & r) - RemainingDay >= 1 Then 
           Range("M" & r).Cells.Font.ColorIndex = 3 
          Else 
           Range("M" & r).Cells.Font.ColorIndex = 0 
          End If 
    
         End If 
        End If 
        Next r 
    
    
        For r = 2 To LastRow 
         RemainingDay = 0 
    
        If Weekday(Range("K" & r).Value, vbSunday) = 1 Then 
          RemainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h"))/24, 1) 
    
         If InStr(1, Range("O" & r).Text, "Fail", vbTextCompare) > 0 Then 
    
          If Range("M" & r) - RemainingDay >= 1 Then 
           Range("M" & r).Cells.Font.ColorIndex = 3 
          Else 
           Range("M" & r).Cells.Font.ColorIndex = 0 
          End If 
    
         End If 
        End If 
         Next r 
         End Sub 
    
+0

等一下,你說*'2/5/2017 18:00:00'剩下的時間,即0.6小時*? 6小時不是0.6小時,不是? –

+0

即使「通過」或「失敗」,如果您想將列M中的單元格變爲紅色(如果當天還剩餘1小時以上),那麼@stack標記? –

+0

我試圖瞭解'RemainingDay'的計算。它是什麼單位? M列中的究竟是什麼? Data和Calc的一個例子會很有用。 –

回答

1

RemainingDay = Round((24 - Format(TimeValue(Range("K" & r).Value), "h"))/24, 1)收益從0到1(你的例子返回0.2)剩餘的一天的值。

所以,在運行它時,如果列M> = 1.3中的值,它將使該單元格中的字體以紅色着色。

我有一個Select Case與一個小「竅門」來結合你的兩個程序。

注意:由於您使用RemainingDay得到的分數剩下一天的時間(從0到1)的值,你可以只使用:

RemainingDay = 1 - TimeValue(Range("K" & r).Value) 

(這個目前尚未實現在代碼中,等待PO反饋)。

要獲得RemainingDay以小時爲單位,您可以使用:

RemainingDay = 24 * (1 - TimeValue(Range("K" & r).Value)) 

代碼

Option Explicit 

Sub MinusSunday() 

Dim r As Long, LastRow As Long, RemainingDay As Double 

With Worksheets("Latency") 
    LastRow = .Range("M:O").Cells(.Rows.Count, "A").End(xlUp).Row 

    Application.ScreenUpdating = False 

    For r = 2 To LastRow 
     RemainingDay = 0 

     If Weekday(.Range("K" & r).Value, vbSunday) = 1 Then 
      ' returns the RemainindDay value in part of days (rounded) 
      RemainingDay = Round((24 - Format(TimeValue(.Range("K" & r).Value), "h"))/24, 1)            
      ' Use Select case "Trick" for both cases 
      Select Case True 
       Case .Range("O" & r).Text Like "Pass", .Range("O" & r).Text Like "Fail"      
        ' ===== Line below Just for DEBUG ===== 
        .Range("L" & r).Value = .Range("M" & r) - RemainingDay 

        If .Range("M" & r) - RemainingDay >= 1 Then 
         .Range("M" & r).Cells.Font.ColorIndex = 3 
        Else 
         .Range("M" & r).Cells.Font.ColorIndex = 0 
        End If 

       Case Else 
        ' currently do Nothing, maybe for the future ? 

      End Select 
     End If 
    Next r 
End With 

End Sub 

運行此代碼返回下面的結果(參見我在加入調試列「L」):

enter image description here

+0

你是怎麼理解這個計算的。好樣的! :P –

+0

@ASH LOL,今天工作的空閒時間太多:) –

+0

順便說一下,在我看來,'Round((24格式(TimeValue(Range(「K」&r).Value),「h」) )/ 24,1)'''''''''''''''''''''''''''''''只有一個猜測,我不知道它做了什麼。 –