<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Excel VBA 變量,數據類型&常量 > 原文: [https://www.guru99.com/vba-data-types-variables-constant.html](https://www.guru99.com/vba-data-types-variables-constant.html) 幾乎所有計算機程序中都使用變量,VBA 也不例外。 在過程開始時聲明變量是一個好習慣。 這不是必需的,但有助于識別內容的性質(文本,??數據,數字等) 在本教程中,您將學習- * [VBA 變量](#1) * [VBA 數據類型](#2) * [VBA 中的常量](#3) ## VBA 變量 變量是存儲在計算機內存或存儲系統中的特定值。 以后,您可以在代碼中使用該值并執行。 計算機將從系統中獲取該值并顯示在輸出中。 必須為每個變量指定一個名稱。 要在 VBA 中命名變量,您需要遵循以下規則。 * 不得超過 255 個字符 * 不允許有空格 * 它不能以數字開頭 * 期間不允許 這是 VBA 中變量的有效名稱和無效名稱的一些示例。 | [![VBA Data Types, Variables & Constant](https://img.kancloud.cn/b2/48/b248accf168c6473f06969ef85e81f06_31x30.png "VBA Data Types, Variables & Constant") ](/images/vba/062416_1101_VBADataType1.png) **有效名稱** | [![VBA Data Types, Variables & Constant](https://img.kancloud.cn/41/c6/41c630306acf0d045b87e2fc4f94b0f4_27x30.png "VBA Data Types, Variables & Constant") ](/images/vba/062416_1101_VBADataType2.png) **無效名稱** | | 我的手表 | My.Watch | | 新車 1 | 1_NewCar(不以數字開頭) | | 員工 ID | 員工編號(不允許空格) | 在 VBA 中,我們需要在使用變量之前聲明變量,方法是分配名稱和數據類型。 在 VBA 中,變量是隱式或顯式聲明的。 * **Implicitly**: Below is an example of a variable declared Implicitly. * 標簽= Teacher99 * 音量= 4 * **顯式**:以下是顯式聲明的變量的示例。 您可以在語法中使用“昏暗”關鍵字 * **昏暗** Num 為整數 * **昏暗**密碼為字符串 VBA 變量與其他編程語言沒有區別。 要在 VBA 中聲明變量,請使用關鍵字**“ Dim”。** VBA 變量的**語法** 要在 VBA 中聲明變量,請鍵入 Dim,然后輸入名稱: ``` Sub Exercise () Dim <name> End Sub ``` 在執行變量之前,我們必須在 Excel 中記錄一個宏。 要記錄宏,請執行以下操作- **步驟 1)**:記錄宏 1 **步驟 2)**:停止宏 1 **步驟 3)**:打開宏編輯器,在 Macro1 中輸入變量代碼 **步驟 4)**:執行宏 1 的代碼 示例,對于 VBA 變量 ``` Sub Macro1() Dim Num As Integer Num = 99 MsgBox " Guru " & Num End Sub ``` 運行此代碼時,您將在工作表中獲得以下輸出。 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/07/70/0770a334c9e92f41193a3512f6f962fc_174x169.png "VBA Data Types, Variables & Constant") ## Excel VBA 數據類型 計算機無法區分數字(1,2,3 ..)和字符串(a,b,c,..)。 為了進行區分,我們使用數據類型。 VBA 數據類型可以分為兩種類型 * **數值數據類型** | **類型** | **儲存空間** | **值范圍** | | 字節 | 1 個字節 | 0 至 255 | | 整數 | 2 字節 | -32,768 至 32,767 | | Long | 4 字節 | -2,147,483,648 至 2,147,483,648 | | 單 | 4 bytes | -3.402823E + 38 至-1.401298E-45(負值)1.401298E-45 至 3.402823E + 38(正值)。 | | 雙 | 8 字節 | -1.79769313486232e + 308 至-4.94065645841247E-324 為負值 4.94065645841247E-324 至 1.79769313486232e + 308 為正值。 | | 貨幣 | 8 bytes | -922,337,203,685,477.5808 至 922,337,203,685,477.5807 | | 小數 | 12 字節 | +/- 79,228,162,514,264,337,593,543,950,335(如果不使用小數點)+/- 7.9228162514264337593543950335(小數點后 28 位) | * **非數值數據類型** | **數據類型** | **使用的字節數** | **值范圍** | | 字符串(固定長度) | 字符串長度 | 1 至 65,400 個字符 | | 字符串(可變長度) | 長度+ 10 個字節 | 0 至 20 億個字符 | | 布爾型 | 2 bytes | 對或錯 | | 日期 | 8 bytes | 100 年 1 月 1 日至 9999 年 12 月 31 日 | | 目的 | 4 bytes | 任何嵌入式對象 | | 變體(數字) | 16 字節 | 任何與 Double 一樣大的值 | | 變體(文字) | 長度+ 22 個字節 | 與可變長度字符串相同 | 在 VBA 中,如果未指定**數據類型,它將自動將變量聲明為 Variant。** 讓我們看一個有關如何在 VBA 中聲明變量的示例。 在此示例中,我們將聲明三種類型的變量字符串,即連接日期和貨幣。 **步驟 1)**就像在上一個教程中一樣,我們將在我們的 Excel 工作表中插入 commandButton1。 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/12/54/125489df4c2d98cb23150cdc741e2197_168x80.png "VBA Data Types, Variables & Constant") **步驟 2)**在下一步中,右鍵單擊按鈕,然后選擇查看代碼。 它將打開代碼窗口,如下所示。 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/4d/f1/4df16eefdebbc744e29878a58c630f66_562x230.png "VBA Data Types, Variables & Constant") **步驟 3)**在此步驟中, * 通過單擊保存按鈕 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/04/bd/04bd240827553f51168e37b5f3c70568_22x19.png "VBA Data Types, Variables & Constant") 保存文件 * 然后在同一窗口中單擊 Excel 圖標 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/48/b6/48b65d821497c32934d5acf28fcdc3b1_21x18.png "VBA Data Types, Variables & Constant") 以返回 Excel 工作表。 * 您可以看到設計模式以綠色突出顯示為“打開” ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/cb/40/cb40003021db95d858080a06d0e3fdca_274x405.png "VBA Data Types, Variables & Constant") **步驟 4)**在單擊命令按鈕之前,關閉設計模式 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/92/8f/928f3482ca235eb0c1d4e1a9509d3d70_327x405.png "VBA Data Types, Variables & Constant") **步驟 5)**關閉設計模式后,將單擊 commandButton1。 它將顯示以下變量作為我們在代碼中聲明的范圍的輸出。 * Name * 加盟日期 * 貨幣收入 ![VBA Data Types, Variables & Constant](https://img.kancloud.cn/96/ee/96ee79ca166116dc59f19fa27ab90b59_682x379.png "VBA Data Types, Variables & Constant") ## VBA 中的常量 常量就像變量一樣,但是您不能對其進行修改。 要在 VBA 中聲明常量,請使用關鍵字 **Const** 。 常數有兩種類型, * 應用程序提供的內置或內置的。 * 符號或用戶定義 您可以將范圍默認指定為**私有**或**公共**。 例如, 年公共公休日= 365 私人監工工作日= 250 下載包含以上代碼的 Excel [下載上面的 Excel 代碼](https://drive.google.com/uc?export=download&id=0ByI5-ZLwpo25Q0loZ2VFZ2tUNkU) **摘要**: * 變量是存儲在計算機內存或存儲系統中的特定值。 * 您可以在語法中使用“ Dim”關鍵字來顯式聲明變量 * VBA 數據類型可以分為兩種類型 * 數值數據類型 * 非數值數據類型 * 在 VBA 中,如果未指定數據類型。 它將自動將變量聲明為 Variant * 常量就像變量一樣,但是您不能對其進行修改。 要在 VBA 中聲明常量,請使用關鍵字 **Const** 。
                  <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>

                              哎呀哎呀视频在线观看