简体   繁体   English

如何使用Excel VBA编辑文本文件

[英]How to edit a text file using excel vba

I need to edit a text file ie add and update the data using the macro. 我需要编辑一个文本文件,即使用宏添加和更新数据。 I have a text file having thousands of records, a separate excel sheet which gives the mapping of data in text file. 我有一个包含数千条记录的文本文件,还有一个单独的Excel工作表,该工作表提供了文本文件中数据的映射。 For example, a record in text file looks like: 例如,文本文件中的记录如下所示:

ABCDNew Delhi India

The mapping excel sheet for this is as below: 对此的映射Excel工作表如下:

Name    4
City    10
Country 10

Which means that the 1st four characters in record give the name, next 10 give city and next 10 give the country of a particular person and so on. 这意味着记录中的前四个字符指定名称,下十个输入城市名称,下十个输入特定人的国家,依此类推。 Now, I have to edit a record (one or two fields) for a particular name and save it in same file. 现在,我必须编辑特定名称的记录(一个或两个字段)并将其保存在同一文件中。 Also I have add a new record in the above specified format and then save the file. 另外,我已经以上述指定的格式添加了一条新记录,然后保存了文件。

The excel sheet just gives the mapping of the fields in the text file. Excel工作表仅提供文本文件中字段的映射。 It tells only the field lengths. 它只告诉字段长度。 As u can see in my question, the text is continuos without any delimiter in it. 正如您在我的问题中看到的那样,文本是连续的,没有任何分隔符。 hence the excel sheet is used to identify the variuos fields in the text file. 因此,Excel工作表用于识别文本文件中的变量字段。 sample record is like: 样本记录如下:

ABCDDelhiIndia1100019876543210 ABCDDelhiIndia1100019876543210

and the mapping is: 映射是:

Name 4 City 5 Country 5 PinCode 6 PhnNo 10 名称4城市5国家5密码6号码10

The mapping is same for all records in a file. 文件中所有记录的映射都是相同的。 Its just gives a way how the records can be read from a file. 它只是提供了一种如何从文件中读取记录的方法。

Any Ideas on how can I do this in simplest way? 关于如何以最简单的方式执行此操作的任何想法?

You can have some delimiter between Name, City and Country for example : ABCD:New Delhi:India. 您可以在名称,城市和国家/地区之间使用一些分隔符,例如:ABCD:New Delhi:India。 Having delimiter can allow you to separate name, city and country. 使用分隔符可以让您分隔名称,城市和国家。 Now you can easily determine the length of each strings. 现在,您可以轻松确定每个字符串的长度。 Now based on your requirements you can easily edit the data and write to the file. 现在,根据您的要求,您可以轻松地编辑数据并写入文件。 You can refer the below code for reference : Text File : exceltest abcd:new delhi:india test:NY:USA 您可以参考以下代码以供参考:文本文件:exceltest abcd:new delhi:india test:NY:USA

Macro Code : 宏代码:

Sub readTextFile()

Dim oFSO As New FileSystemObject

Dim oFS

Dim objFSO As New FileSystemObject

Set myTemp = objFSO.CreateTextFile("C:\Users\Rohit\Desktop\exceltestTemp.txt", True)

Set oFS = oFSO.OpenTextFile("C:\Users\Rohit\Desktop\exceltest.txt")

Do Until oFS.AtEndOfStream

stext = oFS.ReadLine

Dim testarray() As String

testarray = Split(stext, ":")

Dim i As Integer 

i = Len(testarray(0)) 'determine length of each String, here as a sample only length of one string is determined

Cells(4, "a").Value = i 'writing a length to excel sheet, you can write to any location

Cells(1, "a").Value = testarray(0) 'writing contents to excel sheet

Cells(2, "a").Value = testarray(1) 'writing contents to excel sheet

Cells(3, "a").Value = testarray(2) 'writing contents to excel sheet

'Search for the Name

If testarray(0) = "test" Then

testarray(1) = "Mumbai"

End If

myTemp.WriteLine testarray(0) & ":" & testarray(1) & ":" & testarray(2) 'writing to a temp text file

Loop

End Sub

Now based on your requirements you can search for the name and edit the data and write to a file. 现在,根据您的要求,您可以搜索名称并编辑数据并写入文件。 You can then delete the original file and save the temp file as the original file. 然后,您可以删除原始文件并将临时文件另存为原始文件。

PS : Tried on Excel 2007. You need to add reference "Microsoft Scripting Runtime". PS:在Excel 2007上尝试过。您需要添加参考“ Microsoft Scripting Runtime”。

What you have described is fixed width data. 您所描述的是固定宽度数据。 This is straight forward to import the data as a text file in excel using the 'fixed width' option in the import dialog box. 直接使用导入对话框中的“固定宽度”选项将数据作为excel中的文本文件导入。 You can then select the start and end for each field. 然后,您可以为每个字段选择开始和结束。 The trickier part is to export it to the same format. 棘手的部分是将其导出为相同格式。 The only way I know of to achieve this is via VBA. 我知道实现此目标的唯一方法是通过VBA。 It seems like it would be straight forward routine to write though, it has probably been written many, many times. 似乎这将是直接简单的例程,但是可能已经被编写了很多遍了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM