Sub txt()
i = 1 '指定从第一行开始,如果数据从第二行开始,则修改为2即可,依此类推
OutDir = "C:\" '指定生成的txt文件所在的保存目录,可自行修改
If Dir(OutDir, vbDirectory) = "" Then
MsgBox "你所指定的输出目录不存在,请修正后重试!"
Exit Sub
End If
Do Until Cells(i, 2) = "" '如果遇到该单元格为空,则自动终止
Open OutDir + "\" + Cells(i, 2) + ".txt" For Output As #11
If Cells(i + 1, 2) = Cells(i, 2) Then
If Cells(i, 2) = Cells(i - 1, 2) Then
Print #11, Cells(i, 1)
Else:
Print #11, Cells(i, 1)
Print #11, Cells(i + 1, 1)
End If
End If
Close (11)
i = i + 1
Loop
MsgBox "处理完毕"
End Sub