2016-09-28 29 views
1

我不太瞭解VBA。 Ivesleo回答了一箇舊帖子,名爲「基於價值變化填補整列」。 Link to Shade Row Post根據列C中值的變化對底部行進行底值處理,然後在列B中

我用他的代碼,它的工作原理,但我不知道如何改變代碼來修改它,我需要什麼。

  1. 當我們在某些城市(C列)安排項目時,我有幾周的列(B列)。現在,代碼突出顯示了B列或(第2列/周列)中每次更改的行。我需要在代碼中註明該城市每週的每一次變化,以便當我們去另一個城市時,比如說Piley,它不會突出顯示Piley B/C的W1N4,它首先注意到我們正在一個新城市。有點像2級的自定義排序,首先是城市,然後是本週。我不希望根據城市變化來突出顯示,只要注意城市發生了變化,然後根據周列中的價值變化突出顯示該城市。
  2. 你如何將行排到該行數據的末尾?我試過End(xlRight).Select,End(xlLeft).Select和End(xlUp).Row.Select,但它們不起作用。
  3. 顏色是他編造的東西嗎?我不明白它的作用,以及Excel如何知道如何處理它?再次,我是一個初學者。感謝您的幫助。

周/市

W1N1 Silverton 
W1N1 Silverton 
W2N3 Silverton 
W1N4 Silverton 
W1N4 Piley 
Sub color() 
    'Replace the 2 values of G = and C= by the number of the column containing _ 
    'the values being referenced. 
    Dim g As Long 
    Dim c As Integer 
    Dim colorIt As Boolean 

    g = 2 
    c = 2 
    colorIt = True 

    Do While Cells(g, c) <> "" 
     test_value = Cells(g, c) 
     Do While Cells(g, c) = test_value 
      If colorIt Then 
       Cells(g, c).EntireRow.Select 
       Selection.Interior.ColorIndex = 15 
      Else 
       Cells(g, c).EntireRow.Select 
       Selection.Interior.ColorIndex = x1None 
      End If 
      g = g + 1 
     Loop 
     colorIt = Not (colorIt) 
    Loop 
End Sub 

回答

1

下面的代碼已針對您的具體情況:

Sub color() 
    Dim r As Long 
    Dim colourIt As Boolean 
    Dim colour As XlColorIndex 

    colourIt = False 

    With ActiveSheet 
     r = 2 ' First row of data 

     Do While .Cells(r, "B").Value <> "" 
      'See if value has changed 
      If .Cells(r, "B").Value <> .Cells(r - 1, "B").Value Or _ 
       .Cells(r, "C").Value <> .Cells(r - 1, "C").Value Then 
       colourIt = Not colourIt 
      End If 

      'Determine which colour to use on this row 
      If colourIt Then 
       colour = 15 
      Else 
       colour = xlColorIndexNone 
      End If 
      'Apply the colouring 
      .Range(.Cells(r, "B"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.ColorIndex = colour 

      'Point to the next row of data 
      r = r + 1 
     Loop 
    End With 
End Sub 

注:變量colourIt(爲colorIt,但我是澳大利亞人,所以我打算用這個英文拼寫!!)只是一個布爾變量,因此可以取值TrueFalse。每次測試標準進行更改時,代碼都會在兩個可能的值之間切換(使用代碼colourIt = Not colourIt)。

海報的編輯:非常感謝。我只是想補充一下,我是如何改變它以適應我的情況的。我改變了它,如下所示:我使用RGB調色板將顏色更改爲淺橙色(我想這就是它所稱的),並將.Range(.Cells(r,「B」)等改爲.Range(。單元格(r,「A」)等等b/c它突出顯示了從列B開始的行與從列A開始。

我要在評論中發表進一步的感謝。

'Determine which colour to use on this row 
     If colourIt Then 
      colour = RGB(252, 228, 214) 
     Else 
      colour = xlColorIndexNone 
     End If 
     'Apply the colouring 
     .Range(.Cells(r, "A"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.color = colour 
+0

#YowE3K,非常感謝你的幫助,這真的很好!我在你的文章中只添加了一個編輯,我需要詳細說明我對代碼進行的一些修改以適應我的情況,我認爲把它放在我最初的帖子/問題中並不合適,我會將它標記爲答案並標記爲有用!再次感謝。 – sturdy267

相關問題