Vlookup复制单元格的颜色

2024-01-15

我有以下表格:

    A       B     C     D
  1 Bob     1     6     Football
  2 Nate    3     7     Baseball
  3 Silver  3     2     Baseball
  4 Box     7     1     Cycling

A           D
Bob         ?

Nate        ?

我可以成功使用Vlookup来填充?细胞。例如,Vlookup(A8,A$1D$4,4,0)。 我不知道的是让 Vlookup 也复制颜色。 VBA应该有解决办法。我希望你的帮助。


干得好:

  1. 将此代码粘贴到新模块中
  2. 选择要通过 VLOOKUP 目标设置格式的单元格
  3. 运行宏formatSelectionByLookup

这是代码:

Option Explicit
' By StackOverflow user LondonRob
' See http://stackoverflow.com/questions/22151426/vlookup-to-copy-color-of-a-cell-excel-vba

Public Sub formatSelectionByLookup()
  ' Select the range you'd like to format then
  ' run this macro
  copyLookupFormatting Selection

End Sub

Private Sub copyLookupFormatting(destRange As Range)
  ' Take each cell in destRange and copy the formatting
  ' from the destination cell (either itself or
  ' the vlookup target if the cell is a vlookup)
  Dim destCell As Range
  Dim srcCell As Range

  For Each destCell In destRange
    Set srcCell = getDestCell(destCell)
    copyFormatting destCell, srcCell
  Next destCell

End Sub

Private Sub copyFormatting(destCell As Range, srcCell As Range)
  ' Copy the formatting of srcCell into destCell
  ' This can be extended to include, e.g. borders
  destCell.Font.Color = srcCell.Font.Color
  destCell.Font.Bold = srcCell.Font.Bold
  destCell.Font.Size = srcCell.Font.Size

  destCell.Interior.Color = srcCell.Interior.Color

End Sub

Private Function getDestCell(fromCell As Range) As Range
  ' If fromCell is a vlookup, return the cell
  ' pointed at by the vlookup. Otherwise return the
  ' cell itself.
  Dim srcColNum As Integer
  Dim srcRowNum As Integer
  Dim srcRange As Range
  Dim srcCol As Range

  srcColNum = extractLookupColNum(fromCell)
  Set srcRange = extractDestRange(fromCell)
  Set srcCol = getNthColumn(srcRange, srcColNum)
  srcRowNum = Application.Match(fromCell.Value, srcCol, 0)
  Set getDestCell = srcRange.Cells(srcRowNum, srcColNum)

End Function

Private Function extractDestRange(fromCell As Range) As Range
  ' Get the destination range of a vlookup in the formulat
  ' of fromCell. Returns fromCell itself if no vlookup is
  ' detected.
  Dim fromFormula As String
  Dim startPos As Integer
  Dim endPos As Integer
  Dim destAddr As String

  fromFormula = fromCell.Formula

  If Left(fromFormula, 9) = "=VLOOKUP(" Then
    startPos = InStr(fromFormula, ",") + 1
    endPos = InStr(startPos, fromFormula, ",")
    destAddr = Trim(Mid(fromFormula, startPos, endPos - startPos))
  Else
    destAddr = fromCell.Address
  End If
  Set extractDestRange = fromCell.Parent.Range(destAddr)

End Function

Private Function extractLookupColNum(fromCell As Range) As Integer
  ' If fromCell contains a vlookup, return the number of the
  ' column requested by the vlookup. Otherwise return 1
  Dim fromFormula As String
  Dim startPos As Integer
  Dim endPos As Integer
  Dim colNumber As String

  fromFormula = fromCell.Formula

  If Left(fromFormula, 9) = "=VLOOKUP(" Then
    startPos = InStr(InStr(fromFormula, ",") + 1, fromFormula, ",") + 1
    endPos = InStr(startPos, fromFormula, ",")
    If endPos < startPos Then
      endPos = InStr(startPos, fromFormula, ")")
    End If
    colNumber = Trim(Mid(fromFormula, startPos, endPos - startPos))
  Else
    colNumber = 1
  End If

  extractLookupColNum = colNumber

End Function

Private Function getNthColumn(fromRange As Range, n As Integer) As Range
  ' Get the Nth column from fromRange
  Dim startCell As Range
  Dim endCell As Range

  Set startCell = fromRange(1).Offset(0, n - 1)
  Set endCell = startCell.End(xlDown)

  Set getNthColumn = Range(startCell, endCell)

