使用Visual Basic基于文本文件中的特定单词进行计算 [英] Calculation based on specific word in a text file using Visual Basic

查看:62
本文介绍了使用Visual Basic基于文本文件中的特定单词进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改一些代码,这些代码在从.pdf创建后读取.txt文件(这很好用),并在文本文件中找到一个特定的行。一旦我找到了这一行(它将在每个文本文件中,但在不同的行上),然后我想使用它下面的直线来完成计算。我也可以这样做:我可以到文本文件的末尾(没有关于如何编程的线索)并从行计数中减去6以在正确的行上进行计算。我试图得到的线始终是从底部开始的第6行,但是从文件的顶部开始变化。


目前我有类似的东西:

如果是InStr(lineNo,Credits Debits Difference)那么

lineNo = lineNo + 1

For Each i In Split(line)

If(IsNumeric(i))then

totalItems = totalItems + i

结束如果

下一页

结束如果


信用借记差异是我要找的文字txt文件。 lineNo只是我设置的一个变量,用于前进和读取文本文件中的每一行。


感谢您的任何建议。

解决方案

展开 | 选择 | Wrap | 行号

*****如果回复两次 - 对不起!******

对我来说很有意义...但我仍然很难实施它。这就是我现在所拥有的:


dim lineArray()as string,lineContent as string

dim tempLine as string,fNumb as integer

dim itemsFile


itemsFile = txtFilePath

fNumb = FreeFile


''在另一个文件中打开同一个文件实例二读

打开itemsFile输入为#fNumb

lineContent = input(FileLen(itemsFile,#fNumb)

close #fNumb


''将每行文件读入数组中的新行。

lineArray = Split(linecontent,vbNewLine)


''转到数组结束并备份6行以获得正确的行总数

tempLine = lineArray(UBound(lineArray) - 6)


''将行总数分成单独的值

For Each i In Split(临时线)

if(IsNumeric(i))then

''在总字段中添加值

totalItems = totalItems + i

结束如果

下一页


''关闭第二次出现的文件

itemsFile.close


这是所有这些都放在一个正在循环读取文件的while循环中。我上面的if语句将同一个文件打开成一个完全不同的命名对象。我假设第二次打开文件,它可以完整地读取。

当我运行它时,没有任何反应。我已经放置了消息框,看它是否会弹出它们,没有任何反应。也不会弹出任何错误消息。有些事情是错的,我还是无法理解它。如果我删除/注释掉上面的代码,那么程序将运行并创建消息框,甚至是我要求它在最后执行的空白电子邮件。这是一段没有按照我的意愿运行的代码。此代码还包含totalItems的所有单独变量EXCEPT,它们在程序顶部声明。

感谢您提供任何其他建议!我知道它应该做什么,我只是无法正确编写。当我被告知哦,你错过了一个逗号时,我确信我会感到愚蠢,但我最后还是有了这个!


亲爱的,

有一个)遗失在:


''在另一个实例中打开同一个文件进行第二次阅读

打开itemsFile输入为#fNumb

lineContent = 输入(FileLen(itemsFile,#fNumb)关闭#fNumb


必须是:


lineContent =输入(FileLen(itemsFile),#fNumb)



br,


I am in the process of modifying some code that reads a .txt file after it has been created from .pdf (this works great) and find a specific line in the text file. Once I have found this line (it will be in every text file but on a different line) I then want to use the line directly below it to complete a calculation. I can also go about it like this: I can go to the end of the text file (no clue as to how to program this) and subtract 6 from the line count to do the calculation on the correct line. The line I am trying to get at is always the 6th line from the bottom, but varies from the top of the file.

Currently I have something like this:
If InStr(lineNo, "Credits Debits Difference") Then
lineNo = lineNo + 1
For Each i In Split(line)
If (IsNumeric(i)) Then
totalItems = totalItems + i
End If
Next
End If

Credit Debit Difference is the text I am looking for in the txt file. lineNo is just a variable I set up to advance and read each line in the text file.

Thank you for any suggestions.

解决方案

Expand|Select|Wrap|Line Numbers


*****if replied twice - sorry!******
Makes perfect sense to me...BUT I am still having a hard time implementing it. This is what I have now:

dim lineArray() as string, lineContent as string
dim tempLine as string, fNumb as integer
dim itemsFile

itemsFile = txtFilePath
fNumb = FreeFile

''open same file in another instance for a second reading
open itemsFile for input as #fNumb
lineContent = input(FileLen(itemsFile, #fNumb)
close #fNumb

''read each line of file into new line in the array.
lineArray = Split(linecontent, vbNewLine)

''goto end of array and back up 6 lines to get to correct line with totals
tempLine = lineArray(UBound(lineArray) - 6)

''split the line totals into seperate values
For Each i In Split(tempLine)
if (IsNumeric(i)) then
''add values into total field
totalItems = totalItems + i
End if
Next

''close out 2nd occurance of file
itemsFile.close


This is all placed inside of a while loop that is reading the file for the initial time. My if statement above opens the same file into a completely different named object. I am assuming that the 2nd time the file is opened it can be read in it''s entirety.
When I run this, nothing happens. I have put in place message boxes to see if it will even pop them up, and nothing happens. No error messages pop up either. Something is wrong and I just cannot get my mind around it yet. If I remove/comment out the above code, then the program will run and create the message boxes and even a blank e-mail that I asked it to do at the end. This is the one piece of code that is not functioning as I would like it to. This code also has all separate variables EXCEPT for the totalItems, which was declared at the top of program.
Thank you for any additional suggestions you may have! I know what it should do, I just can not get it written correctly. I am sure I will feel stupid when I am told "oh your missing a comma", but I am at my wits end with this one!


dear,
there is a ")" missing in:

''open same file in another instance for a second reading
open itemsFile for input as #fNumb
lineContent = input(FileLen(itemsFile, #fNumb)close #fNumb


must be:

lineContent = Input(FileLen(itemsFile), #fNumb)



br,


这篇关于使用Visual Basic基于文本文件中的特定单词进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