close

 

Excel VBA線上教學

 

Excel篇 :
Excel.DisplayAlerts := False;                                                  //Disable 提示訊息
Excel.Visible := True;                                                              //顯示 Excel 畫面
Excel.ActiveWindow.FreezePanes := True;                        //凍結窗格(上一行一定要先選列或格)
Excel.ActiveWindow.Zoom := 75;                                         //顯示比率為75%
Excel.ActiveWindow.Zoom := True;                                     //依據目前選擇範圍自動決定視窗大小

Workbook篇:
WorkBook := mExcel.WorkBooks[1];                               //將指定變數設定第一個活頁簿(數字可以用名稱取代)
WorkBook.Name :='內容';                                                  //變更WorkBook名稱
Excel.WorkBooks.Add;                                                       //新增一個空白活頁簿
Excel.WorkBooks.Open(完整路徑);                                  //開啟Excel檔
Excel.WorkBooks[mFile].Close;                                        //關閉Excel檔
DeleteFile(mPath+mFile);                                                  //刪除Excel檔
WorkBook.SaveAs(mPath+mFile,-4143);                        //儲存Excel檔

Sheet篇:
Sheet := mExcel.WorkBooks[1].WorkSheets[1];                                           //將指定變數設定第一個工作表(數字可以用名稱取代)
Sheet.Name :='內容';                                                                                        //變更Sheet名稱
Sheet.Copy[After := mWorkBook.Sheets[mWorkBook.Sheets.Count]];    //將mSheet複製到mWorkBook最後
Sheet.Move[After := mWorkBook.Sheets[mWorkBook.Sheets.Count]];    //將mSheet搬移到mWorkBook最後
Workbook.Sheets.Add[After:=mWorkbook.Sheets[mSheetCount-1]];       //新增一個空白工作表
WorkBook.Sheets[1].Delete;                                                                           //刪除指定Sheet
WorkBook.Sheets[1].Activate;                                                                        //將指定Sheet設為使用中

刪除多餘Sheet:
if (mWorkBook.Sheets.Count > 1) then
begin
  for i:=2 to mWorkBook.Sheets.Count do
    mWorkBook.Sheets[2].Delete;
end;

選取篇:
mSheet.Cells.EntireColumn                                         //所有欄
mSheet.Cells.EntireRow                                              //所有列
mSheet.Cells                                                                 //所有儲存格
mSheet.Columns[1]                                                      //第一欄
mSheet.Rows[1]                                                            //第一列
mSheet.Cells[r,c]                                                          //第r列第c欄
mSheet.Range[起,迄]                                                   //區間選擇(起訖可以是欄、列、格)

填值篇:
mSheet.Cells[1,1].Value:= '內容';                               //欄位填值
mSheet.Cells[1,1].Formula:= '公式';                             //欄位填入公式
mSheet.Cells[1,1].FormulaR1C1:= '公式';                         //欄位填入公式
mSheet.Cells[1,1].HasFormula                                    //儲存格是否有公式

格式篇:
mSheet.Range[起,迄].Merge;                                                 //合併儲存格
mSheet.Cells.EntireColumn.AutoFit;                                     //最適欄寬
mSheet.Cells.EntireRow.AutoFit;                                          //最適列高
mSheet.Columns[1].ColumnWidth := 100;                           //設定欄寬
mSheet.Rows[1].RowHeight := 100;                                     //設定列高
mSheet.Rows[1].HorizontalAlignment := -4108;                  //水平置中(靠左:-4131;靠右:-4152)
mSheet.Rows[1].VerticalAlignment := -4108;                      //垂直置中(靠左:-4131;靠右:-4152)
mSheet.Rows[1].WrapText  := True;                                     //自動換列
mSheet.Columns[1].Hidden := True;                                     //隱藏
mSheet.Columns[1].NumberFormatLocal := '@';                     //設定欄位格式[文字]
mSheet.Columns[1].NumberFormatLocal := '#,##0_ ';               //設定欄位格式[數值(整數位 三位一撇)]
mSheet.Columns[1].NumberFormatLocal := '#,##0_ ;[紅色]-#,##0 '; //設定欄位格式[數值(整數位 三位一撇 負數紅字)]
mSheet.Columns[1].NumberFormatLocal := '#,##0_);[紅色](#,##0)'; //設定欄位格式[數值(整數位 三位一撇 負數括號紅字)]
mSheet.Columns[1].NumberFormatLocal := '0.00_ ';                //設定欄位格式[數值(小數兩位)]
mSheet.Columns[1].NumberFormatLocal := '0.0_);[紅色](0.0)';     //設定欄位格式[數值(小數一位 負數紅字)]
mSheet.Columns[1].NumberFormatLocal := '0.00%';                 //設定欄位格式[百分比(小數兩位)]
mSheet.Cells[1].Interior.ColorIndex := 38;                      //設定底色為玫瑰色
mSheet.Cells[1].Interior.ColorIndex := 6;                       //設定底色為黃色
mSheet.Cells[1].Interior.ColorIndex := 36;                      //設定底色為淺黃色
mSheet.Cells[1].Interior.ColorIndex := 35;                      //設定底色為淺綠色
mSheet.Cells[1,1].Font.Size := 10;                              //設定字體大小
mSheet.Cells[1,1].Font.Bold := True;                            //設定粗體字

