VBS讀取txt文檔數據查找Excel中單元格數據符合條件的剪切到工作表2中
阿新 • • 發佈:2017-08-26
msg readline style 新建 font cti pre creat ins
Dim fso,f,a set 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.Add Else MsgBox "存在" EndIf ‘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").ActivateDo 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 loop loop oExcel.ActiveWorkBook.Save oExcel.WorkBooks.Close oExcel.Quit set oExcel = nothing msgbox "操作完成"
VBS讀取txt文檔數據查找Excel中單元格數據符合條件的剪切到工作表2中