2016-09-26 49 views
-1

我已經使用下面的代碼完美工作,但我希望它顯示兩個或多個綠色(RGB(0,176,80))單元值在單獨的單元格中不同的列在同一行內,而不是在同一個單元格中與+號一起顯示?Excel VBA代碼編輯在單獨的列中顯示兩個或更多值

Option Explicit 

Sub main() 
    Dim row As ListRow 
    Dim icol As Long 
    Dim formula As String 

    For Each row In ActiveSheet.ListObjects("MyTable").ListRows 
     formula = "" 
     For icol = 1 To row.Range.Count - 1 
      With row.Range(1, icol) 
       If .Interior.Color = RGB(0, 176, 80) Then formula = formula & .value & "+" '.Address(False, False) 
      End With 
     Next icol 
     If formula <> "" Then row.Range(1, icol).value = Left(formula, Len(formula) - 1) 
    Next row 
End Sub 

回答

0

試試這個

Option Explicit 

Sub main() 
    Dim row As ListRow 
    Dim nCols As Long, icol As Long 
    Dim formula As String 
    Dim arr As Variant 

    With ActiveSheet.ListObjects("MyTable") '<--| reference your table 
     nCols = .ListColumns.Count - 1 '<-- "fix" its initial columns number since subsequent operations will increase it 
     For Each row In .ListRows 
      formula = "" 
      For icol = 1 To nCols 
       With row.Range(1, icol) 
        If .Interior.Color = RGB(0, 176, 80) Then formula = formula & .Value & "+" '.Address(False, False) 
       End With 
      Next icol 
      If formula <> "" Then 
       arr = Split(Left(formula, Len(formula) - 1), "+") '<--| fill an array with 'formula' string elements separated by "+" 
       row.Range(1, icol).Resize(, UBound(arr) + 1).Value = arr '<--| write array content into current row last column adjacent cells 
      End If 
     Next row 
    End With 
End Sub 
+0

輝煌一如既往。完美工作。評論也很好建議。 –

+0

不客氣 – user3598756

相關問題