如何使用vb.net为excel中的单元格着色?

时间:2015-02-13 15:16:53

标签: vb.net excel

如何垂直着色细胞?示例我想为单元格中的所有数据着色" I"直到最后。

这是我到目前为止所做的:

For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                xlSheet.Cells(i + 8, j + 1) =
                ds.Tables(0).Rows(i).Item(j)
                formatRange = xlSheet.Cells(i + 8, j + 1)
                formatRange.BorderAround(Excel.XlLineStyle.xlDash, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
                formatRange = xlSheet.Cells(i + 8, 9 + j)
                formatRange.Interior.Color = system.Drawing.ColorTranslator.ToOle(system.Drawing.Color.Red)
            Next
        Next

2 个答案:

答案 0 :(得分:0)

这不适合你:

xlSheet.Range("I:I").Interior.Color = System.Drawing.ColorTranslator.ToOle(system.Drawing.Color.Red)

答案 1 :(得分:0)

这将为你做。

Sub ColorCol(ColID As String, R As Long, G As Long, B As Long)
Dim LastRow As Long
Dim Rr As Range
With ThisWorkbook.ActiveSheet
LastRow = .Cells(.Rows.Count, ColID).End(xlUp).Row
Set Rr = .Range(ColID & "1", ColID & LastRow)
For Each Cell In Rr.Cells
Cell.Interior.Color = RGB(R, G, B)
Next
End With
End Sub
Sub Main()
ColorCol "A", 255, 0, 0
End Sub

从另一个子

调用它
Sub Main()
ColorCol "A",255,0,0
End Sub

Nota:适用于活动表