VB 子函数定义与输出TAB分隔的TXT文件

Sub pp()

Dim i As Integer
Dim totalRows As Integer
Dim aa As String
Dim bb As String
Dim cc As String


totalRows = Sheets(1).Range("a65536").End(xlUp).Row '第1个页面的有效数据行数

MsgBox ("开始处理: " & "数据总行数" & (totalRows - 1))


'检查数据
For i = 2 To totalRows
    aa = Sheets(1).Cells(i, "A")
    If checkData(i, totalRows, aa) = False Then
        Exit Sub
    End If
Next


'创建文件
Dim fso, testfile
Dim strTab As String
strTab = Chr$(9)
Set fso = CreateObject("scripting.filesystemobject") '指明一个对象
Set testfile = fso.createtextfile("datanice.txt", True) '指明创建目录和文件类型

'循环第1个页面所有行
For i = 2 To totalRows

    '获取第1个页面值
    aa = Sheets(1).Cells(i, "A")
    bb = Sheets(1).Cells(i, "B")
    cc = Sheets(1).Cells(i, "C")

    '写入文件
    testfile.writeline (aa + strTab + bb + strTab + cc) '指明创建方式
    
Next


'关闭文件
testfile.Close

MsgBox ("处理完成! sheet1总行数" & (totalRows - 1))

End Sub

Function checkData(rowid As Integer, totalRows As Integer, data As String) As Boolean
Dim i As Integer
For i = 2 To totalRows
dm = Sheets(1).Cells(i, “A”)
If dm = data And i <> rowid Then
MsgBox (“第1列第” & (i) & “行与第” & (rowid) & “行数据项不得重复!” & “数据项:” + dm)
checkData = False
Exit Function
End If
Next
checkData = True
End Function

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值