博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VBS读取txt文档数据查找Excel中单元格数据符合条件的剪切到工作表2中
阅读量:4696 次
发布时间:2019-06-09

本文共 2000 字,大约阅读时间需要 6 分钟。

 

 

 

Dim fso,f,aset oExcel = CreateObject( "Excel.Application" )oExcel.Visible = false'4) 打开已存在的工作簿:oExcel.WorkBooks.Open( "F:\1.xlsx" )On Error Resume Next'判断是否存在Sheet2工作表,不存在新建If oExcel.WorkSheets("Sheet2") Is Nothing Then    MsgBox "不存在,开始执行"    oExcel.Worksheets.AddElse MsgBox "存在"End If'5) 设置第1个工作表为活动工作表:oExcel.WorkSheets("Sheet1").Activate'读取txt文档Set fso = CreateObject("Scripting.FileSystemObject")Set f=fso.OpenTextFile("D:\UnexistFile.txt",1)'逐行读取并赋值到变量a,Do...loop循环DO While f.AtEndOfStream <> True i = 1 a=f.ReadLine  'msgbox a  oExcel.WorkSheets("Sheet1").Activate  Do while oExcel.Cells(i,4).Value <> "" '查找A列中所有的单元格    '设置某个单元格中的数据赋值到变量    'b=oExcel.Cells(i,4).Value    'c=oExcel.Cells(i,17).Value    d=oExcel.Cells(i,21).Value    'e=oExcel.Cells(i,21).Value    '判断单元格数据是否与txt数据相等    if oExcel.Cells(i,4).Value = a then 'i是行,4是列        'oExcel.Cells(i,4).font.color = vbred '设置字体底色为红色        'msgbox "行"&i &"列"                oExcel.ActiveSheet.Range("A" & i & ":" & "X" & i).Cut        oExcel.WorkSheets("Sheet2").Activate        oExcel.ActiveSheet.Range("A1:X1").Insert        oExcel.WorkSheets("Sheet1").Activate        oExcel.ActiveSheet.Rows(i).Delete    ElseIf Instr(b,a)>1 then  '判断txt的数据是否被包含在单元格中        'msgbox "行"&i &"列"                oExcel.ActiveSheet.Range("A" & i & ":" & "X" & i).Cut        oExcel.WorkSheets("Sheet2").Activate        oExcel.ActiveSheet.Range("A1:X1").Insert        oExcel.WorkSheets("Sheet1").Activate        oExcel.ActiveSheet.Rows(i).Delete    Elseif oExcel.Cells(i,22).Value = a then        'msgbox "行"&i &"列"                oExcel.ActiveSheet.Range("A" & i & ":" & "X" & i).Cut        oExcel.WorkSheets("Sheet2").Activate        oExcel.ActiveSheet.Range("A1:X1").Insert        oExcel.WorkSheets("Sheet1").Activate        oExcel.ActiveSheet.Rows(i).Delete        'EXIT do        end if    i = i+1 looploopoExcel.ActiveWorkBook.SaveoExcel.WorkBooks.CloseoExcel.Quitset oExcel = nothingmsgbox "操作完成"

 

转载于:https://www.cnblogs.com/liuliu-word/p/7434891.html

你可能感兴趣的文章
查看手机已经记住的WIFI密码
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
我是怎么用缠论在商品里边抢钱之二 (2019-07-12 15:10:10)
查看>>
python入门之正则表达式
查看>>
SAS学习经验总结分享:篇五-过程步的应用
查看>>
Android创建文件夹及文件并写入数据
查看>>
file的getPath getAbsolutePath和getCanonicalPath的不同
查看>>
课时4—切入切出动画
查看>>
eclipse 编辑 python 中文乱码的解决方案
查看>>
Python 爬虫的集中简单方式
查看>>
数据库MySQL/mariadb知识点——触发器
查看>>
Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
查看>>
Binary Agents
查看>>
入门Webpack,看这篇就够了
查看>>
如何在数据库中使用索引
查看>>
ring0
查看>>
windows虚拟机下 安装docker 踩过的坑
查看>>
使用 CXF 做 webservice 简单例子
查看>>
2017-2018-1 20155339 《信息安全系统设计基础》第8周学习总结
查看>>
socket.io 消息发送
查看>>