框線:
mSheet.Cells[1,1].Borders[n].LineStyle := 1;
mSheet.Cells[1,1].Borders[n].Weight := 2;
//n = 5.左上右下斜線 6.左下右上斜線 7.左邊線 8.上邊線 9.下邊線 10.右邊線 11.垂直線 12.水平線
//Borders可使用參數:
// LineStyle = 1 實線;-4115 短虛線;4 長短虛線;5 長短短虛線;-4118 細虛線;-4119 雙實線
// Weight =  由細到粗:1 --> 2 --> -4138 --> 4
// ColorIndex = 顏色

//設定格式化條件
Sheet.Cells[1,1].FormatConditions.Delete;                      //清除格式化條件
Sheet.Cells[1,1].FormatConditions.Add[Type:='1', Operator:='1', Formula1:='1', Formula2:='2']; //新增格式化條件(最多3個)

//參數說明
//  參數   中文說明                                  說明
//======== ======== =======================================================================
//Type     來源型態 1.儲存格的值 2.公式
//Operator 規則     1.介於 2.不介於 3.等於 4.不等於 5.大於 6.小於 7.大於或等於 8.小於或等於
//Formula1 條件起
//Formula2 條件迄
mSheet.Cells[1,1].FormatConditions(1).Interior.ColorIndex := 3; //設定條件一為底色紅色
//可設定之格式有:Fonts(字型)、Borders(外框)、Interior(圖樣)
//資料篇
mSheet.Cells.EntireColumn.AutoFilter;                           //自動篩選
mExcel.Selection.Subtotal(1,-4157,VarArrayOf([4,5,6,7,8]),True,False,True);  //做小計

//參數說明
//      參數               中文說明         預設值
//================ ======================== ======
//GroupBy          分組小計欄位             1
//Function         使用函數                 -4157 加總
//TotalList        新增小計位置
//Replace          取代目前小計             True
//PageBreaks       每組資料分頁             False
//SummaryBelowData 摘要資料置於小計資料下方 True
//可使用函數:-4157 加總;-4106 平均值;-4112 項目個數;-4113 數字項目數;-4136 最大值;-4139 最小值;
mSheet.Outline.ShowLevels(2);                                    //把小計層級設2顯示

列印篇:
mSheet.PageSetup.PrintTitleRows := '$1:$1';                      //列印標題列
mSheet.PageSetup.CenterHeader := '表頭';                         //中頁首
mSheet.PageSetup.LeftHeader   := '頁次: &P / &N';                //左頁首
mSheet.PageSetup.RightHeader  := '';                             //右頁首
mSheet.PageSetup.CenterFooter := '& &P / &N';                   //中頁尾
mSheet.PageSetup.LeftFooter   := '頁次: &P / &N';                //左頁尾
mSheet.PageSetup.RightFooter  := '';                             //右頁尾
mSheet.PageSetup.PrintArea := '$B$1:$N$300';                     //設定列印範圍
mSheet.PageSetup.Orientation := 2;                               //1.直印 2.橫印
mSheet.PageSetup.Zoom := 65;                                     //列印時小成65%
mSheet.PageSetup.Zoom := True;                                   //使用頁次縮放功能
mSheet.PageSetup.FitToPagesWide := 1;                            //縮放成一頁寬(需配合Zoom = True)
mSheet.PageSetup.FitToPagesTall := 1;                            //縮放成一頁高(需配合Zoom = True)
mSheet.PageSetup.PaperSize := 8;                                 //設定紙張大小 8:A3、9:A4
mSheet.PageSetup.TopMargin := 1/0.035;                           //頂邊距1cm
mSheet.PageSetup.BottomMargin := 1/0.035;                        //底邊距1cm
mSheet.PageSetup.LeftMargin := 1/0.035;                          //左邊距2cm
mSheet.PageSetup.RightMargin := 1/0.035;                         //右邊距2cm
mSheet.PageSetup.HeaderMargin := 1/0.035;                        //頁首1cm
mSheet.PageSetup.FooterMargin := 1/0.035;                        //頁尾1cm
mSheet.PageSetup.CenterHorizontally := True;                     //頁面水平居中
mSheet.PageSetup.CenterVertically := False;                      //頁面垂直居中

arrow
arrow
    全站熱搜

    alyoou 發表在 痞客邦 留言(0) 人氣()