|
榴莲~ 发表于 2011-11-7 23:52
我这个问题和他差不多,但我想把要合并的向后串一列,但用老师的宏,我试着改了可是不对,请老师帮看看好 ...
附件中的csv文件也挺奇特,另存为csv文件后就可以了:
Sub Macro1()
Dim f$, s() As String, a, arr(1 To 60000, 1 To 15), i&, j&, m&
f = Dir(ThisWorkbook.Path & "\*.csv")
While f > ""
t = Replace(f, ".csv", "")
Open ThisWorkbook.Path & "\" & f For Input As #1
s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
For i = 1 To UBound(s)
m = m + 1
arr(m, 1) = t
a = Split(s(i), ",")
For j = 0 To UBound(a)
arr(m, j + 2) = a(j)
Next
Next
f = Dir()
Wend
ActiveSheet.UsedRange.Offset(1).ClearContents
[a2].Resize(m, 15) = arr
End Sub
Sub Macro2() 'SQL法
Dim cnn As Object, SQL$, myPath$, MyFile$
Set cnn = CreateObject("adodb.connection")
Application.ScreenUpdating = False
myPath = ThisWorkbook.Path & "\"
MyFile = Dir(myPath & "*.csv")
Do While MyFile <> ""
If SQL = "" Then SQL = "select '" & Replace(MyFile, ".csv", "") & "' as 台区编号,* from " & MyFile Else SQL = SQL & " union all select '" & Replace(MyFile, ".csv", "") & "' as 台区编号,* from " & MyFile
MyFile = Dir()
Loop
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;FMT=DELIMITED';Data Source=" & myPath
ActiveSheet.UsedRange.Offset(1).ClearContents
Range("a2").CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End Sub
|
|