<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何使用示例在 Excel 中創建 Visual Basic for Applications(VBA) > 原文: [https://www.guru99.com/creating-your-first-visual-basic-for-applications-vba-in-excel.html](https://www.guru99.com/creating-your-first-visual-basic-for-applications-vba-in-excel.html) ``` Everybody in this country should learn how to program a computer... because it teaches you how to think." -Steve Jobs ``` 我想表達史蒂夫·喬布斯的明智話,并說世界上每個人都應該學習如何對計算機編程。 您可能不必完全以程序員的身份工作或編寫程序,但是它將教會您如何思考。 在本教程中,我們將涵蓋以下主題。 * [什么是 VBA?](#1) * [為什么使用 VBA?](#2) * [VBA 在 excel](#3) 中的個人&商業應用 * [Visual Basic for Applications VBA 基礎知識](#4) * [在 Excel 中創建簡單的 EMI 計算器的分步示例](#5) ## 什么是 VBA? **VBA 代表 Visual Basic for Applications** 。 在進一步研究細節之前,讓我們看一下外行語言中的計算機編程。 假設你有一個女傭。 如果您想讓女傭打掃房間并洗衣服。 您用英語告訴她該怎么做,而她會為您完成工作。 在使用計算機時,您將需要執行某些任務。 就像您告訴女仆做家務一樣,您也可以告訴計算機為您完成任務。 告訴計算機您想要它為您做什么的過程稱為計算機編程。 就像您用英語告訴女仆該做什么一樣,您也可以使用類似英語的陳述來告訴計算機該做什么。 像英語一樣的陳述屬于高級語言類別。 VBA 是一種高級語言,您可以用來將 excel 屈服于所有強大的意志。 VBA 實際上是 Visual Basic 6.0 BASIC 的子集,代表 **B** 實例 **A** 通用 **S** 符號 **I** 指令 **C** ode。 ## 為什么使用 VBA? * 它使用類似英語的語句來編寫指令 * 創建用戶界面就像使用繪畫程序。 您只需要拖放,對齊圖形用戶界面控件即可。 * 學習曲線短。 從您開始學習的第一天起,您就可以立即開始編寫簡單的程序。 * 通過允許您使 excel 表現出所需的方式來增強 excel 的功能 ## VBA 在 Excel 中的個人&業務應用程序 對于個人用途,您可以將其用于簡單的宏,這些宏將自動執行大多數例行任務。 閱讀有關宏的文章,以獲取有關如何實現此目的的更多信息。 對于商業用途,您可以創建由 excel 和 VBA 支持的完整,功能強大的程序。 這種方法的優點是您可以在自己的自定義程序中利用 excel 的強大功能。 ## Visual Basic for Applications VBA 基礎 在編寫任何代碼之前,我們需要首先了解基礎知識。 以下基礎知識將幫助您入門。 * **變量** –在中學時,我們學習了代數。 查找(x + 2y)其中 x = 1 和 y =3。在此表達式中,x 和 y 是變量。 如本例所示,它們可以被分配任何數字,即分別為 1 和 3。 它們也可以分別更改為 4 和 2。 簡而言之,變量是存儲位置。 在使用 VBA 時,也將需要聲明變量,就像在代數類中一樣 * **創建變量的規則** * 不要使用保留字-如果您是學生,則不能使用職稱講師或校長。 這些頭銜僅供講師和學校當局使用。 保留字是在 Vba 中具有特殊含義的那些字,因此,不能將它們用作變量名。 * 變量名稱不能包含空格–您不能定義名為第一個數字的變量。 您可以使用 firstNumber 或 first_number。 * 使用描述性名稱-以您自己的名字命名變量很誘人,但請避免這樣做。 使用描述性名稱,例如數量,價格,小計等,這將使您的 VBA 代碼易于閱讀 * 算術運算符-除法乘法,加法和減法(BODMAS)的括號規則適用,因此在使用使用多個不同算術運算符的表達式時,請記住應用它們。 就像在 excel 中一樣,您可以使用 * +用于加法 * -減法 * *用于乘法 * /用于劃分。 * 邏輯運算符-早期教程中介紹的邏輯運算符的概念在使用 VBA 時也適用。 其中包括 * 如果陳述 * 要么 * 不 * 和 * 真正 * 假 ## 啟用開發者選項 * 創建一個新的工作簿 * 單擊功能區開始按鈕 * 選擇選項 * 單擊自定義功能區 * 選中開發者復選框,如下圖所示 * 點擊確定 ![Introduction to Macros in Excel](https://img.kancloud.cn/d0/9d/d09d1ed742030b9e2800807d0f9f40c5_670x533.png "Introduction to Macros in Excel") 現在,您將能夠在功能區中看到“開發人員”選項卡 ### VBA Hello world 現在,我們將演示如何在 VBA 中編程。 VBA 中的所有程序都必須以“ Sub”開頭,并以“ End sub”結尾。 這里的名稱是您要分配給程序的名稱。 sub 代表一個子例程,我們將在本教程的后面部分學習該子例程。 ``` Sub name() . . . End Sub ``` 我們將創建一個基本的 VBA 程序,該程序將顯示一個輸入框,詢問用戶名,然后顯示問候消息 本教程假定您已完成 excel 中有關宏的教程,并已在 excel 中啟用了 DEVELOPER 選項卡。 * 創建一個新的工作簿 * 將其保存為啟用了 Excel 宏的工作表格式* .xlsm * 單擊開發人員選項卡 * 單擊控件功能區欄下的 INSERT 下拉框 * 選擇一個命令按鈕,如下圖所示 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/df/1c/df1ca57af4aa8e571ddd3c623c116c33_617x439.png "Creating your First Visual Basic for Applications (VBA) in Excel") 在工作表上的任意位置繪制命令按鈕 您將獲得以下對話窗口 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/d1/f9/d1f97ec02a86abdba49a36b68cee3c48_386x370.png "Creating your First Visual Basic for Applications (VBA) in Excel") * 將宏名稱重命名為 btnHelloWorld_Click * 點擊新按鈕 * 您將獲得以下代碼窗口 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/a1/6c/a16c2d7fd73c61227b97d4a70c87f52f_898x509.png "Creating your First Visual Basic for Applications (VBA) in Excel") 輸入以下指令代碼 ``` Dim name As String name = InputBox("Enter your name") MsgBox "Hello " + name ``` 這里, * **“將名稱作為字符串存儲”** 創建一個名為 name 的變量。 該變量將接受文本,數字和其他字符,因為我們將其定義為字符串 * **“名稱= InputBox(“輸入您的名稱”)“** 調用內置函數 InputBox,該功能顯示帶有標題輸入您的名稱的窗口。 輸入的名稱然后存儲在名稱變量中。 * “ **MsgBox” Hello“ +名稱”** 調用內置函數 MsgBox,該函數顯示 Hello 和輸入的名稱。 您的完整代碼窗口現在應如下所示 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/45/f4/45f4b023f2d64bbfa1a9d9be4d84bdad_694x394.png "Creating your First Visual Basic for Applications (VBA) in Excel") * 關閉代碼窗口 * 右鍵單擊按鈕 1 并選擇編輯文本 * 輸入問好 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/2d/22/2d22aef4c07950b73b59f9a235013541_330x273.png "Creating your First Visual Basic for Applications (VBA) in Excel") * 點擊說你好 * 您將獲得以下輸入框 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/97/11/9711417d18a4233a6999a9dac8d734ef_373x159.png "Creating your First Visual Basic for Applications (VBA) in Excel") * 輸入您的名字,即喬丹 * 您將收到以下消息框 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/54/bc/54bc08587559aa4c08b4f4bdac6d8808_161x155.png "Creating your First Visual Basic for Applications (VBA) in Excel") 恭喜,您剛剛在 excel 中創建了您的第一個 VBA 程序 ## 在 Excel 中創建簡單的 EMI 計算器的分步示例 在本教程中,我們將創建一個簡單的程序來計算 EMI。 EMI 是“每月等額分期付款”的縮寫。 這是您獲得貸款時每月償還的金額。 下圖顯示了用于計算 EMI 的公式。 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/8a/a8/8aa8765f288e5c0c8ca45caf79e692da_687x181.png "Creating your First Visual Basic for Applications (VBA) in Excel") 上面的公式很復雜,可以用 excel 編寫。 好消息是 excel 已經解決了上述問題。 您可以使用 PMT 函數來計算上述值。 PMT 功能的工作方式如下 ``` =PMT(rate,nper,pv) ``` **此處,** * **“費率”** 這是月費率。 利率除以每年的還款次數 * **“ nper”** 是付款總數。 這是貸款期限乘以每年的還款次數 * **“ pv”** 當前值。 這是實際的貸款額 使用 excel 單元創建 GUI,如下所示 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/3e/2f/3e2f84d02d0510bf9abf85aa0b29dc2c_432x371.png "Creating your First Visual Basic for Applications (VBA) in Excel") 在第 7 行和第 8 行之間添加命令按鈕 給按鈕宏名稱 btnCalculateEMI_Click 點擊編輯按鈕 輸入以下代碼 ``` Dim monthly_rate As Single, loan_amount As Double, number_of_periods As Single, emi As Double monthly_rate = Range("B6").Value / Range("B5").Value loan_amount = Range("B3").Value number_of_periods = Range("B4").Value * Range("B5").Value emi = WorksheetFunction.Pmt(monthly_rate, number_of_periods, -loan_amount) Range("B9").Value = emi ``` **HERE,** * **“ Dim month_rate as Single,…”** Dim 是用于在 VBA 中定義變量的關鍵字,monthly_rate 是變量名稱,Single 是表示變量將接受數字的數據類型。 * **“ monthly_rate = Range(” B6“)。Value / Range(” B5“)。Value”** Range 是用于從 VBA 訪問 Excel 單元格的函數,Range(“ B6”)。Value 引用 B6 中的值 * **“ WorksheetFunction.Pmt(…)”** WorksheetFunction 是用于訪問 excel 中所有功能的函數 下圖顯示了完整的源代碼 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/b1/c0/b1c02795d2a676202a9afad1d53e57b7_886x287.png "Creating your First Visual Basic for Applications (VBA) in Excel") * 單擊保存并關閉代碼窗口 * 如下面的動畫圖像所示測試您的程序 ![Creating your First Visual Basic for Applications (VBA) in Excel](https://img.kancloud.cn/7f/ea/7feaa70d5227aebd32230474f10d81bb_306x205.png "Creating your First Visual Basic for Applications (VBA) in Excel") ## 示例 2 **步驟 1)**在主菜單的“開發人員”選項卡下,單擊“ Visual Basic”圖標,它將打開您的 VBA 編輯器。 ![What is VBA?](https://img.kancloud.cn/21/6c/216c3e65e35746f6002c0266ca6917bb_213x162.png "What is VBA?") **步驟 2)**將打開 VBA 編輯器,從中可以選擇要在其中運行代碼的 Excel 工作表。 要打開 VBA 編輯器,請雙擊工作表。 ![What is VBA?](https://img.kancloud.cn/15/ae/15ae3d1406fa1d43cf3c84a328d26dfa_194x214.png "What is VBA?") 它將在文件夾的右側打開一個 VBA 編輯器。 它將看起來像一個空白。 ![What is VBA?](https://img.kancloud.cn/17/97/1797b650023415a46de9283195a1b5f4_635x300.png "What is VBA?") **步驟 3)**在這一步中,我們將看到第一個 VBA 程序。 要閱讀和顯示我們的程序,我們需要一個對象。 在 VBA 中,該對象或媒體位于 MsgBox 中。 * 首先,輸入“ Sub”,然后輸入“程序名稱”(Guru99) * 在 MsgBox 中寫任何您想顯示的內容(學習 guru99 很有趣) * 通過 End Sub 結束程序 ![What is VBA?](https://img.kancloud.cn/9b/aa/9baadb9cecbe5aacc28a3f21189d9ff0_296x171.png "What is VBA?") **步驟 4)**在下一步中,您必須通過單擊編輯器菜單頂部的綠色運行按鈕來運行此代碼。 ![What is VBA?](https://img.kancloud.cn/39/cc/39cc7c92ad3ba203c0b450a7ea3e69b8_548x246.png "What is VBA?") **步驟 5)**運行代碼時,將彈出另一個窗口。 在這里,您必須選擇要在其中顯示程序的工作表,然后單擊“運行”按鈕 ![What is VBA?](https://img.kancloud.cn/04/07/04079f810c32b9a4e2c7f9b0ba947c91_443x337.png "What is VBA?") **步驟 6)**單擊運行按鈕時,程序將被執行。 它將在 MsgBox 中顯示味精。 ![What is VBA?](https://img.kancloud.cn/b7/fc/b7fc645162bce6822843ec58c61cf1d9_338x246.png "What is VBA?") [下載上面的 Excel 代碼](https://drive.google.com/uc?export=download&id=0BwL5un1OyjsddWN3Szc3TE9RTTg) ## 摘要 VBA 代表 Visual Basic for Application。 它是 Visual Basic 編程語言的子組件,可用于在 excel 中創建應用程序。 使用 VBA,您仍然可以利用 excel 的強大功能,并在 VBA 中使用它們。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看