如果A列中的值为x,则突出显示行

时间:2017-04-28 01:35:52

标签: excel vba excel-vba conditional-formatting

如果import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TableIcon extends JPanel { public TableIcon() { Icon aboutIcon = new ImageIcon("about16.gif"); Icon addIcon = new ImageIcon("add16.gif"); Icon copyIcon = new ImageIcon("copy16.gif"); String[] columnNames = {"Picture", "Description"}; Object[][] data = { {aboutIcon, "About"}, {addIcon, "Add"}, {copyIcon, "Copy"}, }; DefaultTableModel model = new DefaultTableModel(data, columnNames) { // Returning the Class of each column will allow different // renderers to be used based on Class public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; JTable table = new JTable( model ); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane( table ); add( scrollPane ); } private static void createAndShowGUI() { JFrame frame = new JFrame("Table Icon"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TableIcon()); frame.setLocationByPlatform( true ); frame.pack(); frame.setVisible( true ); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

中的文字,如何突出显示单行的颜色

以第4行为例: 我最终想要得到的是,如果A列中的单元格为column A = X,则将行颜色从= X更改为黑色 Range("B4:N4")白色来自And Text.Color

最终,我希望它像Range("F4:N4"),但只有一行颜色。

到目前为止,这就是我正在使用的。

Range(Cells(i, "B"), Cells(LastRow, LastCol))

3 个答案:

答案 0 :(得分:1)

VBA条件格式。

Option Explicit

Sub Header()

    Dim sht2 As Worksheet
    Dim firstRow As Long, lastRow As Long, lastCol As Long

    'Application.ScreenUpdating = false
    On Error GoTo 0
    Set sht2 = ThisWorkbook.Worksheets("Email Form")
    firstRow = 4

    With sht2
        .Activate
        .Unprotect

        lastRow = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
        lastCol = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

        'black row, white text B:N
        With .Range(.Cells(firstRow, "B"), .Cells(lastRow, lastCol))
            'optionally remove any pre-existing CFRs
            .FormatConditions.Delete
            With .FormatConditions.Add(Type:=xlExpression, Formula1:="=UPPER($A4)=CHAR(88)")
                .Interior.ThemeColor = xlThemeColorLight1
                .Font.ThemeColor = xlThemeColorDark1
                .SetFirstPriority
                .StopIfTrue = False
            End With
        End With
        'don't display values from B:E
        With .Range(.Cells(firstRow, "B"), .Cells(lastRow, "E"))
            With .FormatConditions.Add(Type:=xlExpression, Formula1:="=UPPER($A4)=CHAR(88)")
                .NumberFormat = ";;;"
            End With
        End With

        'I tnhink you want to reProtect the worksheet here
        .Protect
    End With


    Application.ScreenUpdating = True
End Sub

enter image description here

答案 1 :(得分:0)

我认为您可以使用条件格式来实现目标:

您可以为两个不同范围的每种格式设置创建条件。

一次选择一个范围,然后从“主页”选项卡中创建新的条件格式规则,选择“使用公式”,然后输入如下公式:

=$A2="X"

请注意,在条件格式设置中使用相对/混合引用时,它将与您使用的范围中的第一个单元格进行比较。我已选择范围B2:N7来应用格式,因此需要创建混合引用,因为它应该应用于B2单元格。您无法看到它,但参考会自动更改同一范围内的所有其他单元格,就像您在其余范围内填充公式一样。例如,K5单元格的格式将取决于$ A5中的值(因为列引用是固定的,但行引用是动态的)。

然后为指定的范围设置所需的背景颜色或字体颜色。此条件将检查相应行的A列。

Add the Firebase Admin SDK to your Server

答案 2 :(得分:0)

我重新编写了一些代码,并添加了评论以告诉您原因。但总的来说,我遵循了原来的方法。

Sub Header()

    Dim Sht2 As Worksheet
    Dim LastRow As Long, LastCol As Long
    Dim IsBlack As Boolean, FillPattern As Long
    Dim Rng As Range
    Dim R As Long

'    Set sht2 = ThisWorkbook.Worksheets("Email Form")
    Set Sht2 = ThisWorkbook.Worksheets("Taylor")
'    On Error GoTo 0                     ' this is the default: no need to set
    Application.ScreenUpdating = False

    With Sht2
        .Activate                       ' no need to activate this sheet
        .Unprotect
        ' this is the whole sheet: Easier to refer to it as .Cells
        ' Set rng = .Cells
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'        LastRow = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, _
'                           LookIn:=xlFormulas, SearchOrder:=xlByRows, _
'                           SearchDirection:=xlPrevious, MatchCase:=False).Row
'        LastCol = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, _
'                           LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
'                           SearchDirection:=xlPrevious, MatchCase:=False).Column
'        MsgBox "Last row = " & LastRow & vbCr & _
'               "Last column = " & LastCol

        For R = 4 To LastRow
            IsBlack = Not CBool(StrComp(.Cells(R, 1).value, "X", vbTextCompare))
            FillPattern = CLng(Array(xlNone, xlSolid)(Abs(IsBlack)))
            Set Rng = .Range(.Cells(R, 1), .Cells(R, LastCol))
            With Rng.Interior
                If .Pattern <> FillPattern Then
                    .Pattern = FillPattern
                    If IsBlack Then
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorLight1
                    End If
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                    Rng.Font.ColorIndex = Array(xlAutomatic, 2)(Abs(IsBlack))
                End If
            End With
        Next R
    End With

    ' VBA does this cleanup automatically at the end of the sub
'    Set sht2 = Nothing
'    Set Rng = Nothing
    Application.ScreenUpdating = False
End Sub