简体   繁体   English

基于同一工作簿中另一个工作表的数据的工作表中的颜色单元格

[英]Color Cells in a Worksheet Based on Data From Another Worksheet in Same Workbook

I have the following worksheet called Data : 我有以下工作表称为数据 在此输入图像描述

In the same workbook I have another worksheet called Employee Database . 在同一个工作簿中,我有另一个名为Employee Database的工作表。 在此输入图像描述

In Excel, how can I color the "Employee E-mail Address" and the corresponding "Company" and "Company URL" cells red from the Data worksheet if the "Employee E-mail Address" is not in the Employee Database ? 在Excel中,如果“员工电子邮件地址”不在员工数据库中,如何从“ 数据”工作表中为“员工电子邮件地址”和相应的“公司”和“公司URL”单元格着色?

In otherwords, I am trying to make the Employee Database worksheet look like this: 换句话说,我试图使Employee Database工作表看起来像这样: 在此输入图像描述

I've just given an example and in reality I have over 10,000 cells worth of data to do this to. 我刚刚给出了一个例子,实际上我有超过10,000个单元格的数据来做到这一点。 I started doing this manually and realized it will take me forever. 我手动开始这样做,意识到它将永远带我。

I'd love to know if there is a macro that can do this in Excel? 我想知道是否有一个宏可以在Excel中执行此操作?

Help would be so much appreciated! 帮助将非常感谢! I have the example workbook of the screenshots above available for download here: http://www.mediafire.com/?dttztp66dvjkzn8 我在上面可以下载截图的示例工作簿: http//www.mediafire.com/?dttztp66dvjkzn8

Is this what you are trying? 这是你在尝试什么? This will create a new sheet "Desired Result" with the output. 这将创建一个带有输出的新工作表“Desired Result”。 Paste this in a module. 将其粘贴到模块中。

Option Explicit

Sub Sample()
    Dim wsData As Worksheet, wsDB As Worksheet, wsO As Worksheet
    Dim lRow As Long, i As Long
    Dim clrRng As Range

    Set wsData = Sheets("Data")
    Set wsDB = Sheets("Employee Database")
    Set wsO = Sheets.Add

    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Desired Result").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0

    With wsO
        .Name = "Desired Result"
        wsData.Cells.Copy .Cells

        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        For i = 2 To lRow
            If .Range("A" & i).Value = "" Then .Range("A" & i).Value = .Range("A" & i - 1).Value
        Next i

        For i = 1 To lRow
            If Application.WorksheetFunction.CountIf(wsDB.Columns(3), .Range("A" & i).Value) = 0 Then
                If clrRng Is Nothing Then
                    Set clrRng = .Rows(i)
                Else
                    Set clrRng = Union(clrRng, .Rows(i))
                End If
            End If
        Next i

        If Not clrRng Is Nothing Then clrRng.Interior.ColorIndex = 3

        For i = lRow To 2 Step -1
            If .Range("A" & i).Value = .Range("A" & i - 1).Value Then .Range("A" & i).ClearContents
        Next i
    End With
End Sub

You can do this without VBA, but it will require a slight change to the data on your Data sheet. 您可以在没有VBA的情况下执行此操作,但需要对Data表上的Data进行轻微更改。

I don't recommend the "Pivot Table" or "Subtotal"-style of data storage in Excel, where you enter a primary key in one column only once then fill down associated data next to it until the next primary key. 我不建议在Excel中使用“数据存储表”或“小计”样式的数据存储, 只需在一列中输入一次主键,然后在其旁边填充相关数据,直到下一个主键。

Like merged cells, this will only lead to problems later when you want to re-organize your data. 与合并单元格一样,只有在您想要重新组织数据时才会出现问题。

Here's what I did: 这是我做的:

Fill in missing email addresses on Data sheet 在数据表中填写缺少的电子邮件地址

Highlight cells A2 all the way down column A to the end of the data in column B . 突出显示单元格A2一直向下到A列到B列数据的末尾。 So if you had company names in cells B2:B100 , but only had emails from A2:A98 , you should highlight A2:A100 . 因此,如果您在单元格B2:B100有公司名称,但只有来自A2:A98电子邮件,则应突出显示A2:A100 This is because we are filling in the email address in each row of available data. 这是因为我们在每行可用数据中填写电子邮件地址。

