|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub Macro1()
- Dim cnn As Object, rs As Object, rst As Object, SQL$
- Dim MyFile$, s$, t$, i&, j&, p$
- p = ThisWorkbook.Path & ""
- MyFile = Dir(p & "*.xls")
- While MyFile <> ""
- If MyFile <> ThisWorkbook.Name Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=excel 8.0;Data Source=" & ThisWorkbook.Path & "" & MyFile
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- SQL = "select * from [" & s & "]"
- Set rst = CreateObject("adodb.Recordset")
- rst.Open SQL, cnn, 1, 3
- Open p & Replace(MyFile, ".xls", ".txt") For Output As #1
- For i = 1 To rst.RecordCount
- t = ""
- For j = 0 To rst.Fields.Count - 1
- t = t & Chr(9) & rst.Fields(j).Value
- Next j
- Print #1, Mid(t, 2)
- rst.MoveNext
- Next i
- Close #1
- End If
- End If
- rs.MoveNext
- Loop
- End If
- MyFile = Dir()
- Wend
- rs.Close
- rst.Close
- cnn.Close
- Set rs = Nothing
- Set rst = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|