简体   繁体   English

如果单元格值包含在另一个工作表中,则为Excel vba

[英]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.interiorr.interiorColumns(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.

相关问题 将单元格的位置粘贴到另一个工作表中-Excel VBA - Pasting the Location of a Cell in Another Worksheet - Excel VBA 如何使用另一个工作表中单元格内的单元格地址更改具有 Excel vba 的单元格的值? - How to change the value of a cell with Excel vba using a cell address that's inside a cell in another worksheet? vba excel在日期单元格值上隐藏工作表 - vba excel hide worksheet on date cell value 根据 Excel VBA 中的单元格值选择工作表 - Selecting a worksheet based on a cell value in Excel VBA Excel:根据另一个工作表上另一个单元格中的值更新工作表 - Excel: Update Worksheet based on value in another cell on another worksheet Excel VBA:根据日期(单元格值)删除行并追加到另一个工作表 - Excel VBA: delete row and append to another worksheet based on date (cell value) VBA Excel-根据用户输入将单元格值和关联的行获取到另一个工作表中 - VBA Excel- Get Cell value and associated rows into another worksheet based on User Input Excel VBA将单元格分配给单元格(根据另一个工作表将单元格值插入每行) - Excel VBA assigning cells to cells (inserting cell value to each row according to another worksheet) VBA 根据单元格值乘以另一个工作表中的单元格 - VBA to multiply based on cell value, by cell in another worksheet Excel VBA:引用另一个工作表中单元格的公式(变量) - Excel VBA: Formula with reference to cell in another worksheet (variable)
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM