2013-04-29 59 views
2

我在這多行由VBA宏給出不同顏色的背景Excel表格。這些背景顏色應該鎖定到行。我的問題是,當表由一列或排序的數據重新排序另一個背景色移動。排序不動,格式化

我可以用另一種方式來阻止這種情況的發生格式化使細胞保持鎖定?

我使用的格式的代碼是:

For Each Row In rng.Rows 

If Condition Then 

    Row.Select 

    cIndex = ColourIndex(colour) 
    With Selection.Interior 
     .ColorIndex = cIndex 
    End With 

End If  
Next 

我的表的一個例子是這樣的:

enter image description here 編輯:額外的代碼

Sub Quota(ByVal Type As String) 

Dim records As Long 
Dim sht1 As Worksheet 
Set sht1 = Worksheets("Sheet1") 
Dim sht2 As Worksheet 
Set sht2 = Worksheets("Sheet2") 

records = sht1.Range("A1048576").End(xlUp).Row - 5 

Dim rng As Range 
Dim rngRowCount As Long 
Dim rLastCell As Range 
Dim i As Long 

sht2.Activate 

'Last used cell 
Set rLastCell = sht2.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _ 
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False) 
'All used columns except 1st 
Set rng = sht2.Range(Cells(2, 1), rLastCell) 
rng.Select 
rngRowCount = rng.Rows.CountLarge 

For i = 1 To rngRowCount 

Dim valueAs String 
Dim colour As String 
Dim VarX As Long 
Dim maxValue As Long 

value= sht2.Cells(i + 1, 1).Value 
colour = sht2.Cells(i + 1, 2).Value 

If Type = "A" Then 
    VarX = sht2.Cells(i + 1, 3).Value 
ElseIf Type = "B" Then 
    VarX = sht2.Cells(i + 1, 5).Value 
End If 

maxValue = (records/100) * VarX 

ColourRows value, colour, maxValue 

Next i 

End Sub 

Sub ColourRows(value As String, colour As String, maxValue As Long) 

Dim sht1 As Worksheet 
Set sht1 = Worksheets("Sheet1") 
sht1.Activate 

Dim rng As Range 
Dim firstSixRowsOnwards As Range 
Dim lastColumn As Long 
Dim usedColumns As Range 
Dim usedColumnsString As String 
Dim highlightedColumns As Range 
Dim rngDataRowCount As Long 
Dim performancevalueAs String 
Dim cIndex As Integer 
Dim count As Long 

count = 0 

Dim rLastCell As Range 

'End row 
rngDataRowCount = sht1.Range("A1048576").End(xlUp).Row 
'First 6 rows 
Set firstSixRowsOnwards = sht1.Range("A6:XFD1048576") 
'Last column 
lastColumn = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 
'Used Range 
Set rng = sht1.Range(Cells(1, 1), Cells(rngDataRowCount, lastColumn)) 
'Used Columns 
Set usedColumns = sht1.Range(Cells(1, 1), Cells(1048576, lastColumn)) 

Set rng = Intersect(rng, firstSixRowsOnwards, usedColumns) 

