聯成電腦評價文章精選:常用 Excel VBA 指令彙總

常用 Excel VBA 指令彙總

HI~聯成電腦評價推薦給您的實用文章,這次小編在網路中發現一位熱心的捧油,很勤奮的整理出常用的 VBA 指令,這邊就分享給有需要的人,同時也謝謝原PO這麼努力及熱心。

花了兩天的時間把這幾年用過的 Excel VBA 指令彙總起來,分門別類,方便以後使用,順手貼出來給有需要的朋友。看倌多珍惜啊,這可是好幾年的心血說。(使用 Delphi 語法) LINE Q , Dcard ,PTT ,學電腦 ,領贈品,聯成電腦Dcard,巨匠電腦,巨匠美語,評價,比一比,MOBILE01,伊莉論壇,卡提諾論壇,台灣論壇討論推薦


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

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

//Sheet篇
mSheet := mExcel.WorkBooks[1].WorkSheets[1];                    //將指定變數設定第一個工作表(數字可以用名稱取代)
mSheet.Name :=’內容’;                                           //變更Sheet名稱
mSheet.Copy[After := mWorkBook.Sheets[mWorkBook.Sheets.Count]]; //將mSheet複製到mWorkBook最後
mSheet.Move[After := mWorkBook.Sheets[mWorkBook.Sheets.Count]]; //將mSheet搬移到mWorkBook最後
mWorkbook.Sheets.Add[After:=mWorkbook.Sheets[mSheetCount-1]];   //新增一個空白工作表
mWorkBook.Sheets[1].Delete;                                     //刪除指定Sheet
mWorkBook.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 = 顏色

//設定格式化條件
mSheet.Cells[1,1].FormatConditions.Delete;                      //清除格式化條件
mSheet.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;                      //頁面垂直居中

文章來源:http://blog.xuite.net/luckysft/blog/14509469-%E5%B8%B8%E7%94%A8+Excel+VBA+%E6%8C%87%E4%BB%A4%E5%BD%99%E7%B8%BD

聯成電腦士林分校粉絲團 ( https://www.facebook.com/lccnet05/ )
聯成電腦 ( http://www.lccnet.com.tw )
聯成電腦FB ( https://www.facebook.com/lccnetzone )
聯成電腦學員作品( http://lccnetvip.pixnet.net/blog )
聯成電腦評價集 ( http://www.alldesign.tw )
聯成電腦評價 ( http://www.lccnet.tv/ )
聯成評價 ( http://expert.lccnet.com.tw/forum.php )