根据同一行中的其他单元格值更改行内的单元格值

时间:2013-12-05 20:29:40

标签: excel vba

我正在创建一个宏启用电子表格,以帮助我跟踪我目前在工程职位上工作的支持服务单。

我有16列提供有关每个支持服务单的信息,例如日期,设备的序列号,设备位置等。新行是新的支持票。

假设我们正在使用第50行。

列E表示已打开故障单的设备的状态。例如Deployed,Configure和Floor Install。

列F表示设备类型,例如Exalogic和Exadata

K列代表我是否需要根据E“OR”的值来创建特殊票证。列K的值可以是是或否。

这是我到目前为止所做的,它适用于E列范围。

Dim rng As Range 
Dim cellE 
Dim cellF 

If Not Intersect(Range("E12:E100"), Target) Is Nothing Then 
Application.EnableEvents = False 

For Each rng In Intersect(Range("E12:E100"), Target) 

    Set cellE = Range("E" & rng.Row) 
    Set cellK = Range("K" & rng.Row) 

    If Range("E" & rng.Row) = "Configure" Then 
        Range("K" & rng.Row) = "No" 
    ElseIf Range("E" & rng.Row).Value Like "*Floor*" Then 
        Range("K" & rng.Row) = "No" 

    Else 
        Range("K" & rng.Row) = "" 
    End If 

Next rng 
Application.EnableEvents = True 

End If 

但是,如果我尝试添加F范围,则无效:

If Not Intersect(Range("E12:E100"), Range("F12:F100"), Target) Is Nothing Then 
Application.EnableEvents = False 

For Each rng In Intersect(Range("E12:E100"), Range("F12:F100"), Target) 

    Set cellE = Range("E" & rng.Row) 
    Set cellF = Range("F" & rng.Row) 
    Set cellK = Range("K" & rng.Row) 

    If Range("E" & rng.Row) = "Configure" Then 
        Range("K" & rng.Row) = "No" 
    ElseIf Range("E" & rng.Row).Value Like "*Floor*" Then 
        Range("K" & rng.Row) = "No" 
    ElseIf Range("F" & rng.Row).Value Like "*Exa*" Then 
         'Range("K" & rng.Row) = "Not Needed"
    Else 
        Range("K" & rng.Row) = "" 
    End If 

Next rng 
Application.EnableEvents = True 

End If 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

为了更好地解释我的评论:

列E从不与列F相交。

所以你的相交总会失败,你正在寻找三个范围的交集,当你想要目标与两者中的任何一个的交集时,你可以使用:

If Not Intersect(Range("E12:F100"), Target) Is Nothing Then 
Application.EnableEvents = False 

For Each rng In Intersect(Range("E12:F100"), Target) 
    Dim curRow as long
    curRow = rng.Row
    Set cellE = Range("E" & curRow) 
    Set cellF = Range("F" & curRow) 
    Set cellK = Range("K" & curRow) 

    If cellE  = "Configure" Or cellE.Value Like "*Floor*" Then 
        cellK  = "No" 
    ElseIf cellF .Value Like "*Exa*" Then 
         'cellK  = "Not Needed"
    Else 
        cellK  = "" 
    End If 

Next rng 
Application.EnableEvents = True 

End If 

如果您需要非连续范围,也可以使用

`Intersect(Range("E12:E100","F12:F100"), Target)` 

因为这将两个范围合并为一个

If Not Intersect(Range("E12:E100","F12:F100"), Target) Is Nothing Then 
Application.EnableEvents = False 

For Each rng In Intersect(Range("E12:E100","F12:F100"), Target)
    Dim curRow as long
    curRow = rng.Row
    Set cellE = Range("E" & curRow) 
    Set cellF = Range("F" & curRow) 
    Set cellK = Range("K" & curRow) 

    If cellE  = "Configure" Or cellE.Value Like "*Floor*" Then 
        cellK  = "No" 
    ElseIf cellF .Value Like "*Exa*" Then 
         'cellK  = "Not Needed"
    Else 
        cellK  = "" 
    End If 

Next rng 
Application.EnableEvents = True 

End If