VBA将文本文件导入Excel和Parse

时间:2015-03-30 16:38:23

标签: excel vba excel-vba

我为一个菜鸟而道歉。我有以下样式生成的TXT文件。我需要能够将它们转换为可以导入数据库的SpreadSheet。

": var1:"
"NOM (LSL, USL) = 0.044 (0.035, 0.051)"
"Subgroup"  1   2   3       
"Status"    ""  ""  ""  
"X" 0.045   0.047   0.048       
"R" 0.009   0.008   0.003       
"s" 0.003   0.003   0.001       
"S1"    0.050   0.051   0.049       
"S2"    0.046   0.047   0.047       
"S3"    0.048   0.049   0.048       
"S4"    0.047   0.048   0.046       
"S5"    0.042   0.044   0.047       
"S6"    0.042   0.044   0.048       
"S7"    0.041   0.043   0.048       
"S8"    0.044   0.048   0.049       
"comments"  " " " " " " 
"Inspector" "tb"    "tb"    "tb"    
"Machine"   "shot1" "shot2" "shot3"     
"ID #"  " " " " " " 
"Work Order"    " " " " " "     
"Samples"   " " " " " " "   
"Sample"    " " " " " " "   
"Sample"    " " " " " " "   
"Sample"    " " " " " " "   
"Time"  "23:05:04"  "23:30:33"  "02:15:40"
"Date"  "03/26/2015"    "03/26/2015"    "03/27/2015"

": var2:"
"NOM (LSL, USL) = 0.023 (0.020, 0.026)"
"Subgroup"  1   2   3   
"Status"    ""  ""  ""  
"X" 0.022   0.021   0.024       
"R" 0.003   0.002   0.003       
"s" 0.001   0.001   0.001       
"S1"    0.022   0.022   0.023   
"S2"    0.023   0.021   0.022   
"S3"    0.022   0.021   0.023   
"S4"    0.021   0.021   0.025   
"S5"    0.022   0.022   0.024   
"S6"    0.023   0.020   0.022   
"S7"    0.020   0.022   0.024   
"S8"    0.020   0.020   0.025   
"comments"  " " " " " " 
"Inspector" "tb"    "tb"    "tb"
"Machine"   "shot1" "shot2" "shot3"
"ID #"  " " " " " "     
"Work Order"    " " " " " " 
"Samples"   " " " " " " 
"Sample"    " " " " " " 
"Sample"    " " " " " " 
"Sample"    " " " " " " 
"Time"  "23:05:04"  "23:30:33"  "02:15:40"
"Date"  "03/26/2015"    "03/26/2015"    "03/27/2015"

": var3:"
"NOM (LSL, USL) = 0.023 (0.020, 0.026)"
"Subgroup"  1   2   3
"Status"    ""  ""  ""
"X" 0.045   0.044   0.046
"R" 0.004   0.005   0.007
"s" 0.001   0.002   0.003
"S1"    0.046   0.046   0.045
"S2"    0.045   0.041   0.046
"S3"    0.046   0.046   0.048
"S4"    0.047   0.046   0.042
"S5"    0.044   0.045   0.048
"S6"    0.046   0.045   0.047
"S7"    0.043   0.042   0.047
"S8"    0.045   0.042   0.041
"comments"  " " " " " " 
"Inspector" "tb"    "tb"    "tb"
"Machine"   "shot1" "shot2" "shot3"
"ID #"  " " " " " "
"Work Order"    " " " " " "
"Samples"   " " " " " "
"Sample"    " " " " " "
"Sample"    " " " " " "
"Sample"    " " " " " "
"Time"  "23:05:04"  "23:30:33"  "02:15:40"
"Date"  "03/26/2015"    "03/26/2015"    "03/27/2015"

这只是TXT的一个小样本,可能会有更多的S#并且会更多Var。很多数据都可以被贬低,我只需要一些东西并对其进行格式化。 >导入 - > PARSE-> FORMAT

        : var1:     : var2:     : var3:
NOM     0.044       0.023       0.023
LSL     0.035       0.02        0.02
USL     0.044       0.026       0.026
S1      0.05        0.022       0.046
S2      0.046       0.023       0.045
S3      0.048       0.022       0.046
S4      0.047       0.021       0.047
S5      0.042       0.022       0.044
S6      0.042       0.023       0.046
S7      0.041       0.02        0.043
S8      0.044       0.02        0.045
S1      0.051       0.022       0.046
S2      0.047       0.021       0.041
S3      0.049       0.021       0.046
S4      0.048       0.021       0.046
S5      0.044       0.022       0.045
S6      0.044       0.02        0.045
S7      0.043       0.022       0.042
S8      0.048       0.02        0.042
S1      0.049       0.023       0.045
S2      0.047       0.022       0.046
S3      0.048       0.023       0.048
S4      0.046       0.025       0.042
S5      0.047       0.024       0.048
S6      0.048       0.022       0.047
S7      0.048       0.024       0.047
S8      0.049       0.025       0.041

我知道如何完成其​​中的一些事情,但如果可能的话,我不知道如何将它们合并为一个Sub。对于我的立场,我应该用这样的东西来解决这个问题。

Sub Import()

    With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Temp\HospiraWalls2.TXT" _
        , Destination:=Range("$A$1"))
        .Name = "HospiraWalls2"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    ActiveWindow.SmallScroll Down:=9
End Sub
  

PARSING:我不知道

Sub OneColumn()

Dim vIn     As Variant
Dim vOut    As Variant
Dim i       As Long
Dim j       As Long
Dim ub1     As Long
Dim ub2     As Long

vIn = Application.Transpose(Range("B1").CurrentRegion)
ub1 = UBound(vIn, 1)
ub2 = UBound(vIn, 2)
ReDim vOut(1 To ub1 * ub2, 1 To 1)
For i = 1 To ub1
    For j = 1 To ub2
        vOut(j + (ub2 * (i - 1)), 1) = vIn(i, j)
    Next j
Next i
Range("A1:A" & (ub1 * ub2)) = vOut

End Sub

1 个答案:

答案 0 :(得分:1)

我认为您的文本文件中的数据太过非结构化,无法使用QueryTables()方法。

有人可能会为您提供更好的算法,但这将是我的方法。 (注意:仅限伪代码)

Open the file
MyLine = Read a line from the file
While not EOF
  'get your key for the line (the bit between " ")
  Key = Mid(Myline, 2, InStr(2, MyLine, """")) 
  if left(key, 5) = ": var" then
    Call NewVarSetupNewColumn
  elseif left(key, 3) = "Nom" then
    Call ParseNomLine
  elseif left(key,1) = "S" and left(key,2) <> "Sa" then 'Get "Sx", but not "Sample"
    Call ParseSxLine
  elseif ...
  'handle remaining lines you need, if you don't need the line, don't handle it
  Read next line
End While

您可以将Call代码放在行中,但是通过单独创建子程序,您可以单独编写和测试每个代码,同时忽略文件的其余部分。一旦你有一个工作,你可以注释掉电话,这样你就不必在你调试下一个电话时通过它,只是为了节省时间。另外,使用大量代码在单独的子代中阅读它会更容易。