This is to import csv files in a workbook temporary and connect and delete at the end... seems working...
If you want to test, you need to select the csv files in order File1 then File2
Sub test()
Dim fn, i As Long, wb As Workbook, temp
fn = Application.GetOpenFilename("CSVFiles,*.csv", , , , True)
If Not IsArray(fn) Then Exit Sub
If UBound(fn) <> 2 Then Exit Sub
GetWorkbook fn
Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.Provider = "Microsoft.Ace.OLEDB.12.0"
.Properties("Extended Properties") = "Excel 12.0; HDR=Yes;"
.Open ThisWorkbook.Path & "\temp.xlsx"
End With
rs.Open "TRANSFORM FIRST(A.[textValue]) AS TextValue SELECT clng(G.[position]) AS [G-Value], 1 AS Theme," & _
"COUNT(clng(A.[position])) AS Var, G.[label] AS [Attribute Name] FROM `" & fn(1) & "$` AS G, `" & _
fn(2) & "$` AS A WHERE G.[geneid] = A.[geneid] " & _
"GROUP BY clng(G.[position]),G.[label] ORDER BY clng(A.[position]) Asc PIVOT clng(A.[position]);", cn, 3
For i = 0 To rs.Fields.Count - 1
Cells(1, i + 1) = rs.Fields(i).Name
Next
[a2].CopyFromRecordset rs
Set cn = Nothing: Set rs = Nothing
Kill ThisWorkbook.Path & "\temp.xlsx"
End Sub
Private Sub GetWorkbook(fn)
Dim i As Long, temp, wb As Workbook
temp = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = UBound(fn)
Set wb = Workbooks.Add
Application.SheetsInNewWorkbook = temp
For i = 1 To UBound(fn)
With wb.Sheets(i).QueryTables.Add(Connection:= _
"TEXT;" & fn(i), Destination:=wb.Sheets(i).Range("$A$1"))
.FieldNames = True
.FillAdjacentFormulas = False
.PreserveFormatting = True
.AdjustColumnWidth = True
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
fn(i) = wb.Sheets(i).Name
Next
wb.SaveAs ThisWorkbook.Path & "\temp.xlsx", 51
wb.Close
End Sub
Can not think any other...
Bookmarks