我将在脚本中比较两个excel数据,并将在第三个excel中写入匹配数据。脚本完美运行并将匹配数据写入第三个excel。问题是它为不匹配的数据创建了一个空行。不知道如何删除空行或如何阻止它写入空数据。
"C:\Excelone.xls"
中,A列值为 1,2,3,4,5,6 "C:\Exceltwo.xls"
中,A列值为 1,ab,3,ac,5,6 我需要的输出是 1,3,5,6
请帮我解决问题。请找到以下脚本
Set myxl = createobject("excel.application")
myxl.Workbooks.Open "C:\Excelone.xls"
myxl.Application.Visible = true
set mysheet = myxl.ActiveWorkbook.Worksheets("Sheet1")
Set myx2 = createobject("excel.application")
myx2.Workbooks.Open "C:\Exceltwo.xls"
myx2.Application.Visible = true
set mysheet1 = myx2.ActiveWorkbook.Worksheets("Sheet1")
Set myx3 = createobject("excel.application")
myx3.Workbooks.Open "C:\Excelthree.xls"
myx3.Application.Visible = true
set mysheet3 = myx3.ActiveWorkbook.Worksheets("Sheet1")
row=0
'With mysheet.UsedRange
For each search_data in mysheet.UsedRange
row = row+1
For each search_data1 in mysheet1.UsedRange
If search_data=search_data1 Then
mysheet3.cells(row,1).value=search_data1
Exit For
End If
Next
Next
'End With
myxl.ActiveWorkbook.Save
myxl.ActiveWorkbook.Close
myxl.Application.Quit
Set mysheet =nothing
Set myxl = nothing
答案 0 :(得分:0)
尝试将计数器(row = row+1
)移动到成功匹配的循环中,即
For each search_data in mysheet.UsedRange
'row = row+1
For each search_data1 in mysheet1.UsedRange
If search_data=search_data1 Then
row = row+1
mysheet3.cells(row,1).value=search_data1
Exit For
End If
Next
Next