使用VBA文本中提取信息 [英] extracting information from a text using vba

查看:214
本文介绍了使用VBA文本中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取文本文件。这个是在我的文本文件中的行的一些信息,我想提取的数量和保存这些数字在数组中。

  ST / X 1000.0000000000000000 1400.0000000000000000 40.0000000000000000 25.0000000000000000 12.0000000000000000
 

数的数目并不固定(即在这个例子中我有5个数字,但也可以是更多或更少) 总有数字之间3空格,但是数字的长度也没有固定(例如1000.0000000000000000具有21的长度,但12.0000000000000000具有19的长度)。我写了code此块。但我的code中的问题是,它返回的空格作为最后一个数字了。我的code犯规正常工作 你有更好的想法,我做这个工作做得更好? 感谢您的帮助和想法:)

我的code:

 昏暗lngPos只要
 昏暗lngCount只要
 昏暗的IFILE作为整数
 昏暗Xarray()作为字符串
 让IFILE = FreeFile
 昏暗的名称作为字符串
 昏暗喜作为整数
 名称= util1.fDateiName(* .txt的,文本)
C:\ Dokumente UND Einstellungen \ bbastan \桌面\ test.txt的
打开名称输入作为IFILE

虽然不是EOF(IFILE)
线路输入#ifile,entireline
REDIM Xarray(10)
喜= 0
打开名称为输入作为IFILE
lngPos = 1
虽然不是EOF(IFILE)
线路输入#ifile,entireline

       做
        lngPos = INSTR(lngPos,entireline,空间(3))
        如果lngPos> 0然后
            喜=喜+ 1
            lngCount = lngCount + 1
            lngPos = lngPos + 3
            Xarray(十一)= MID(entireline,lngPos,21)
        结束如果
    循环直到lngPos = 0
蜿蜒

昏暗我作为整数
若惜> 2然后
MSGBOXJA
对于i = 1到喜 -  1
MSGBOX Xarray(I)
接下来我
其他
MSGBOXnein
对于i = 1到西安
MSGBOX Xarray(I)
接下来我
 

解决方案

在VBA 斯普利特()函数可以简化你的东西有点。它分割使用的分隔符的字符串并把元素放入数组。例如,下面的测试code ...

 子LineSplitTest()
昏暗entireline作为字符串,strArray()作为字符串,东西为Variant
entireline =ST / X 1000.0000000000000000 1400.0000000000000000 40.0000000000000000 25.0000000000000000 12.0000000000000000
strArray =斯普利特(entireline,空间(3)-1,vbBinaryCompare)
对于每一个东西在strArray
    事情=修剪(事)
    如果len(事)> 0然后
        Debug.Print事
    结束如果
下一个
Debug.Print[完成]
结束小组
 

...打印此在VBA IDE的即时窗口:

  ST / X
1000.0000000000000000
1400.0000000000000000
40.0000000000000000
25.0000000000000000
12.0000000000000000
[完成]
 

I want to extract some information from a text file.This is a line in my text file and I want to extract the numbers and save these numbers in an array.

ST/X   1000.0000000000000000   1400.0000000000000000   40.0000000000000000   25.0000000000000000   12.0000000000000000  

The number of numbers are not fix(i.e in this example I have 5 numbers but it could be more or less) There are always 3 spaces between the numbers, but the length of numbers also are not fix(for example 1000.0000000000000000 has a length of 21, but 12.0000000000000000 has a length of 19). I wrote this block of code. but the problem of my code is that it returns the white space as the last number too. my code doesnt work correctly Did you have a better Idea for me to do this job better? thank you for your helps and ideas :)

my code:

 Dim lngPos As Long 
 Dim lngCount As Long 
 Dim ifile As Integer 
 Dim Xarray() As String 
 Let ifile = FreeFile 
 Dim Name As String 
 Dim xi As Integer 
 Name = util1.fDateiName("*.txt", "Text") 
'"C:\Dokumente und Einstellungen\bbastan\Desktop\test.txt" 
'Open Name For Input As ifile 

'While Not EOF(ifile) 
'Line Input #ifile, entireline 
ReDim Xarray(10) 
xi = 0 
Open Name For Input As ifile 
lngPos = 1 
While Not EOF(ifile) 
Line Input #ifile, entireline 

       Do 
        lngPos = InStr(lngPos, entireline, Space(3)) 
        If lngPos > 0 Then 
            xi = xi + 1 
            lngCount = lngCount + 1 
            lngPos = lngPos + 3 
            Xarray(xi) = Mid(entireline, lngPos, 21) 
        End If 
    Loop Until lngPos = 0 
Wend 

Dim I As Integer 
If xi > 2 Then 
MsgBox "ja" 
For I = 1 To xi - 1 
MsgBox Xarray(I) 
Next I 
Else 
MsgBox "nein" 
For I = 1 To xi 
MsgBox Xarray(I) 
Next I

解决方案

The VBA Split() function might simplify things for you somewhat. It splits a string using a delimiter and puts the elements into an array. For example, the following test code...

Sub LineSplitTest()
Dim entireline As String, strArray() As String, thing As Variant
entireline = "ST/X   1000.0000000000000000   1400.0000000000000000   40.0000000000000000   25.0000000000000000   12.0000000000000000  "
strArray = Split(entireline, Space(3), -1, vbBinaryCompare)
For Each thing In strArray
    thing = Trim(thing)
    If Len(thing) > 0 Then
        Debug.Print thing
    End If
Next
Debug.Print "[done]"
End Sub

...prints this in the Immediate Window of the VBA IDE:

ST/X
1000.0000000000000000
1400.0000000000000000
40.0000000000000000
25.0000000000000000
12.0000000000000000
[done]

这篇关于使用VBA文本中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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