VBA导入文本文件到Excel和分析

我为成为一个noob而道歉。 我有以下风格生成的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的。 大量的数据可能会被忽略,我只需要从中得到一些东西并对其进行格式化即可。 > IMPORT-> 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 

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

有人可能会有一个更好的algorithm,但这是我的方法。 (注意:只有伪代码)

 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代码放在一行,但是通过分离出不同的子程序,你可以单独编写和testing每一个,而忽略文件的其余部分。 一旦你有一个工作,你可以注释出来的电话,所以你不需要通过它,而你正在debugging下一个,只是为了节省时间。 另外,使用单独的子文件中的大部分代码读取会更容易。