|
楼主 |
发表于 2013-4-5 11:00
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 小花鹿 于 2013-4-5 11:47 编辑
整体读取文本文件:
Option Explicit
Sub test()
Dim thispath As String, mytxt As String, s, tm, br(), i&, j&
thispath = ThisWorkbook.Path & "\"
mytxt = Dir(thispath & "*.txt")
Open thispath & mytxt For Input As #1
tm = Split(StrConv(InputB(LOF(1), #1), vbUnicode), vbCrLf)
Close #1
For i = 0 To UBound(tm)
s = Split(tm(i), ",")
If i = 0 Then ReDim br(UBound(tm), UBound(s))
For j = 1 To UBound(s)
br(i, j) = s(j)
Next j
br(i, 0) = "'" & s(0)
Next i
Sheet3.[a1].Resize(i, j) = br
End Sub
原始数据(txt):
代码,fails,STEP,DEVICE,ACT,UT,LOW,HIGH,TestMSR,A,B
123456789020,fail,159,PR856-T,5,o,0.05,8,99999,351,359
123456789012,fail,985,C869-T,34.05,PF,,44.265,54.3096,1,68
123456789022,fail,1404,,0.13,uF,0.065,0.195,0.055,,1
123456789012,fail,1423,C810-B,0.1,uF,0.065,0.14,,1675,
123456789012,fail,1607,C529-T,1,,0.6,1.3,0.5866,742,1
123456789025,fail,1645,C1091-B,1,uF,0.65,,0.6016,1259,1258
123456789012,fail,,,1,uF,,1.7,,343,1
123456789012,fail,1827,PC526-T,38,uF,26.6,49.4,25.5999,1,316
不想占楼层,在这里回复6楼:原理很明白,只是写代码时还得想一会,特别是这句 n = Int(Rnd() * (r - i + 1)) + i
补充内容 (2016-10-30 11:02):
Sub test1()
Dim thispath As String, mytxt As String, s, tm, br(), i&, j&, ad
Set ad = CreateObject("adodb.stream")
With ad
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile ThisWorkbook.Path & "\@Template.htm"
tm = .ReadText
.Close
End With
tm = Split(tm, vbCrLf)
For i = 0 To UBound(tm)
Cells(i + 1, 1) = tm(i)
Next i
End Sub
补充内容 (2016-12-8 11:33):
Sub getdata()
Dim rst As Object, StrConn As String, arr As Variant
Set rst = VBA.CreateObject("ADODB.Recordset")
StrConn = "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='" _
+ "text;hdr=yes';data source=" & ThisWorkbook.Path & "\"
rst.Open "d1.txt", StrConn, 1, 3
rst.Move 1
arr = rst.GetRows()
rst.Close
MsgBox UBound(arr)
MsgBox UBound(arr, 2)
[a1:g1] = arr
Set rst = Nothing
End Sub
|
|