For Each Row In rng.Rows 

    compareValue= Cells(Row.Row, 5)).Value 

    If (InStr(1, value, compareValue, 1) Then 

     Dim rowNumber As Long 
     Row.Select 

     If count < maxValue Then 

      cIndex = ColourIndex(colour) 
      With Selection.Interior 
       .ColorIndex = cIndex 
      End With 

      count = count + 1 

     Else 

      cIndex = 3      'red 
      With Selection.Interior 
       .ColorIndex = cIndex 
      End With 

     End If 

    End If 

Next 

End Sub 
+0

你錯過了你的桌子......我F可顯示你所擁有的,你想有 – 2013-04-29 08:36:15

+0

所以在上面行3和4應該總是紅的例子,無論什麼排序,6-8總是很藍等... – db579 2013-04-29 08:40:43

+0

能否請你分享確切的VBA代碼你用來填寫行嗎?我想知道你評估的標準,以及你如何選擇顏色來填充行。我認爲這將幫助我們找到出路。 – MeenakshiSundharam 2013-04-29 08:55:21

回答

2

我相信,如果您可以通過列中選擇您的數據,然後排序(而不是排有限的範圍內),然後格式化將隨之而來。

編輯:

如果你想鎖定格式,然後使用基於行號,如條件格式ROW()= x或ROW()=值的範圍...

測試:按公式設置規則使用條件格式,例如= ROW()= 3確保excel不會雙引號給你,到整個數據範圍。第3行隨後將按照您在此處設置的格式進行設置。

設置在VBA

Sub test() 
    Range("A3").Select 

    With Range("A3") 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=ROW()=3" 
    .FormatConditions(1).Interior.ColorIndex = 46 
    End With 
End Sub 
+0

這對我不起作用 - 按列排序仍然會移動背景顏色 – db579 2013-04-29 08:39:40

+0

哦,我的不好,我誤解了你原來的帖子,並認爲這是你想要的 – 2013-04-29 08:42:24

+1

不用擔心,謝謝! – db579 2013-04-29 08:44:32

1

這裏,我們去:

在這種情況下,我會做的兩兩件事之一:

  1. 條件格式。需要大量的邏輯和手動步驟,因此讓我們離開它的。
  2. 宏:當你對數據進行排序,請激活一個功能

    Sub Option1() 
    Dim row As Range 
    Dim rowNum As Integer 
    Dim tRange As Range 
    
    'set range here: in your example, it is A2:D11 
    
    Set tRange = ActiveSheet.Range("A2:D11") 
    
    'clear colors 
    tRange.ClearFormats ' clears the previous format 
    
    rowNum = 1 
    
    For Each row In tRange.Rows 
    
        Select Case rowNum 
         Case 1, 2 
          row.Interior.Color = RGB(255, 255, 0) ' 1 and 2nd will be yellow 
         Case 3, 4 
          row.Interior.Color = 255 ' 3rd and 4th row will be red 
         Case 5, 6 
          row.Interior.Color = RGB(0, 0, 255) ' 5 and 6th row will be blue 
         Case Else 
          row.Interior.Color = RGB(0, 255, 0) '' all the bottom row would be a Green row 
        End Select 
        rowNum = rowNum + 1 
    Next row 
    End Sub 
    

幫助?

+0

我的問題稍微複雜一些,因爲我不知道哪些行會通過條件或者他們將事先填充哪些顏色 - 這是從電子表格中提取的 - 現在已經發布了更多的代碼,如果這有助於您幫助我 – db579 2013-04-29 09:13:58

+0

我嘗試使用row.Interior而不是像之前的selection.interior,但沒有幫助 – db579 2013-04-29 09:27:51

+0

此代碼不符合要求,如示例中所示。 – pnuts 2013-04-30 00:25:38

2

可與CF來完成,例如(頂部規則> 11):

SO16274258 example

編輯 - 我無意間忽略了一個規則如下

第二下使用=ROW($A1)=11

SO16274258 second example

+0

一旦代碼確定哪些行應該着色哪些顏色已經運行,是否有一種方法可以通過我的VBA代碼動態獲取該集合? – db579 2013-05-03 02:06:43

+0

我想我正在嘗試回答Q的早期版本。如果顏色是根據「哪個行號」以外的條件在VBA中設置的,那麼我認爲您需要更多的代碼才能按行捕獲顏色,然後重新應用這些顏色一種排序。上面我想到的是,你使用「其他」標準僅僅是爲了確定在CF中基於* row *的公式的格式選擇。即不要將Row2與代碼變成紅色,因爲它在ColumnA中具有'20',而不是忘記代碼,而是手動爲Row2選擇紅色(如果你願意,因爲它有'20')。 – pnuts 2013-10-26 01:28:50