简体   繁体   English

Excel / VB-如何遍历每个行/列,并根据值进行格式化?

[英]Excel / VB - How do I loop through each row/column and do formatting based on the value?

Here's what I need to do: 这是我需要做的:

1) Loop through every cell in a worksheet 2) Make formatting changes (bold, etc) to fields relative to each field based on the value 1)遍历工作表中的每个单元格2)根据值对与每个字段相关的字段进行格式更改(粗体等)

What I mean is that if a field has a value of "foo", I want to make the field that is (-1, -3) from it bold, etc. I tried to do this with the following script with no luck. 我的意思是,如果一个字段的值为“ foo”,我想将其变为(-1,-3)的字段为粗体,以此类推。我尝试使用下面的脚本来做到这一点,但没有成功。

Thanks Johnny 谢谢约翰尼

Pseudo Code to Explain: 伪代码说明:

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

The Failed Macro (I don't really know VB at all): 失败的宏(我一点也不了解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
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

I added two functions but it should have also worked with an extensive if / else if / else. 我添加了两个函数,但是它应该也可以与广泛的if / else if / else一起使用。

Based on the comments on the solution above, i think this might be helpful 根据对上述解决方案的评论,我认为这可能会有所帮助

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

This does not fully accomplish your goal. 这不能完全实现您的目标。 What it does is it searches a specified list of cells, and it matches them against a seperate list of cells. 它的作用是搜索指定的单元格列表,并将它们与单独的单元格列表进行匹配。 If it matches the values, it takes the FORMAT of the second list of cells and applies it to the cell it matched in the first list of cells. 如果与值匹配,则采用第二个单元格列表的格式并将其应用于第一个单元格列表中匹配的单元格。 You can modify this by changing the TryMatchValue function so that instead of matching the CellToTest, it pastes the format onto another cell which is 2 across and one up. 您可以通过更改TryMatchValue函数来进行修改,以使其不匹配CellToTest,而是将格式粘贴到另一个2格一格的单元格上。

This has the advantage that, if you want to add more values and different formats, you only need to go to your excel sheet and add more values. 这样做的好处是,如果要添加更多的值和不同的格式,则只需转到excel工作表并添加更多的值。 Also you only need to change the format on that value. 同样,您只需要更改该值的格式。

An example would be... 一个例子是...

Have the cells you are searching in A1:D1000 Have these values in cells E2:E6... Subtotal (which is bold and underlined) Total (which is bold, underlined and italic) Net (which is bold underlined and Red) etc... 在A1:D1000中具有要搜索的单元格在单元格E2:E6中具有这些值...小计(粗体和下划线)总计(粗体,下划线和斜体)净额(下划线和红色粗体)等。 ..

then when it hits Subtotal, it will change the cell to be bold and underlined. 然后点击小计时,会将单元格更改为粗体和下划线。 When it hits Total it will change the cell to be bold underlined and italic etc etc... 当它达到Total时,会将单元格更改为加粗下划线和斜体等。

hope this helps 希望这可以帮助

excel中的条件格式设置功能是否可以为您提供所需的内容而无需编写宏?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历特定列中的单元格并根据其内容删除整行? - How do I loop through cells in a specific column and delete the entire row based on its contents? 如何根据列和行的选择从Excel表中选择特定值? - How do I select specific value from Excel table based on a column and row selection? 如何在Excel中将列转置为行 - How do I transpose a column into a row in Excel (C#-Excel)如何为每个现有行的新列写一个公式? - (C# - Excel) How do I write a formula to a newly created column for each existing row? 如何在excel中创建一个公式来搜索行中的值,然后从该列中的另一个单元格中获取值? - How do I create a formula in excel that searches for a value in row, and then takes the value from another cell in that column? 我如何遍历一列,如果单元格符合某些条件,则将整行粘贴到另一个工作表中 - How do I loop through a column, if a cell matches some criteria then paste that whole row into another worksheet 使用Excel表和VBA在动态列的每一行中循环 - Loop through each row in dynamic column using Excel Tables and VBA 如何根据每个单元格的值和固定单元格的值有条件地格式化VBA中的列? - How do I conditionally format a column in VBA based on each cell's value and a fixed cell's value? 如何遍历每一列并删除Excel(VB)中的重复项 - How to go through each column and remove duplicates in Excel (VB) 如何在VBA中为每个循环获取索引(使用Excel编程)? - How do I get the index in a VBA For Each Loop (programming with Excel)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM