VBA输出指定字段并保存为CSV

SUB DATA()
Dim cnn, SQL$, a, B, C, D, E, f, G, H, i, L, M
Dim OUT As Workbook
    Set cnn = CreateObject("adodb.connection")
    Set rs = CreateObject("adodb.recordset")
    
    
 cnn.Open "Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source =" & ThisWorkbook.Path & "\"
    

SQL = "SELECT 小区标识,A.CGI,NODEBID*小区ID AS ECI,LA,LO FROM [轩驰.CSV] A  LEFT JOIN [基础信息表.CSV] B on a.小区标识=B.CELL WHERE 设备类型 NOT IN ('5G')  AND 小区标识 IS NOT NULL"

Set OUT = Workbooks.Add

 Set rs = cnn.Execute(SQL)
 Worksheets("Sheet1").Activate
 
  Sheets("Sheet1").Cells.ClearContents       '清理保存数据的区域
     For i = 0 To rs.Fields.Count - 1
       Worksheets("Sheet1").Cells(1, i + 1) = rs.Fields(i).Name
     Next
Sheets("Sheet1").Range("a2").CopyFromRecordset rs
    
rs.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
  
    OUT.SaveAs ThisWorkbook.Path & "\4G_CELL_INFO.csv", xlCSV
    Workbooks("4G_CELL_INFO.csv").Close True
 
MsgBox ("OK")

END SUB
上一篇:vba考点例题解析


下一篇:UiPath 执行VBA代码Selection.Copy复制不生效