vba学习系列(4)-- index()提取指定单元格并保留字体格式

系列文章目录

文章目录

  • 系列文章目录
  • 一、目标需求
  • 二、使用步骤
    • 1.VBA程序
    • 2.VBA简要程序
  • 总结

一、目标需求

工作表2 B列中姓名,在工作表1 C列中存在相同姓名时,提取工作表2 AK列的对应单元格内容;
工作表2名称:OQC
工作表1名称:汇总

二、使用步骤

1.VBA程序

代码如下(示例):


Sub ExtractData()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2, rng3, rng4, sourceCell, destinationCell As Range, cell As Range
    Dim i, j As Integer
    Dim charColor As Long
    Dim sourceValue As String
    Dim charIndex As Integer
    Dim formattedText As String
    Dim fontColors() As Long
    Dim colorIndex As Integer
    Dim color As Integer

    
    '设置工作表1和工作表2
    Set ws1 = ThisWorkbook.Sheets("汇总")
    Set ws2 = ThisWorkbook.Sheets("OQC")
    '设置范围对象
    Set rng1 = ws1.Range("C:C")
    Set rng2 = ws2.Range("B:B")
    Set rng3 = ws1.Range("F3:F100")
    Set rng4 = ws2.Range("AK2:AK100")
    
    rng3.Interior.colorIndex = xlNone
    rng3.Font.colorIndex = xlNone
    rng3.Font.colorIndex = xlAutomatic
    
    '遍历工作表1的每个单元格
    For Each cell In rng1
        If cell.value <> "" Then
            '在工作表2的B列查找匹配项
            Set matchcell = rng2.Find(What:=cell.value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not matchcell Is Nothing Then
            '如果找到匹配项,则提取工作表2对应的AK列数据
                a = matchcell.Row
                Set sourceCell = ws2.Cells(matchcell.Row, "AK")
                Set destinationCell = ws1.Cells(cell.Row, "F")
                Set targetCell = ws1.Cells(cell.Row, "F")
                destinationCell.value = ""
                
                'Text = sourceCell.value
                Set rngSource = ws2.Range("AK" & a)
                
                ReDim fontColors(0 To rngSource.Characters.Count)
                For j = 1 To sourceCell.Characters.Count
                    charColor = sourceCell.Characters(j, 1).Font.color
                    fontColors(j) = charColor
                Next j
                
                For j = 1 To sourceCell.Characters.Count
                    With destinationCell.Characters(j, 1).Font
                        color = fontColors(j)
                    End With
                Next j
                
                ws2.Range("AK" & a).Copy Destination:=ws1.Cells(cell.Row, "F")                    
                                               
                '定义要使用的颜色数组
                'Colors = Array(vbRed, vbBlue, vbGreen)
                
                'For i = 1 To sourceCell.Characters.Count
                    'Set charFormat = sourceCell.Characters(Start:=i, Length:=1).Font
                    'targetCell.Characters(Start:=i, Length:=1).Font.color = charFormat.color
                    'targetCell.Characters(Start:=i, Length:=1).Font.Bold = charFormat.Bold
                     ' 如果需要复制其他字体属性,比如Italic或Underline,可以继续添加代码行
                'Next i                                    
                
                'ReDim fontColors(1 To sourceCell.Characters.Count)
                'ReDim fontColors(2 To rngSource.Characters.Count)
                
                'destinationCell.value = sourceCell.value
                'sourceCell.Copy Destination:=destinationCell
                
                'destinationCell.Interior.color = sourceCell.Interior.color
                'destinationCell.Font.color = sourceCell.Font.color                                
            
               'ws1.Cells(cell.Row, "F").value = ws2.Cells(matchcell.Row, "AK").value
               'ws1.Cells(Cell.Row, "F").Font.Color = ws2.Cells(matchCell.Row, "AK").Font.Color
               'ws1.Cells(Cell.Row, "F").Font.ColorIndex = ws2.Cells(matchCell.Row, "AK").Font.ColorIndex
               'ws1.Cells(Cell.Row, "F").Interior.Color = ws2.Cells(matchCell.Row, "AK").Interior.Color
               'ws1.Cells(Cell.Row, "F").Interior.ColorIndex = ws2.Cells(matchCell.Row, "AK").Interior.ColorIndex
               
               'ws1.Cells(Cell.Row, "F").HorizontalAlignment = ws2.Cells(matchCell.Row, "AK").HorizontalAlignment
               'ws1.Cells(Cell.Row, "F").VerticalAlignment = ws2.Cells(matchCell.Row, "AK").VerticalAlignment
               'ws1.Cells(Cell.Row, "F").Borders.LineStyle = ws2.Cells(matchCell.Row, "AK").Borders.LineStyle
               'ws1.Cells(Cell.Row, "F").Borders.Color = ws2.Cells(matchCell.Row, "AK").Borders.Color
                                         
               'ws1.Cells(Cell.Row, "F").Font.Name = ws2.Cells(matchCell.Row, "AK").Font.Name
               'ws1.Cells(Cell.Row, "F").Font.Size = ws2.Cells(matchCell.Row, "AK").Font.Size
               'ws1.Cells(Cell.Row, "F").Font.Bold = ws2.Cells(matchCell.Row, "AK").Font.Bold
               'ws1.Cells(Cell.Row, "F").Font.Italic = ws2.Cells(matchCell.Row, "AK").Font.Italic                        
          
            End If
        End If
    Next cell
End Sub

2.VBA简要程序


Sub ExtractData()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2, rng3, rng4, sourceCell, destinationCell As Range, cell As Range
    Dim i, j As Integer
    Dim charColor As Long
    Dim sourceValue As String
    Dim charIndex As Integer
    Dim formattedText As String
    Dim fontColors() As Long
    Dim colorIndex As Integer
    Dim color As Integer

    
    '设置工作表1和工作表2
    Set ws1 = ThisWorkbook.Sheets("汇总")
    Set ws2 = ThisWorkbook.Sheets("OQC3")
    '设置范围对象
    Set rng1 = ws1.Range("C:C")
    Set rng2 = ws2.Range("B:B")
    Set rng3 = ws1.Range("F3:F100")
    Set rng4 = ws2.Range("AK2:AK100")
    
    rng3.Interior.colorIndex = xlNone
    rng3.Font.colorIndex = xlNone
    rng3.Font.colorIndex = xlAutomatic
    
    '遍历工作表1的每个单元格
    For Each cell In rng1
        If cell.value <> "" Then
            '在工作表2的B列查找匹配项
            Set matchcell = rng2.Find(What:=cell.value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not matchcell Is Nothing Then
            '如果找到匹配项,则提取工作表2对应的AK列数据
                a = matchcell.Row
                Set sourceCell = ws2.Cells(matchcell.Row, "AK")
                Set destinationCell = ws1.Cells(cell.Row, "F")
                Set targetCell = ws1.Cells(cell.Row, "F")
                destinationCell.value = ""
                
                Set rngSource = ws2.Range("AK" & a)
                
                ReDim fontColors(0 To rngSource.Characters.Count)
                For j = 1 To sourceCell.Characters.Count
                    charColor = sourceCell.Characters(j, 1).Font.color
                    fontColors(j) = charColor
                Next j
                
                For j = 1 To sourceCell.Characters.Count
                    With destinationCell.Characters(j, 1).Font
                        color = fontColors(j)
                    End With
                Next j
                
                ws2.Range("AK" & a).Copy Destination:=ws1.Cells(cell.Row, "F")
                                                
            End If
        End If
    Next cell
End Sub


总结

分享:
接受可以让我面对所有的问题,当我感到焦虑的时候,通常是因为我发现自己不能接受生活中的一些人、地方、事情,直到我完全接受了它们,我才能获得心灵上的安宁。除非我完全的接受生活,否则我将无法获得快乐。我不需要再纠结这个世界上有什么需要改变而是关注我自己的态度需要发生怎样的改变;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/557681.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

springboot结合vue实现文件上传下载功能

紧接着上一次的博客&#xff0c;这次来实现一下文件(主要是图片)的上传和下载功能&#xff0c;上一次的博客如下所示&#xff1a; Springboot集成JWT token实现权限验证-CSDN博客 其实文件的上传和下载功能(后端的部分)&#xff0c;在我之前的博客就已经有写了&#xff0c;所以…

力扣经典150题第三十题:长度最小的子数组

目录 力扣经典150题解析之三十&#xff1a;长度最小的子数组1. 介绍2. 问题描述3. 示例4. 解题思路方法一&#xff1a;滑动窗口 5. 算法实现6. 复杂度分析7. 测试与验证测试用例设计测试结果分析 8. 进阶9. 总结10. 参考文献感谢阅读 力扣经典150题解析之三十&#xff1a;长度最…

重构国内游戏账号登录系统的思考和实践

本期作者 背景 账号登录系统&#xff0c;作为游戏发行平台最重要的应用之一&#xff0c;在当前的发行平台的应用架构中&#xff0c;主要承载的是用户的账号注册、登录、实名、防沉迷、隐私合规、风控等职责。合规作为企业经营的生命线&#xff0c;同时&#xff0c;账号登录作为…

数据结构系列-堆的实现

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 堆的实现&#xff0c;其实也就是二叉树的实现&#xff0c;我们在这里是基于数组对其进行实现的&#xff01; typedef struct Heap {HPDataType* a;int size;int capacity; }HP;…

毕业设计做一个linux操作系统怎么样?

毕业设计选择做操作系统的话&#xff0c;不太建议做的规模太大&#xff0c;你可以参考一下Linux内核的代码量&#xff0c;完全从头写的工作量还是挺大的。如果是一行一行从头写&#xff0c;学生期间&#xff0c;一学期写10000-20000行有效代码就很强了&#xff0c;而且还要学习…

【面经】2024春招-云计算后台研发工程师1(3个问题,移动TW等)

【面经】2024春招-云计算后台研发工程师1&#xff08;3个问题&#xff0c;移动&TW等&#xff09; 文章目录 岗位与面经基础1&#xff1a;数据库 & 网络&#xff08;3个问题&#xff09;基础2&#xff1a;系统 & 语法模板3&#xff1a;算法 & 项目&#xff08;移…

探索人工智能绘图的奇妙世界

探索人工智能绘图的奇妙世界 人工智能绘图的基本原理机器之美&#xff1a;AI绘图作品AI绘图对艺术创作的影响未来展望与挑战图书推荐&#x1f449;AI绘画教程&#xff1a;Midjourney使用方法与技巧从入门到精通内容简介获取方式&#x1f449;搜索之道&#xff1a;信息素养与终身…

Timelapse - 2024.04.09 -Win

阅读须知&#xff1a; 探索者安全团队技术文章仅供参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作,由于传播、利用本公众号所提供的技术和信息而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者 本人负责&#xff0c;作者不为此承担任何责任,如…

centos6.5重启docker容器死机问题

概述 近期在整理服务问题&#xff0c;使用docker容器重新部署服务。 过程中有不少坑&#xff0c;主要是系统配置和系统版本的问题。 环境 CentOS release 6.5 (Final) docker version 1.7.1 问题现象 使用restart命令重启docker容器&#xff0c;系统突然卡死&#xff0c…

protobuf抓包,读包

protobuf抓包 有时候会遇到使用protobuf协议的http请求, 而protobuf封包后的二进制几乎不可读, 如何调试呢 protobuf就是类似一个json的数据传输协议, 相比json更快, 体积更小; 缺点就是不可读 Content-Type: application/x-protobuf数据大概是下面这样的(浏览器开发者工具 自…

(十八)C++自制植物大战僵尸游戏的游戏暂停实现

植物大战僵尸游戏开发教程专栏地址http://t.csdnimg.cn/uzrnw 游戏暂停 当玩家遇到突发事件&#xff0c;可以通过暂停功能暂停游戏&#xff0c;以便及时处理问题。在激烈的游戏中&#xff0c;玩家可能需要暂停游戏来进行策略调整。此外&#xff0c;长时间的游戏对战可能会让玩…

爬取微博评论数据

# -*- coding: utf-8 -*- import requests #用于发送请求并且拿到源代码 from bs4 import BeautifulSoup #用于解析数据 1.找到数据源地址并且分析链接 2.发送请求并且拿到数据 3.在拿到的数据中解析出需要的数据 4.存储数据 headers { "User-Agent": "…

ubunt18.04安装ROS2

本文无废话&#xff0c;实现了ubunt18.04 下ros2的安装&#xff0c;并且同时兼容ros和ros2 如果想完ros&#xff08;1&#xff09;的&#xff0c;请参考我的前一篇文章&#xff1a;ubunt18.04安装ROS避坑指南 参考&#xff1a; https://blog.csdn.net/cau_weiyuhu/article/deta…

Ubuntu20.04版本命令行设置挂载磁盘,并设置开机自动挂载

最近部署应用 系统是Ubuntu20.4版本的Linux系统&#xff0c;加了数据盘&#xff0c;需要格式化后挂载&#xff0c;记录下&#xff1a; Linux 数据盘挂载(采用 parted 分区工具)-格式化为 ext4 1. 初始化 Linux 数据盘 挂载数据盘后或者随实例创建时一并创建的数据盘&#xff…

【数学建模】最优旅游城市的选择问题:层次分析模型(含MATLAB代码)

层次分析法&#xff08;The analytic hierarachy process&#xff0c;简称AHP&#xff09;是一种常用的决策分析方法&#xff0c;其基本思路是将复杂问题分解为多个组成部分&#xff0c;然后对这些部分进行逐一评估和比较&#xff0c;最后得出最优解决方案。&#xff08;例如&a…

Linux 5.10 Pstore 学习之(二) 原理学习

目录 编译框架模块初始化pstore子系统ramoops模块初始化实例化注册回调数据结构 pstore_blk模块pstore_zone模块 测试扩展调试 编译框架 目标结构 linux_5.10/fs/pstore/ ├── blk.c ├── ftrace.c ├── inode.c // 核心1 ├── internal.h ├── Kconfig ├── …

Vitis HLS 学习笔记--scal 函数-探究

目录 1. Vitis HLS重器-Vitis_Libraries 2. 初识scal() 3. 函数具体实现 3.1 变量命名规则 3.2 t_ParEntries解释 3.3 流类型详解 3.4 双重循环 4. 总结 1. Vitis HLS重器-Vitis_Libraries 在深入探索Vitis HLS&#xff08;High-Level Synthesis&#xff09;的旅程中&…

了解 containerd 中的 snapshotter,先从 native 开始

本文内容节选自 《containerd 原理剖析与实战》&#xff0c;本书正参加限时优惠内购&#xff0c;点击阅读原文&#xff0c;限时 69.9 元购买。 上一篇文章《一文了解 containerd 中的 snapshot》中&#xff0c;介绍了containerd 的 snapshot 机制&#xff0c;了解到 containerd…

64B/66B编码 自定义PHY层设计

一、前言 之前的一篇文章讲解了64B/66B的基本原理&#xff0c;本篇在基于64B/66B GT Transceiver的基础之上设计自定义PHY。基本框图如下。 二、GT Mdule GT Module就按照4个GT CHannel共享一个GT COMMON进行设置&#xff0c;如下图。要将例子工程中的GT COMMON取出&#xff…

3.4 海思SS928开发 - 烧写工具 - BurnTool Emmc 烧写

3.4 烧写工具 - BurnTool Emmc 烧写 BurnTool 工具提供了多种烧写方式&#xff0c;这里只介绍最常用的 烧写emmc方式。 环境准备 PC 与单板之间连接好调试串口以及网线。 将厂商提供的出厂镜像拷贝至 PC 硬盘上&#xff0c;解压后得到的文件如下&#xff1a; . ├── boot_…
最新文章