Microsoft Access VBA - 编辑文本文件 [英] Microsoft Access VBA - Edit Text File

查看:62
本文介绍了Microsoft Access VBA - 编辑文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Microsoft Access vba 编辑文本文件.我想要做的是从文本文件中删除所有逗号.

I am trying to edit a text file using Microsoft access vba. What I want to do is the remove all the comma from the text file.

我在网上尝试了一些代码,它对我的​​测试文件非常有效.

I tried the some of the codes online and it works perfectly fine for my test file.

将文件中的,"替换成"的代码是什么

What the code is to replace the "," in the file with ""

然而,当我尝试在我的实际文本文件上运行它时,访问将变得没有响应,我等了 2 个小时,但仍然没有响应.

However, when I try to run it on my actual text file, the access will become not responding , I waited 2 hours , but still didn’t responded.

我不确定是不是因为我的数据太大了,我的文本文件大小是 417MB.

I not sure is it because of my data is too huge, the size of my text file is 417MB.

有没有人可以给我建议?谢谢!

Is there anyone is able to advise me on this? Thank you!

我正在使用的代码

Private Sub Click()

Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
    sFileName = "C:123\123\data.txt"
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum
    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close iFileNum
    sTemp = Replace(sTemp, ",", "")
    iFileNum = FreeFile
    Open sFileName For Output As iFileNum
    Print #iFileNum, sTemp
    Close iFileNum
End Sub

推荐答案

这一行:

sTemp = sTemp & sBuf & vbCrLf

可能是什么扼杀了你的表现.请参阅此处的讨论:使用预制 Stringbuilder 类

is probably what's killing your performance. See discussion here: Using Pre-Made Stringbuilder Class

您会发现逐行处理文件并写出到不同的文件会更快.还要保持原始文件完好无损,以防万一...

You will find it's faster to process the file line-by-line and write out to a different file. Also keeps your original file intact in case you make a mistake...

Private Sub Click()

    Dim sBuf As String
    Dim iFileNum As Integer, iFileNum2 As Integer
    Dim sFileName As String, sFileNameOut As String

    sFileName = "C:123\123\data.txt"
    sFileNameOut = "C:123\123\data_out.txt"

    iFileNum = FreeFile
    Open sFileName For Input As iFileNum

    iFileNum2 = FreeFile
    Open sFileNameOut For Output As iFileNum2

    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        Write #iFileNum2, Replace(sBuf, ",", "")
    Loop

    Close iFileNum
    Close iFileNum2

End Sub

这篇关于Microsoft Access VBA - 编辑文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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