使用VBA从具有相同单词的不同行之间的文本文件中读取行 [英] Reading lines from text file between different lines with same words using VBA

查看:66
本文介绍了使用VBA从具有相同单词的不同行之间的文本文件中读取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个包含以下数据的.txt文件:



Hi All,

I have a .txt file with data like following:

Student 1
name abc
age 12
section 123

Student 2
section 456
name xyz
age 13

Student 3
name dfg
age 12
section 456





我的输出文件是.xlsx文件





My output file will be .xlsx file

student  name  age  section
student1 abc    12   123
student2 xyz    13   456
student3 dfg    12   456





我正在处理此文件,以便每个学生的详细信息都在每行中在excel。

我如何在学生1和学生2之间放置一个标志或什么区别? Student之间的数据是常量,但序列是变化的。那不是问题。我无法找到在个别学生之间标记旗帜的方法!!

需要一些帮助!!



谢谢..! ! :)



我尝试了什么:



我无法想想改变旗帜的方法,即使我使用它。



这是我的代码:



I am processing this file so that details of each student comes in each row in excel.
How can I put a flag or something to differenciate between student 1 from student 2? The data between Student is constant but sequence are changed. That is not a problem. I am unable to find a way to mark flags between the individual student!!
Need some help!!

Thank you..!! :)

What I have tried:

I am unable to think of a way to change flag even if I use it.

Here is my code:

while not EOF()
   if instr(DataLine,"Student") > 0 then
      student_val=DataLine
      flag=true
   end if

   if flag=true then
      if instr(DataLine,"name") > 0 then
        name_val=rplace(DataLine,"name","")
      end if
   end if

wend





这为第一个学生提供了价值。但是什么时候可以重置标志以知道它是文本文件中的student2并以excel中的新行开头?



this gives values for first student. But when can I reset the flag to know that it is student2 in text file and start with new line in excel?

推荐答案

那么,当前的学号应该是解析状态机的属性。每次解析器遇到' Student< N> '行时,它应该改变这样的属性。解析所有其他行使用该属性将收集的数据放入正确的excel行。
Well, the current student number should be a property of your parsing state machine. Each time the parser meets a 'Student <N>' line, it should change such property. The parsing of all the other lines uses the property to place the collected data into the proper excel row.


这篇关于使用VBA从具有相同单词的不同行之间的文本文件中读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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