|
- Sub 转换()
- Dim oConn As Object
- Dim sSelect As String
- Dim vCol As Variant, nCol As Integer
-
- vCol = "1,3,5" '需要转换的列数
- vCol = Split(vCol, ",")
-
- Set oConn = CreateObject("Adodb.Connection")
- If Val(Application.Version) < 12 Then
- oConn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties='Excel 8.0;IMEX=1;HDR=No';Data Source=" & ThisWorkbook.FullName
- Else
- oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;IMEX=1;HDR=No';Data Source=" & ThisWorkbook.FullName
- End If
- For nCol = LBound(vCol) To UBound(vCol)
- If Val(vCol(nCol)) > 0 Then
- If sSelect <> "" Then sSelect = sSelect & " Union "
- sSelect = sSelect & "Select Trim([F" & vCol(nCol) & "]) As Data From [Sheet1$] Where Not [F" & vCol(nCol) & "] Is Null"
- End If
- Next
- sSelect = "Select [Data] From (" & sSelect & ") Group By [Data] Order By [Data]"
- [G:G].ClearContents
- [G1].CopyFromRecordset oConn.Execute(sSelect)
- oConn.Close
- Set oConn = Nothing
- End Sub
复制代码 |
|