[英]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: 这是我做的:
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列,复制和粘贴值。
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)
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: 有一些小的限制:
Data
sheet any more. Data
表上留下A列的空白。 Employee Database
sheet in between rows of data. Employee Database
表中的数据行之间有空行。 This is due to the way the dynamic range works. The benefits of this approach are, IMO, huge. IMO这种方法的好处是巨大的。
Data
sheet updates immediately. Data
表上的格式会立即更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.