如何根据Excel中的单元格更改更新日期?

时间:2018-02-24 21:14:17

标签: excel vba excel-vba

这是一个小背景。在陆军航空兵中,我们必须至少每60天进行一次夜间护目镜(NG)飞行。此日期是从上次进行NG航班时收集的,以便使用该模式显示和跟踪货币。

An image to help with my question

上面链接的图片作为参考(实际图书的屏幕截图),我试图在另一个标签(显示面板)上使用基于INPUT的DATE(B COLUMN)(B28)更新日期(> ;或= 1.0)放置在CELL(P COLUMN)(P28)中。

有了这个,每当在同一列中进行另一个NG小时输入时,如果上述情况适用,它将再次执行该脚本。

我试过敲了一些VBA线,但我没有运气。

2 个答案:

答案 0 :(得分:0)

这是我认为你需要的shell,它位于与你监视P列的P表相关联的代码窗格中...

如果P列发生变化且值>> 1

,则会触发

还有一点逻辑。如果新值大于现有值,我只更新C32。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim updateCell As Range
Dim lastDateCell As Range
Set updateCell = ThisWorkbook.Worksheets("Display Panel").Range("C32")
Set lastDateCell = ThisWorkbook.Worksheets("Display Panel").Range("AA32")

If Target.Column = 16 And Target.Value >= 1 Then

       If Target.Offset(, -14) > updateCell Then 

           lastDateCell  = updateCell 
           updateCell = Target.Offset(, -14) 'only update if greater than existing

       End If

End Sub 

Flight Log表格的代码窗格中的代码:

Flight log code window with code

答案 1 :(得分:0)

(Posted the solution on behalf of the question author).

Below is what I added.

Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Flight Log").Unprotect "Password Here" 'This unprotects the sheet to allow changes

Dim updateCell As Range
Dim lastDateCell As Range
Set updateCell = ThisWorkbook.Worksheets("Computations").Range("A36") 'This is the primary date location
Set lastDateCell = ThisWorkbook.Worksheets("Computations").Range("A40") 'This is the backup date location

If Target.Column = 16 And Target.Value >= 1 Then 'This check P column for the value

       If Target.Offset(, -14) > updateCell Then 'If "P" returns the value, date in "B" is grabbed

           lastDateCell = updateCell 'This updates the primary date cell
           updateCell = Target.Offset(, -14) 'This ensures date is not overridden by lesser value

       End If

End If
Sheets("Flight Log").Protect "Password Here" 'This line reprotects the sheet
End Sub

Additionally, I use two LOOKUP functions to evaluate the HOUR(s) in reference to the dates grabbed by the VBS:

=IFERROR(LOOKUP(A36,'Flight Log'!B:B,'Flight Log'!P:P),"/// NG DATE ERROR ///")

and,

=IFERROR(LOOKUP(A40,'Flight Log'!B:B,'Flight Log'!P:P),"/// NO B/U DATE ///")

after this, I use another IF function to verify the HOUR wasn't miss-entered

=IF(A37>=1,A36,IF(A41>=1,A40,"NG HOUR ERROR"))

The simple IF functions check and verify the HOUR wasn't entered incorrectly by the Pilot--which would prompt the VBS script to grab that DATE--and then re-entered to it's intended "correct" value. For instance, "Pilot enters time into block, pilots realizes time he entered was wrong, and corrects it." This will keep his error, from being an overall error.