|
- Sub test()
- Dim fso As New FileSystemObject
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim sql As String
- Dim strconn As String
- Dim ws As Worksheet
-
- If MsgBox("提取数据将删除本工作簿除[总表]以外的其他数据表并重新生成,确定提取吗?", vbOKCancel + vbCritical + vbDefaultButton2, "提示") = vbCancel Then
- Exit Sub
- End If
-
- Dim mybook As String
- Dim mysheet As String
-
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
-
- For Each ws In Worksheets
- If ws.Name <> "总表" Then
- ws.Delete
- End If
- Next
-
- mybook = ThisWorkbook.FullName
- mysheet = "sheet1"
-
- strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & ";Extended Properties=""TEXT;HDR=YES;IMEX=1"";"
- cnn.Open strconn
-
- Set f = fso.GetFolder(ThisWorkbook.Path)
-
- For Each wj In f.Files
- If wj.Name Like "*.txt" Then
- Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
- ws.Name = Left(wj.Name, Len(wj.Name) - 4)
- sql = "select * from " & wj.Name
- rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
- For i = 0 To rs.Fields.Count - 1
- ws.Cells(1, i + 1) = rs.Fields(i).Name
- Next
- ws.Range("a2").CopyFromRecordset rs
- With Columns("A:A")
- .NumberFormatLocal = "0_ "
- .EntireColumn.AutoFit
- End With
- rs.Close
- End If
- Next
-
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- cnn.Close
- Set fso = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|