2017-09-05 52 views
0

我創建了一個宏,將填充多個字段基於一個下拉選擇一個電子表格,例如:如何從相鄰下拉列表中的單元格中輸入數據時刪除填充顏色?

在列L,我有一個下拉的兩個項目名單,「YES」和「NO」 。當選擇項目時,在相鄰的兩個單元將與預定的數據填充,例如:

選擇「Yes」將填充兩個相鄰細胞與黃色

選擇「否」,將填充在兩個相鄰的細胞與一句話,「NULL」

這裏就是我堅持..

當有人在「是」相鄰的兩個單元格中輸入數據,我需要的黃色填充走開。

當有人向這些單元格中輸入數據時,是否有辦法刪除黃色填充?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 

Application.ScreenUpdating = False 

Select Case Target 

Case "YES" 
    If Target = "YES" Then 
     Target.Offset(0, 1).Interior.ColorIndex = 6 
     Target.Offset(0, 2).Interior.ColorIndex = 6 
      If Not Target.Cells.Count = 1 Then 
       Exit Sub 
        If Intersect(Target, Columns(2)) Is Nothing Then 
         Exit Sub 
        End If 
      End If 
    End If 
Case Else 
    If Target = "NO" Then 
     Target.Offset(0, 1) = "NULL" 
     Target.Offset(0, 2) = "NULL" 
      If Not Target.Cells.Count = 1 Then 
       Exit Sub 
        If Intersect(Target, Columns(2)) Is Nothing Then 
         Exit Sub 
          If Intersect(Target, Columns(2)) Is Nothing Then 
           Exit Sub 
          End If 
        End If 
      End If 
    End If 
End Select 
End Sub 

回答

0

嘗試:

If Target = "NO" Then 
    Target.Offset(0, 1) = "NULL" 
    Target.Offset(0, 1).Interior.ColorIndex = xlColorIndexNone 
    Target.Offset(0, 2) = "NULL" 
    Target.Offset(0, 2).Interior.ColorIndex = xlColorIndexNone 
+0

謝謝,@Cyril這正是我需要的,雖然當我清除值的範圍的內容,運行時錯誤「13」:類型不匹配窗口出現。有沒有辦法來防止這種情況發生? – Nick

+0

我不相信我遵循你的意思。你如何清理內容?我會想象你有一個不正確的對象,或者對象類型不正確。如果此答案已解決您發佈的問題,請標記爲已回答。如果您有任何問題,我可以嘗試提供幫助,但請考慮是否值得將其作爲另一個問題發佈(如果我沒有答案,而其他人則傾向於檢查新發布的項目)。 – Cyril

+0

忘記@NickRivera最後一個。 – Cyril

相關問題