2017-03-24 57 views
0

我正在測試下面的Worksheet_Event代碼。它正在做一些奇怪的事情。這些基本上是三個部分的代碼:Worksheet_Event:如何查找單元格和偏移量1和2列

#1) Enter "EXPL-" & Target.Value 

#2) Find a cell in Column R with this language: 
"Out of Standard (Comment Needed)" 
Then, move over one column, and highlight cell. 
Then, go over 1 more column form there, and highlight cell. 

#3) Conditional Format for missing values 

好像#1和#3做工精細,但#2不突出柱S或列的T細胞,它從過突出的下一個單元格活動細胞。活動單元格應該是在列R.

Private Sub Worksheet_Change(ByVal Target As Range) 

'#1) 
    If Target.Column <> 1 Then Exit Sub 

    On Error GoTo ErrHandler 
    Application.EnableEvents = False 
    If Target.Column = 1 Then 
    If IsNumeric(Target.Value) Then 
     Target.Offset(0, 0).Value = "EXPL-" & Target.Value 
    End If 
    End If 
ErrHandler: 
Application.EnableEvents = True 

'#2) 
lRow = Range("R" & Rows.Count).End(xlUp).Row 
Set MyRows = Range("R19:R" & lRow) 
For Each cell In MyRows 
On Error Resume Next 
    If cell.Value = "Out of Standard (Comment Needed)" Then cell.Address.Select 
     Selection.Offset(0, 1).Select 
      With Selection.FormatConditions(1).Interior 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorAccent2 
       .TintAndShade = 0.399945066682943 
      End With 
    If cell.Value = "Out of Standard (Comment Needed)" Then cell.Address.Select 
     Selection.Offset(0, 1).Select 
      With Selection.FormatConditions(1).Interior 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorAccent2 
       .TintAndShade = 0.399945066682943 
      End With 
Next 

'#3) 
lRow = Range("B" & Rows.Count).End(xlUp).Row 
Set MyRows = Range("B19:B" & lRow) 
MyRows.Select 
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ 
    "=ISBLANK(B19)" 
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
With Selection.FormatConditions(1).Interior 
    .PatternColorIndex = xlAutomatic 
    .ThemeColor = xlThemeColorAccent2 
    .TintAndShade = 0.399945066682943 
End With 
Selection.FormatConditions(1).StopIfTrue = False 

End Sub 
+0

在Change事件上運行的代碼正在對工作表進行更改,當然這個工作表稱爲更改事件,導致出現「奇怪的事情」。要做的事是在代碼開始運行時禁用事件,並且只有在完成所有更改(Application.EnableEvents = True)後才啓用它們。除此之外,我推薦使用Range對象而不是Selection對象,比如MyRows.Formatconditions.Add,而沒有任何Select操作。 – Variatus

回答

1

關於你的代碼(太長,把儘可能評論)幾點意見:

您已經有了:

If Target.Column <> 1 Then Exit Sub 

所以有一點再次使用下面的線(否則你會退出子):

If Target.Column = 1 Then 

在你的情況下儘量避免我們因爲它只會跳過錯誤而不是處理錯誤。

沒有必要使用cell.Address.SelectSelection.Offset(0, 1).SelectSelection,而是使用完全合格Range,並使用它的OffestResize性能。

因此,而不是使用3線以下兩次:

If cell.Value = "Out of Standard (Comment Needed)" Then cell.Address.Select 
    Selection.Offset(0, 1).Select 
     With Selection.FormatConditions(1).Interior 

你可以使用:

With Cell.Offset(, 1).Resize(1, 2).FormatConditions(1).Interior 
+0

謝謝大家! – ryguy72

2

我不知道如果這正是你正在嘗試做還是不做。讓我知道你的想法。所以這種方法有幾個問題,但我想只關注你提到的兩個單元格。

正如其他人所說,我不會使用選擇。我將在for循環中引用單元格變量的偏移量。

此外,如果R列包含「Out of Standard(需要評論)」,那麼您有兩個if語句似乎設置了選擇。這兩個if語句沒有封裝單元格格式化語句。這意味着每次都會執行單元格格式,而不考慮列R中的文本(不確定這是否是有意設計的)。我把它壓縮成一個if語句來封裝單元格格式。

例如

If 1=2 Then Debug.Print("1st") 
    Debug.Print("2nd") 

這相當於

If 1=2 Then 
    Debug.Print("1st") 
End If 
Debug.Print("2nd") 

第二總是在此聲明被印刷,因爲第二調試語句不是如果塊內。 1st永遠不會被打印,因爲1永遠不會等於2。

If 1=2 Then 
    Debug.Print("1st") 
    Debug.Print("2nd") 
End If 

在這個例子既不是第一或第二將永遠不會打印,因爲它們都屬於if塊內和1永遠等於2

最後,我可能是錯在這裏,但我注意到,您正在使用條件格式。也許你的條件格式是在你的工作表中,而不是在代碼中。它似乎沒有爲此範圍添加條件格式,因此我刪除了格式條件並僅設置了單元格的內部格式。

'#2) 
lRow = Range("R" & Rows.Count).End(xlUp).Row 
Set MyRows = Range("R19:R" & lRow) 
For Each cell In MyRows 
On Error Resume Next 
    If cell.Value = "Out of Standard (Comment Needed)" Then 
      With cell.Offset(0, 1).Interior 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorAccent2 
       .TintAndShade = 0.399945066682943 
      End With 

      With cell.Offset(0, 2).Interior 
       .PatternColorIndex = xlAutomatic 
       .ThemeColor = xlThemeColorAccent2 
       .TintAndShade = 0.399945066682943 
      End With 
    End If 
Next