VB读取TXT文件中的指定列并计算

zqy1975 2013-07-12 06:12:23
00 0000000000001 159.0000 159.0000 2.0000 0 00
00 0000000000001 159.0000 159.0000 1.0000 0 00
00 0000000000001 159.0000 159.0000 5.0000 0 00
这是和TXT文件,行数不固定,如何用VB读出第四列 159.000乘第五列2.00,并把每一行这个乘积相加,比如159*2+159*1+159*5,急啊,那位大侠能帮我写一下
...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yrsaini 2013-07-12
  • 打赏
  • 举报
回复
测试过,完全没问题
zqy1975 2013-07-12
  • 打赏
  • 举报
回复
计算的结果是0
嗷嗷叫的老马 2013-07-12
  • 打赏
  • 举报
回复
dim sBuff as string,sLBuff() as string,sTmp() as string
dim I as long

open "xx.txt" for binary as #1
    sbuff=space(lof(1))
    get #1,,sbuff
close #1

slbuff()=split(sbuff,vbcrlf)
for i=0 to ubound(slbuff)
    slbuff(I)=trim(slbuff(I))
    if slbuff(I)<>"" then
        stmp()=split(slbuff(I)," ")
        debug.print stmp(3),stmp(4)
    end if
next
yrsaini 2013-07-12
  • 打赏
  • 举报
回复
Public Function 判断文件是否存在(ByVal file As String) As Boolean 1 On Error Resume Next 2 If (GetAttr(file) And vbDirectory) = False Then 判断文件是否存在 = True 3 If Err Then 判断文件是否存在 = False: Err.Clear End Function Function 读取文本文件3(ByVal fileName As String) As String Dim handle As Integer ' 判断文件存在性 If Not 判断文件是否存在(fileName) Then Exit Function ' 以binary模式打开文件 handle = FreeFile Open fileName$ For Binary As #handle ' 读取内容,关闭文件 读取文本文件3 = Space$(LOF(handle)) Get #handle, , 读取文本文件3 Close #handle End Function Private Sub Command1_Click() Dim zongshu As Long, tmpstr As String, tmpstrsplit, strsplit tmpstr = 读取文本文件3("y:\xt.txt") tmpstrsplit = Split(tmpstr, vbCrLf) For i = 0 To UBound(tmpstrsplit) strsplit = Split(tmpstrsplit(i), " ") If UBound(strsplit) > 4 Then zongshu = zongshu + Val(strsplit(3)) * Val(strsplit(4)) End If Next MsgBox zongshu End Sub

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