VBA中实现Txt文本内容切分,贴到excel中

使用VBA编写子函数subReadDBTxt,将TXT文件中的数据按行读取,并粘贴到Excel特定单元格中,用于数据对比和查找差分。通过while循环处理文件内容,结合break语句控制流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VBA中实现Txt文本内容切分,贴到excel中cell列中

※Txt文件中的内容是从DB中导出的文本数据。在Excel中做数据对比时,找出相应的差分。
同时,VBA中while语句的Break功能的实现。

‘#子函数定义’
private sub subReadDBTxt(FileName as String)
Dim StarRng As Range '定义Excel中粘贴的开始位置
Dim txtPath as String, nextLine As string
Dim WriteFlg As Boolean, OpenFlg as Boolean
Dim i%, j%
Dim sheetName as string
'txtPath=“C:\test\abcdefg.txt”
Open FileName For Input As #1
WriteFlg = False
OpenFlg = False

Do while Not EOF(1)
	on Error Resume Next
	Line input #1, nextLine
	if nextLine = "" then
		WriteFlg = False
		OpenFlg = False
		goto CONTINUE
	end if

	if InStr(nextLIne, "sheet1") > 0 then
		sheetName="sheet1"
		goto CONTINUE
	elseif InStr(nextLIne, "sheet2") > 0 then
		sheetName="sheet2"
		goto CONTINUE
	End If
	if InStr(nextLIne, "----------------------") > 0 then 'Oracle 中DB导出时带有的分隔符
		WriteFlg = True
		goto CONTINUE
	end IF
	if WriteFlg = True then
		if  OpenFlg = False then
			worksheets(s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值