Excel VBA 如何生成.csv或.xlsx檔案
阿新 • • 發佈:2019-02-15
Sub 調價通知() 'excel控制word,生成新檔案,插入圖片和檔名,儲存 'office 2003, VBA工具/引用中要勾選Microsoft Word 11.0 Object Library 'office 2007, VBA工具/引用中要勾選Microsoft Word 12.0 Object Library y = Year(Date) m = Month(Date) If Len(m) = 1 Then m = "0" & m d = Day(Date) If Len(d) = 1 Then d = "0" & d a = y & "-" & m & "-" & d file = a & "置頂價格調整方案通知" file_moban = a & "-模板-【置頂】調價模板-sy修改.xlsm" '生成excel Dim excelApp As Excel.Application '定義變數 Set excelApp = CreateObject("Excel.Application") '生成WORD物件 excelApp.Workbooks.Add '新建檔案 FN$ = "C:\Users\msy\Documents\work\調價模板\" & file '生成檔名 excelApp.ActiveWorkbook.SaveAs FN$ '儲存檔案 excelApp.Quit '退出 Workbooks.Open FN$ Set excelApp = Nothing '取消變數 '填背景 file_tongzhi = file & ".xlsx" Windows(file_tongzhi).Activate ActiveWindow.DisplayGridlines = False Range("A1:E2").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5880731 .TintAndShade = 0 .PatternTintAndShade = 0 End With 'copy Windows(file_moban).Activate Sheets("調價通知").Select num = Range("H1") Range(Cells(1, 1), Cells(num, 7)).Copy Windows(file_tongzhi).Activate Range("A1").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False '調整為日期格式 'Columns("F:F").NumberFormatLocal = "yyyy/m/d" '調整格式 'Windows("2.xlsx").Activate ' Range("A4:G44").Select Range(Cells(4, 1), Cells(num, 7)).Select With Selection.Font .Name = "Arial Unicode MS" .Size = 10 End With With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter End With Range("A4:G4").Select Selection.Font.Bold = True With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Range(Cells(5, 1), Cells(num, 7)).Select With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Columns("A:G").ColumnWidth = 13.13 Sheets(1).Name = "調價通知" ActiveWorkbook.Save End Sub