如何使用Excel VBA识别文本文件是UNIX还是Windows? [英] How to identify if a text file is unix or windows using excel vba?

查看:68
本文介绍了如何使用Excel VBA识别文本文件是UNIX还是Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Excel中处理某些文件,这些文件可能是Unix或Windows格式.我已经有了正确的代码来读取每个文件,但是在调用正确的过程之前,我需要提前知道文件类型.

I need to process some files in Excel that may be in Unix or Windows format. I already have the right code to read each one, but I need to know in advance the file type before calling the right procedure.

是否有使用VBA识别文件类型(Unix/Windows)的简便方法?

Is there an easy way to identify the file type (Unix / Windows) using VBA?

谢谢

推荐答案

您可以使用类似的方法进行计数:

You could use something like this to count:

    Private Sub CountCRLF()
       Dim TmpStr$()
       TmpStr = Split(Text1.Text, Chr(13) & Chr(10))
       MsgBox "There are " & UBound(TmpStr()) & " CRLF's in string", vbInformation, "Count"
    End Sub

如果超过几个,则应为Windows文件.如果文件混合了两种类型的换行符,则需要一种更复杂的方法.

If that is more than a few, it should be a Windows file. If a file mixes both types of line breaks, a more complex approach would be needed.

但是,这使我担心性能,除非您确定文件真的很小.而且您的问题中的某些内容使我认为您已经具有一个功能来逐行处理这些行,并且您可能应该只尝试处理这些文件一次,并在进行过程中进行检测和处理...

But this worries me in terms of performance, unless you are sure the files are really small. And something in your question makes me think you already have a function to handle the lines one by one and you should probably try to only process these files once, detecting and handling as you go along...

这篇关于如何使用Excel VBA识别文本文件是UNIX还是Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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