End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vlookup复制单元格的颜色 的相关文章

  • 从受密码保护的 Excel 文件到 pandas DataFrame

    我可以使用以下命令打开受密码保护的 Excel 文件 import sys import win32com client xlApp win32com client Dispatch Excel Application print Exce
  • 运行时错误“1004”:无法获取 WorksheetFunction 类的 Combin 属性

    我在 Excel 2013 的工作簿中有 VBA 函数 可以根据泊松分布计算 p 值 当 的时候events下面代码中的变量超过 1029 我得到运行时错误 1004 无法获取 WorksheetFunction 类的 Combin 属性
  • 如何使用 Java Apache POI 隐藏 Excel 工作表中以下未使用的行?

    我正在使用数据库中的数据填充模板 Excel 工作表 for Map
  • Excel 工作表到 iPhone 数据 -- A 点到 B 点

    尽可能简单 我有一个非常简单的 Excel 电子表格 只有 1000 多条记录 我想将其用作 iPhone 应用程序的静态数据源 最好的进攻计划是什么 我心中的可能性 1 直接读取XLS作为数据源 是否有Obj C库用于此 2 将XLS 转
  • 如何使用 Excel.UriLink.16 更改 Excel URL 的文件关联?

    我正在尝试更改文件关联 以便在另一个浏览器中打开 Excel 单元格中的 URL 根据使用 CMD ftype命令与Excel UriLink 16 我应该能够使用以下命令从 powershell 通过 cmd 执行此操作 To chang
  • 由于直接引用范围而不是通过中间变量而导致 Excel VBA 运行时错误 450

    当我尝试直接引用某个范围内的值时 出现运行时错误 450 但如果我使用中间变量 它就会起作用 我不明白为什么 所以我担心在将来的某个时候我会再次遇到错误而不知道为什么 我尝试过使用 With End With 块 但当我直接引用范围时它仍然
  • MS Access 中的舍入

    VBA Access 中舍入的最佳方法是什么 我目前的方法是利用Excel方法 Excel WorksheetFunction Round 但我正在寻找一种不依赖Excel的方法 请注意 VBA Round 函数使用 Banker 舍入 将
  • Excel VBA 导出到文本文件。需要删除空行

    我有一个工作簿 使用以下脚本将其导出到文本文件 它工作正常 但是当我打开文本文件时 末尾总是有一个空行 这导致我在生成此文本文件后运行的另一个脚本出现问题 有关如何从导出中删除空行的任何帮助 Code Sub Rectangle1 Clic
  • 如何使用Matlab将数据保存到Excel表格中?

    我想将数据以表格形式保存在 Excel 工作表中 它应该看起来像 Name Age R no Gpa Adnan 24 18 3 55 Ahmad 22 12 3 44 Usman 23 22 3 00 每次当我执行我的文件时类数据 m 下
  • 如何使用 VBA 将 mm/dd/yyyy 更改为 dd/mm/yyyy

    我在使用 VBA 将 mm dd yyyy 转换为 dd mm yyyy 日期格式时遇到问题 我有一个这样的表 仅供参考 该表是从报告工具自动生成的 字符串操作 或任何 Excel 函数可以提供帮助吗 希望知道如何解决这个问题的人可以给我一
  • 如何在字符串vba中包含引号

    我想存储以下文本 Test1 Monday Test Abcdef 全部在字符串中包含引号 我知道要在字符串中包含引号 我必须包含 之前 但在这里这不是一个很好的解决方案 因为我在文本中有太多这样的解决方案 知道如何一次完成这一切吗 您有两
  • VBA根据单元格的值是否为零显示/隐藏行

    我有一个 Excel 工作表 我想根据另一个单元格中的值隐藏或取消隐藏某些行 简而言之 整个事情应该取决于单元格中的值C2 D2 E2 If C2 is blank我想rows 31 to 40被隐藏 如果是的话不为空 他们需要是visib
  • laravel中过滤后如何导出excel?

    我想仅导出视图刀片中过滤的数据 我正在使用 Laravel 7 和 maatwebsite excel 3 1 和 PHP 7 4 2 我浏览了文档并应用了这个 View a href class btn btn success i cla
  • 无法将 Excel 值的类型“double”转换为“string”

    我正在加载 Excel 文件 如网络上许多地方所示 OpenFileDialog chooseFile new OpenFileDialog chooseFile Filter Excel files xls xlsl xls xlsx i
  • 如何在 Azure 逻辑应用中解析 Excel 电子表格

    我需要使用 Azure 逻辑应用从 Excel 电子表格中解析和提取列信息 我已经为我的逻辑应用程序设置了从 Outlook 检索最新未读电子邮件的功能 此外 我的逻辑应用程序执行 FOR EACH 来读取所有附件 来自未读电子邮件 并确保
  • 复制一张工作表上的静态范围,然后根据单元格中的单个值粘贴到另一张工作表中的动态范围

    我对这个问题分为三个部分 我在 Sheet1 A1 中有一个带有周数的单元格 我在 Sheet1 B1 F1 中有一个需要复制的静态范围 然后 我需要将该值粘贴到 Sheet2 中的动态范围中 偏移量为行的周数 这是我正在为我经常使用的工作
  • Outlook 无法识别一个或多个姓名

    我有以下 vba 代码 它读取邮箱并向任何发送无效代码作为邮箱回复的用户发送回复 但有时会收到运行时错误 Outlook 无法识别一个或多个名称 我的问题是 创建新的 MAPI 配置文件是否可以解决该问题 或者我是否需要添加一个代码来解析地
  • EPPlus Excel 行高不一致

    我已经使用 EPPlus 生成了一个 excel 文件 在 MS Office 2007 中一切似乎都很完美 但客户端使用的是 MS Office 2010 2013 并且在第 29 行之后未设置行高 这是一个非常奇怪的问题 我已经尝试了
  • 在Google电子表格中划分整列

    我是 Google 电子表格的一个相对较新的用户 我希望 B 列中的每个单元格都是 A 列 同一行 中内容除以 5 的结果 B1 B2 B3 等 商 应为 A1 A2 A3 等 被除数 除以 5 除数 在示例中 B1 A1 5 我知道一项一
  • 根据单元格值向用户窗体添加复选框

    我对 VBA 很陌生 只有 3 天 但我发现它非常有用且易于使用 但现在我面临一个问题 我需要制作一个具有不同复选框的用户窗体 但我需要根据工作表某一列中使用的信息自动添加它们 我相信我可以使用 For Each Next 但我真的不知道如

随机推荐