iT邦幫忙

3

Excel VBA 第一篇 -- 基礎介面介紹

以下是使用 Excel 2016 示範

環境設定

第一步:開啟開發人員

開啟Excel後上方工具列呈現大概如圖是
https://ithelp.ithome.com.tw/upload/images/20201030/20132241Bx1ImktBmQ.png
為了使用Excel VBA 我們需要開啟開發人員的選項

  1. 依序點擊檔案 => 選項 後會看到下方圖示
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241m8CAKpXYMm.png
  2. 依序點擊完成後,回到Excel確認上方工具欄是否出現開發人員的選項!
    https://ithelp.ithome.com.tw/upload/images/20201030/201322416TeIDgAeMV.png

第二步:開啟Visual Basic

  1. 點擊開發人員選項中的Visual Basic
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241VMBhW8la1Q.png
  2. 點擊後會出現下列頁面
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241CAv4u1fBSK.png
    如果出現則代表成功完成前置設定!/images/emoticon/emoticon12.gif

基礎介面介紹

VBA project

https://ithelp.ithome.com.tw/upload/images/20201030/201322416grQOzjWoI.png

  1. VBAProject(活頁簿1.xlsx) => 代表一個名稱為活頁簿1.xlsx的Excel檔案開啟中
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241w6aS9dKZra.png
  2. VBAProject(活頁簿1.xlsx)中的工作表1(工作表1) => 代表活頁簿1.xlsx中有名稱為工作表1的工作表存在
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241ERWlMW3iGE.png
  3. 點擊工作表1(工作表1)後右方會出現一個空白區域,那就是我們VBA可以輸入的地方之一
    https://ithelp.ithome.com.tw/upload/images/20201030/20132241nddDz3JJD0.png

嘗試第一個VBA程式

在剛剛的空白區域中輸入以下型式,巨集名稱可以自行定義,但是要符合其規定,不能使用到特定名稱

sub 巨集名稱()
    所希望執行的程式內容
end sub

了解後我們就可以實作Hello World!
稍微更改一下剛剛的文字,填入實際名稱與內容!

Msgbox在執行後會彈出式訊息方塊

sub Hello()
    Msgbox "Hello World!"
end sub

輸入完成後,按下上方工具列的綠色三角形或是執行中的綠色三角形都可以
https://ithelp.ithome.com.tw/upload/images/20201030/20132241yrCye49O1E.png
順利的話就可以看到訊息方塊彈出
https://ithelp.ithome.com.tw/upload/images/20201030/201322417rOPfpckeA.png
如果出現下方圖示之情形,只需要按下再次按下執行即可
https://ithelp.ithome.com.tw/upload/images/20201030/20132241ycgeD38UBt.png

踏進Excel VBA第一步達成!!/images/emoticon/emoticon30.gif



圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
fannyliu
iT邦新手 4 級 ‧ 2020-11-02 10:05:58

謝謝您辛苦了!
好仔細的圖文說明,
期待後續教學。

我要留言

立即登入留言