Go to Editing » Find & Select » Go To Special , select Blanks and click OK . 转到编辑 » 查找并选择 » 转到特殊 ,选择Blanks ,然后单击OK

去特别,空白

Now with blanks selected, type = (up arrow) , then press Ctrl + Enter . 现在选择了空白,键入= (向上箭头),然后按Ctrl + Enter The blank cells in column A will fill in with the missing email addresses. A列中的空白单元格将填入缺少的电子邮件地址。 Highlight column A, copy and paste values. 突出显示A列,复制和粘贴值。

Create Dynamic Named Range for Emails 为电子邮件创建动态命名范围

On the Employee Database sheet, create a named range called "Emails" with the following formula in the "Refers to" box: 在“ Employee Database表上,在“引用”框中使用以下公式创建名为“电子邮件”的命名范围:

=OFFSET('Employee Database'!$C$1,1,0,COUNTA('Employee Database'!$C:$C)-1,1)

Add Conditional Formatting 添加条件格式

On the Data sheet, highlight A2:C whatever (ex: A2:C20000 ), then go to Home » Styles » Conditional Formatting and use the following formula: Data表上,突出显示A2:C whatever (例如: A2:C20000 ),然后转到主页 » 样式 » 条件格式并使用以下公式:

=ISNA(MATCH($A2,Emails,0))

Select the color scheme you want and click OK . 选择所需的颜色方案,然后单击“ OK Here's how it looks on my computer with some sample data: 以下是我在计算机上查看一些示例数据的方式:

突出显示未找到的样本数据

There are a few minor constraints: 有一些小的限制:

  • You cannot leave column A blank on the Data sheet any more. 您不能再在Data表上留下A列的空白。
  • You cannot have blank rows on the Employee Database sheet in between rows of data. 您不能在Employee Database表中的数据行之间有空行。 This is due to the way the dynamic range works. 这是由于动态范围的工作方式。

Benefits 优点

The benefits of this approach are, IMO, huge. IMO这种方法的好处是巨大的。

  • You can add or remove rows from the Employee Database sheet, and the highlighting will automatically adjust. 您可以在“员工数据库”工作表中添加或删除行,突出显示将自动调整。 Ex: if I add d@gmail.com and remove c@nbc.com, the formatting on the Data sheet updates immediately. 例如:如果我添加d@gmail.com并删除c@nbc.com,则Data表上的格式会立即更新。

更新员工数据库

  • You don't have to alter your existing worksheet structure (other than filling in the missing data and adding a range name). 您不必更改现有的工作表结构(填写缺少的数据和添加范围名称除外)。 No need for additional worksheets. 无需额外的工作表。
  • Your workbook can stay VBA-free (if it didn't have any already). 您的工作簿可以保持VBA免费(如果它已经没有)。

暂无
暂无

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

相关问题 将数据从一个工作簿的特定工作表复制到另一个具有相同工作表名称的工作表 - Copy data from specific worksheet of a workbook to another with the same worksheet name 仅将数据粘贴到另一个工作簿中工作表中不受保护(未锁定)的单元格中 - Pasting data only into unprotected (unlocked) cells on a worksheet from another workbook 将具有格式的单元格从Excel工作表复制到Delphi中的另一个工作簿工作表 - Copy cells with format from an excel worksheet to another workbook worksheet in Delphi VBA 将多个工作簿中的数据复制到另一个工作簿“同一工作表”中,然后更新格式颜色和字体 - VBA to copy data from multiple workbooks into another workbook 'same worksheet' then update format color and font 将数据从一个工作表复制到另一个工作簿中的指定工作表 - Copying data from one worksheet to specified worksheet in another workbook 将数据从工作表复制到另一个工作簿 - Copying Data from a worksheet to another workbook 根据当前工作簿上单元格中的数据,确定要从另一个工作簿复制什么工作表 - Determin what worksheet to copy from another workbook based on data in a cell on the current workbook 将工作簿工作表中的数据拉入另一个工作簿工作表的同一列 - Pull data from a workbook's worksheets into same column of another workbook's worksheet 将数据从工作簿(在文本框中)放到另一个工作簿(在工作表中) - Putting data from a workbook (inside a textbox) to another workbook (at worksheet) VBA将数据从一个工作簿复制到另一工作簿/工作表不起作用 - VBA to Copy data from one workbook to another workbook/worksheet not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM