【Excel教學】在VBA中如何快速調用、使用儲存格
文、意如老師
認識Excel 儲存格調用方式
儲存格是由欄、列組成的,接下來要來認識幾個函數來調用儲存格。
函式:Cells(列、欄)
例如:列2欄5對應的儲存格是E2
使用方式:Cells ( 2 , 5) ,也可使用Cells(2 , E)。
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img1](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image001.png?fit=800%2C424&ssl=1)
讀出儲存格上面的值
操作測試:
到E2 輸入任一值,例如 15
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img2](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image003-1.png?fit=800%2C327&ssl=1)
製作彈窗並顯示E2中的值。
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img3](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image005-1.png?fit=548%2C426&ssl=1)
程式碼:
a = Cells(2, 5)
MsgBox (a)
End Sub
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img4](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image007.png?fit=800%2C449&ssl=1)
選取單格、或一個區域:
選取單格:方式一(使用Cells):
例選取2列5行的儲存格
Cells(2,5).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img5](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image009.png?fit=800%2C243&ssl=1)
程式碼參考如下:
Cells(2, 5).Select
End Sub
方式二(使用Range):
選取單一儲存格,例如E2儲存格
Range(“E2”).Select
選取一個範圍,例如B3:E6 範圍
Range(“B3:E6”).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img6](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image011.png?fit=800%2C254&ssl=1)
也可以使用Range(Cells(2, 5), Cells(3, 6)).Select來選擇一個範圍
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img7](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image011.png?fit=800%2C254&ssl=1)
跳著選取多格儲存格
Range(“A1,B2,A3,B4”).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img8](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image015.png?fit=800%2C251&ssl=1)
選取單一欄:
Range(“B:B”).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img9](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image017.png?fit=747%2C310&ssl=1)
一次選取多欄:
Range(“B:B,E:E”).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img10](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image019.png?fit=800%2C290&ssl=1)
如要選取列的話只要把欄英文改列數字即可
Range(“2:2,5:5”).Select
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img11](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image021.png?fit=800%2C238&ssl=1)
Cells( )與Range( )賦予儲存格值的使用方式
使用Cells在儲存格中填入123
Cells(2, 3) = 123
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img12](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image023.png?fit=800%2C332&ssl=1)
使用Range範圍中(B2:C5)中填入100
Range(“B2”, “C5”) = 100
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img13](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image025.png?fit=800%2C258&ssl=1)
Cells跟Range很像但差別在哪裡?
主要差別在Cells更為彈性,可以使用變數的方式填入,例如想要在C1到C5的儲存格寫入倍數5的數字,即可使用for迴圈來實現,將變數值填入Cells中。
參考程式碼:
For i = 1 To 5
Cells(i, 3) = i * 5
Next
End Sub
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img14](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image027.png?fit=800%2C349&ssl=1)
儲存格樣式設定
首先在B2 輸入一個預設文字Hello World
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img15](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image029.png?fit=646%2C294&ssl=1)
將B2中的文字設定大小為16
Range(“B2”).Font.Size = 16
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img16](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image031.png?fit=800%2C206&ssl=1)
將B2中的文字設定顏色為綠色
Range(“B2”).Font.Color = RGB(0, 255, 0)
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img17](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image033.png?fit=800%2C249&ssl=1)
將B2中的背景改成藍色
Range(“B2”).Interior.Color = RGB(0, 0, 255)
![[office-yiru-excel-teaching-how-to-quickly-call-and-use-storage-cells-in-vba]for ai.rookiesavior lesson img18](https://i0.wp.com/rookiesavior.net/wp-content/uploads/2023/01/image035.png?fit=800%2C338&ssl=1)
將B2設定為粗體
Range(“B2”).Font.Bold = true
調整B2欄位寬度
Range(“B2”).ColumnWidth = 25
將B2設定為自動調整欄寬
Range(“B2”).EntireColumn.AutoFit
將B2外框設定為雙框線
Range(“B2”).Borders.LineStyle = xlDouble
清除B2的資料格式
Range(“B2”).ClearFormats
清除B2儲存格內容
Range(“B2”).ClearContents
關於如何使用VBA 程式來選取、複製與貼上資料及如何調用其他工作表與活頁簿上的資料,將在下一篇文章詳細介紹。

加入菜鳥救星學習會員,不定期獲得更多知識吧!點我註冊 / 登入
不同頻道,不同知識 !
塔羅牌 | 秘儀解析-基礎入門
SEO 入門|初學者的實戰攻略


