VBA实现:在附注工作表查找关键词并精准复制内容至校验工作表
好的,我明白你的需求了。下面是一个VBA脚本,可以帮助你在Excel中完成这个任务。请确保你已经启用了Excel的开发工具选项卡,以便插入和运行VBA代码。 ```vba Sub FindAndCopyDerivatives() Dim wsSource As Worksheet Dim wsTarget As Worksheet Dim keyword1 As String, keyword2 As String, keyword3 As String Dim keywordRange As Range Dim startRow As Long, endRow As Long, copyRow As Long Dim targetRow1 As Long, targetRow2 As Long, targetRow3 As Long, targetRow4 As Long Dim foundKeyword1 As Range, foundKeyword2 As Range, foundKeyword3 As Range Dim foundTotal1 As Range, foundTotal2 As Range ' 设置工作表 Set wsSource = ThisWorkbook.Sheets("附注") Set wsTarget = ThisWorkbook.Sheets("附注校验") ' 设置关键词 keyword1 = "利率衍生工具" keyword2 = "权益衍生工具" keyword3 = "其他衍生工具" ' 初始化目标行 targetRow1 = 149 targetRow2 = 154 targetRow3 = 159 targetRow4 = 163 ' 查找关键词 Set foundKeyword1 = wsSource.Columns(1).Find(What:=keyword1, LookIn:=xlValues, LookAt:=xlWhole) Set foundKeyword2 = wsSource.Columns(1).Find(What:=keyword2, LookIn:=xlValues, LookAt:=xlWhole) Set foundKeyword3 = wsSource.Columns(1).Find(What:=keyword3, LookIn:=xlValues, LookAt:=xlWhole) ' 处理找到的关键词 If Not foundKeyword1 Is Nothing Then Call ProcessKeyword(foundKeyword1, wsSource, wsTarget, targetRow1, targetRow4) End If If Not foundKeyword2 Is Nothing Then Call ProcessKeyword(foundKeyword2, wsSource, wsTarget, targetRow2, targetRow4) End If If Not foundKeyword3 Is Nothing Then Call ProcessKeyword(foundKeyword3, wsSource, wsTarget, targetRow3, targetRow4) End If ' 在合计行下方重新查找关键词 If Not foundTotal1 Is Nothing Then Set keywordRange = wsSource.Range(wsSource.Cells(foundTotal1.Row + 1, 1), wsSource.Cells(wsSource.Rows.Count, 1)) Set foundKeyword1 = keywordRange.Find(What:=keyword1, LookIn:=xlValues, LookAt:=xlWhole) Set foundKeyword2 = keywordRange.Find(What:=keyword2, LookIn:=xlValues, LookAt:=xlWhole) Set foundKeyword3 = keywordRange.Find(What:=keyword3, LookIn:=xlValues, LookAt:=xlWhole) If Not foundKeyword1 Is Nothing Then Call ProcessKeyword(foundKeyword1, wsSource, wsTarget, 174, 188) End If If Not foundKeyword2 Is Nothing Then Call ProcessKeyword(foundKeyword2, wsSource, wsTarget, 179, 188) End If If Not foundKeyword3 Is Nothing Then Call ProcessKeyword(foundKeyword3, wsSource, wsTarget, 184, 188) End If End If End Sub Sub ProcessKeyword(keywordCell As Range, wsSource As Worksheet, wsTarget As Worksheet, targetRow1 As Long, targetRow4 As Long) Dim startRow As Long, endRow As Long, copyRow As Long Dim foundTotal As Range startRow = keywordCell.Row Set foundTotal = wsSource.Range(wsSource.Cells(startRow, 1), wsSource.Cells(wsSource.Rows.Count, 1)).Find(What:="合计", LookIn:=xlValues, LookAt:=xlWhole) endRow = foundTotal.Row - 1 For copyRow = startRow To endRow wsSource.Range(wsSource.Cells(copyRow, 1), wsSource.Cells(copyRow, 7)).Copy wsTarget.Cells(targetRow1, 1).PasteSpecial Paste:=xlPasteValues targetRow1 = targetRow1 + 1 Next copyRow wsSource.Range(wsSource.Cells(foundTotal.Row, 1), wsSource.Cells(foundTotal.Row, 7)).Copy wsTarget.Cells(targetRow4, 1).PasteSpecial Paste:=xlPasteValues End Sub ``` 需要注意的逻辑点: 1. 关键词查找范围:确保关键词查找的范围正确,特别是在合计行下方重新查找时,避免重复查找。 2. 合计行的唯一性:假设每个关键词下方只有一个合计行,如果有多行合计,需要进一步处理。 3. 目标单元格的更新:确保目标单元格的行号在每次复制后正确更新,避免覆盖已有的数据。 希望这个脚本能帮到你!如果有任何问题或需要进一步调整,请告诉我。 ######[AI写代码神器 | 1106点数解答 | 2024-12-18 14:32:49]
- 2025 暑假苍溪 - 广安 - 重庆红色研学:中小学生传承红色基因之旅 (字节豆包 | 1244点数解答 | 2025-06-19 16:32:34)256
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)380
- VBA代码:巧用“数据源”关键词在“w附注”找区域并复制到“附注”指定位置(字节豆包 | 636点数解答 | 2024-10-11 17:16:08)286
- Python爬取百度学术300条文献信息并可视化各年份文献数量(字节豆包 | 858点数解答 | 2024-12-10 03:59:45)361
- 数字化制图综合平台:助力企业转型,推动烟草物流图纸智能化设计 (字节豆包 | 490点数解答 | 2024-10-30 20:30:29)159
- 数字化制图综合平台:引领烟草物流及多领域设计革新 在企业数字化转型的时代浪潮中,数字化制图技术创新应用成为产业升级与提升竞争力的关键。企业对高效、精准、智能化设计解决方案需求迫切,构建集二维到三维正逆向工程快捷化、干涉检测可视化、设备明细数据提取自动化以及设备模块模型库于一体的综合平台意义重大。 此平台集成先进数字化制图工具与智能算法,可实现二维设计到三维建模的无缝转换,大大缩短设计周期。正逆向工程能快速切换,满足不同场景设计需求。内置干涉检测功能可实时监测设计潜在冲突,保障设计方案可行与安全。设备明细数据自动提取提高设计准确性与效率。设备模块模型库为设计师提供丰富资源,支持设计标准化与模块化,加速项目推进。 整体来看,该综合平台的应用极大促进了烟草物流行业及更广泛领域的技术创新与效率提升。在烟草物流方案图纸智能化设计中,它提升了图纸设计效率与质量,使设计工作更加高效、精准。对于企业而言,这一平台带来显著竞争优势,能帮助企业在数字化转型中脱颖而出,在激烈的市场竞争中占据有利地位,推动整个行业向更高效、更智能的方向发展。 (阿里通义 | 216点数解答 | 2024-10-30 20:30:51)190
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)350
- 工程师揭秘:性能优化工作的完整流程大公开!(百度文心 | 275点数解答 | 2023-11-09 18:29:04)315
- 揭秘成都治水管水改革:五大机制闭环管理,技术报告助力供排水智能化优化(GPT | 372点数解答 | 2023-12-04 09:49:06)313
- 2020 年成都治水管水改革多管齐下:建“五大机制”、管网体检,编制报告提效供水排水 (讯飞星火 | 251点数解答 | 2023-12-04 09:49:34)299
- 多维度成长:从个人提升到社会担当的蜕变之旅(字节豆包 | 72点数解答 | 2025-03-16 11:46:27)228
- stroke_data.xls 数据读取、筛选与可视化:男性心脏病患者年龄与血糖关系揭秘(阿里通义 | 467点数解答 | 2025-05-18 14:35:44)214