Excel VBA 返回单元格的值

Excel VBA returing a value of a cell

希望有人能够帮助我解决这个问题。我对 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

没什么作用,满足条件就弹出消息框。我希望做的是将消息框中的消息与来自同一行但不同列的单元格的 return 值一起。我用 *** 代码突出显示了当前 returns 活动单元格的地址,但正如我之前解释的那样,我希望这是来自同一活动行但来自列 [=9] 的不同单元格的值=].我曾尝试使用范围功能,但没有用。有人可以帮我解决这个问题吗?

谢谢您,我们将不胜感激。

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

因为您正在检查每个 Cell,那么变量 Cell 就是您的范围。所以你会写:

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

或者,如果您总是需要 F 列,您可以使用不同的方法:

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

这就是你想要的?

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