Excel VBA追踪一个单元格的值

希望有人能够帮助我。 我对VBA和编码是全新的。 这里是我的代码如下:

Private Sub Workbook_Open() For Each Cell In Range("I2:I500") If Cell.Value < Date - 9 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 22 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "Status 1 " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 22 Cell.Font.Bold = True End If ElseIf Cell.Value < Date - 5 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 55 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "Status 2 " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 55 Cell.Font.Bold = True End If ElseIf Cell.Value < Date - 4 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 41 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "Status 3 " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 41 Cell.Font.Bold = True End If ElseIf Cell.Value < Date - 2 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 33 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "Status 4 " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 33 Cell.Font.Bold = True End If Else Cell.Font.ColorIndex = 1 Cell.Font.Bold = False End If Next For Each Cell In Range("O2:O500") If Cell.Value < Date - 30 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 22 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "30 Days " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 44 Cell.Font.Bold = True End If ElseIf Cell.Value < Date - 60 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 55 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "60 Days " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 46 Cell.Font.Bold = True End If ElseIf Cell.Value < Date - 90 And Cell.Value <> "" Then If Cell.Font.ColorIndex <> 41 And Cell.Interior.Color <> RGB(151, 210, 86) Then MsgBox "90 Days " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 3 Cell.Font.Bold = True End If Else Cell.Font.ColorIndex = 1 Cell.Font.Bold = False End If Next For Each Cell In Range("L2:L500") If Cell.Value = "NO" Then MsgBox "Actione has not been taken " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 3 Cell.Font.Bold = True Cell.Font.Underline = True End If If Cell.Value = "YES" Then Cell.Font.ColorIndex = 1 Cell.Font.Bold = False Cell.Font.Underline = False End If Next For Each Cell In Range("N2:N500") If Cell.Value = "NO" Then MsgBox "Actione has not been taken " & ***Cell.Address(False, False)*** Cell.Font.ColorIndex = 3 Cell.Font.Bold = True Cell.Font.Underline = True End If If Cell.Value = "YES" Then Cell.Font.ColorIndex = 1 Cell.Font.Bold = False Cell.Font.Underline = False End If Next End Sub 

它没有太大的作用,只要满足条件就popup消息框。 我希望做的是与消息框内的消息一起返回来自同一行但不同列的单元格的值。 我用***代码高亮显示了当前返回一个活动单元格的地址,但正如我之前解释的,我希望这是来自同一活动行的不同单元格的值,而是来自列“F”。 我试图使用范围函数,但它没有工作。 有人可以帮助我吗?

谢谢你,任何帮助将不胜感激。

您将要使用Range.Offset()方法http://msdn.microsoft.com/en-us/library/office/ff840060%28v=office.15%29.aspx

因为你正在检查每个Cell ,所以variablesCell就是你的范围。 所以你会写:

 Cell.Offset(0, Number of columns offset from current location).Value 

如果您总是希望列F,则可以使用不同的方法:

 Range("F" & Cell.Row).Value 

这是你想要的?

Range("F" & Cell.row).Value