[英]Excel vba if cell value is included in another worksheet
I have worksheets called '1Q FY 18' which is the second sheet and 'Holiday' which is the 7th sheet. 我有名为“ 1Q FY 18”的工作表,这是第二张工作表,名为“假日”的工作表,是第七工作表。
The row 12 of '1Q FY 18' contains dates from 4/1 to 6/30. “ 1Q FY 18”的第12行包含从4/1到6/30的日期。 And the cells from 'B20' to 'B35' contains holidays of 2018.
从“ B20”到“ B35”的单元格包含2018年的假期。
I would like to highlight columns in the second sheet that have matching value at range("B20:B35"). 我想突出显示第二张工作表中具有匹配值range(“ B20:B35”)的列。 In other words, I would like to color the columns that is holiday.
换句话说,我想为假期设置颜色。
My code was like this. 我的代码是这样的。
Sub Holiday()
lastr = Worksheets(2).UsedRange.Rows.Count
lastc = Worksheets(2).UsedRange.Columns.Count
Worksheets(2).Activate
Set ws7 = Worksheets("Holiday")
Set rng1 = ws7.Range("B20:B35")
For i = 15 To lastc
If Not IsEmpty(Application.WorksheetFunction.VLookup(Cells(12, i).Value, rng1, 1)) Then
Range(Cells(13, i), Cells(lastr - 2, i)).Interior.ColorIndex = 6
End If
Next i
End Sub
But error occurs with this one. 但是与此错误发生。 When I debug, it highlights
当我调试时,它突出显示
****If Not IsEmpty(Application.WorksheetFunction.VLookup(Cells(12, i).Value, rng1, 1)) Then**** ****如果不是IsEmpty(Application.WorksheetFunction.VLookup(Cells(12,i).Value,rng1,1))然后****
What do I do? 我该怎么办?
Here is a simple macro that does what you describe. 这是一个执行您描述的简单宏。 I've placed a
.select
statement in it so you can step through the code to see how it works, but you should remove it after that. 我在其中放置了一个
.select
语句,因此您可以单步执行代码以查看其工作方式,但此后应将其删除。 Once you understand how it works you can change to suit your needs. 一旦了解了它的工作原理,就可以进行更改以满足您的需求。 Let me know if you have questions.
如果您有任何问题,请告诉我。 The animated gif shows each sheet of the workbook and then the macro running.
动画的gif显示工作簿的每一页,然后显示宏运行。 In my example I've just chosen every week for the holidays, but you can change that.
在我的示例中,我只是选择每周放假一次,但是您可以更改它。 I also included a sub to reset the colors conveniently.
我还包括一个子,可以方便地重置颜色。 (You say you want to "color the columns" but I just colored the cells. If you really want to color the whole column you would replace
cell.interior
.. and r.interior
... with Columns(cell.Column).interior
...) (您说过您想给列上色,但我只是给单元格上色。如果您真的想给整个列
cell.interior
, r.interior
用Columns(cell.Column).interior
替换cell.interior
..和r.interior
...。 Columns(cell.Column).interior
...)
Sub test()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim r1 As Range, r2 As Range, cell As Range, holidayCell As Range
Set sh1 = Worksheets("1Q FY 18")
Set sh2 = Worksheets("Holiday")
Set r1 = sh1.Range("A12")
Set r1 = sh1.Range(r1, r1.End(xlToRight))
Set r2 = sh2.Range("B20:B35")
resetColor r1
For Each cell In r1
cell.Select
For Each holidayCell In r2
If cell = holidayCell Then
cell.Interior.Color = vbYellow
Exit For
End If
Next
Next cell
End Sub
Sub resetColor(r As Range)
With r.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.