Excel / VBA - 如何循环每个行/列并根据值进行格式化?

时间:2010-04-12 21:18:13

标签: excel-vba vba excel

这就是我需要做的事情:

1)遍历工作表中的每个单元格 2)根据值

对相对于每个字段的字段进行格式更改(粗体等)

我的意思是,如果一个字段的值为“foo”,我想从它的粗体等字段(-1,-3)开始。我试着用下面的脚本做没有运气。

由于 约翰尼

伪代码解释:

For Each Cell in WorkSheet
    If Value of Cell is 'Subtotal'
        Make the cell 2 cells to the left and 1 cell up from here bold and underlined
    End If
End ForEach

失败的宏(我根本不认识VB):

Sub Macro2()
'
'
'
Dim rnArea As Range
Dim rnCell As Range

Set rnArea = Range("J1:J2000")

    For Each rnCell In rnArea
        With rnCell
            If Not IsError(rnCell.Value) Then
                Select Case .Value
                    Case "000 Total"
                        ActiveCell.Offset(-1, -3).Select
                        ActiveCell.Font.Underline = XlUnderlineStyle.xlUnderlineStyleSingleAccounting
                End Select
            End If
        End With
    Next
End Sub

3 个答案:

答案 0 :(得分:1)

Option Explicit

Private Sub macro2()
    Dim rnArea As Range
    Dim rnCell As Range

    ' you might need to change the range to the cells/column you want to format e. g. "G1:G2000" '
    Set rnArea = Range("J1:J2000")

    For Each rnCell In rnArea
        With rnCell
            If isBold(.Offset(1, 3).Value) Then
                .Font.Bold = True
            End If
            If isUnderlined(.Offset(1, 3).Value) Then
                'maybe you want this: .Font.Underline = xlUnderlineStyleSingle '
                .Font.Underline = xlUnderlineStyleSingleAccounting
            End If
        End With
    Next
End Sub

Private Function isBold(cellValue As Variant) As Boolean
    Dim myList() As Variant
    Dim listCount As Integer
    Dim i As Integer

    myList = Array("Totals", "FooTotal", "SpamTotal")
    listCount = 3

    isBold = False
    For i = 0 To listCount - 1
        If cellValue = myList(i) Then
            isBold = True
            Exit Function
        End If
    Next i
End Function

Private Function isUnderlined(cellValue As Variant) As Boolean
    Dim myList() As Variant
    Dim listCount As Integer
    Dim i As Integer

    myList = Array("FooTotal", "SpamTotal")
    listCount = 2

    isUnderlined = False
    For i = 0 To listCount - 1
        If cellValue = myList(i) Then
            isUnderlined = True
            Exit Function
        End If
    Next i
End Function

我添加了两个函数,但它也应该使用广泛的if / else if / else。

答案 1 :(得分:1)

基于对上述解决方案的评论,我认为这可能会有所帮助

Sub FormatSpecialCells()
    Dim SearchRange As Range
    Dim CriteriaRange As Range

    Set SearchRange = Range("A2:A24")
    Set CriteriaRange = Range("C2:C5")

    Dim Cell As Range

    For Each Cell In SearchRange
     TryMatchValue Cell, CriteriaRange
    Next


End Sub

Private Sub TryMatchValue(CellToTest As Range, CellsToSearch As Range)
    Dim Cell As Range

    For Each Cell In CellsToSearch
        If Cell.Value = CellToTest.Value Then
            Cell.Copy
            CellToTest.PasteSpecial xlPasteFormats, xlPasteSpecialOperationNone, False, False
        End If
    Next
End Sub

这并不能完全实现你的目标。它的作用是搜索指定的单元格列表,并将它们与单独的单元格列表进行匹配。如果它与值匹配,则采用第二个单元格列表的FORMAT,并将其应用于在第一个单元格列表中匹配的单元格。您可以通过更改TryMatchValue函数来修改它,以便它不是匹配CellToTest,而是将格式粘贴到另一个2和1之间的单元格上。

这样做的好处是,如果要添加更多值和不同格式,只需转到Excel工作表并添加更多值。此外,您只需要更改该值的格式。

一个例子是......

在A1:D1000中搜索您要搜索的单元格 在单元格E2:E6中有这些值... 小计(粗体和下划线) 总计(粗体,下划线和斜体) 网(粗体下划线和红色) 等...

然后当它达到小计时,它会将单元格更改为粗体和下划线。 当它达到Total时,它会将单元格更改为粗体下划线和斜体 等等...

希望这会有所帮助

答案 2 :(得分:1)

excel中的conditional formatting功能是否可以为您提供所需而无需编写宏?