Excel VBA 字串 (String) 詳細介紹
在 VBA 中,字串 (String) 是一種資料類型,用於儲存文字資訊,例如姓名、地址、訊息等。以下將詳細介紹 VBA 字串的各種操作與應用。
1. 字串的基本概念
(1) 字串的宣告與賦值
Dim myString As String ' 宣告一個字串變數
myString = "Hello VBA!" ' 賦值
- 字串必須用 雙引號 " " 包圍。
- 可以是空字串
""
,表示沒有內容。
使用 &
或 +
連接字串(建議使用 &
,避免與數學運算混淆):
Dim firstName As String, lastName As String, fullName As String
firstName = "John"
lastName = "Doe"
fullName = firstName & " " & lastName ' 結果:"John Doe"
2. 字串的常用操作
(1) 取得字串長度 (Len
)
Dim text As String
text = "Excel VBA"
MsgBox Len(text) ' 輸出:8
(2) 轉換大小寫 (UCase
, LCase
)
Dim originalText As String
originalText = "Hello World"
MsgBox UCase(originalText) ' 輸出:"HELLO WORLD"
MsgBox LCase(originalText) ' 輸出:"hello world"
(3) 截取子字串 (Left
, Right
, Mid
)
Left(字串, 長度)
:從左邊開始截取。Right(字串, 長度)
:從右邊開始截取。Mid(字串, 起始位置, 長度)
:從中間截取。
Dim exampleText As String
exampleText = "ABCDEFG"
MsgBox Left(exampleText, 3) ' 輸出:"ABC"
MsgBox Right(exampleText, 3) ' 輸出:"EFG"
MsgBox Mid(exampleText, 2, 4) ' 輸出:"BCDE"
(4) 尋找字串位置 (InStr
)
Dim searchText As String
searchText = "Find the word in this sentence."
MsgBox InStr(searchText, "word") ' 輸出:10("word" 起始位置)
- 如果找不到,返回
0
。
(5) 替換字串 (Replace
)
Dim original As String
original = "I like apples."
MsgBox Replace(original, "apples", "oranges") ' 輸出:"I like oranges."
(6) 去除空白 (Trim
, LTrim
, RTrim
)
Dim dirtyText As String
dirtyText = " Excel VBA "
MsgBox "[" & Trim(dirtyText) & "]" ' 輸出:"[Excel VBA]"(去頭尾)
MsgBox "[" & LTrim(dirtyText) & "]" ' 輸出:"[Excel VBA ]"(去左邊)
MsgBox "[" & RTrim(dirtyText) & "]" ' 輸出:"[ Excel VBA]"(去右邊)
(7) 分割字串 (Split
)
Dim csvData As String
csvData = "Apple,Orange,Banana"
Dim fruits() As String
fruits = Split(csvData, ",") ' 用逗號分割成陣列
MsgBox fruits(0) ' 輸出:"Apple"
MsgBox fruits(1) ' 輸出:"Orange"
3. 字串與數值的轉換
(1) 字串轉數字 (CInt
, CLng
, CDbl
, Val
)
Dim numStr As String
numStr = "123"
Dim num As Integer
num = CInt(numStr) ' 轉成 Integer
' Val 函數會讀取字串開頭的數字
MsgBox Val("100 points") ' 輸出:100
(2) 數字轉字串 (CStr
, Format
)
Dim number As Integer
number = 100
Dim strNum As String
strNum = CStr(number) ' 轉成字串 "100"
' 使用 Format 控制顯示格式
MsgBox Format(1234.567, "0.00") ' 輸出:"1234.57"(四捨五入)
4. 特殊字元與跳脫字元
- 換行符號:
vbNewLine
或Chr(10)
&Chr(13)
- Tab 鍵:
vbTab
或Chr(9)
- 雙引號:需要用兩個雙引號表示
""
MsgBox "Line 1" & vbNewLine & "Line 2" ' 換行顯示
MsgBox "He said, ""Hello""" ' 輸出:He said, "Hello"
5. 字串的進階應用
(1) 檢查字串是否為空
Dim checkStr As String
checkStr = ""
If checkStr = "" Then
MsgBox "字串是空的"
End If
' 或使用 Len 函數
If Len(checkStr) = 0 Then
MsgBox "字串是空的"
End If
(2) 字串比較 (StrComp
)
Dim result As Integer
result = StrComp("apple", "APPLE", vbTextCompare) ' 不區分大小寫
If result = 0 Then
MsgBox "字串相同"
End If
vbBinaryCompare
:區分大小寫(預設)。vbTextCompare
:不區分大小寫。
(3) 字串反轉 (自訂函數)
Function ReverseString(ByVal text As String) As String
Dim i As Integer
Dim reversed As String
For i = Len(text) To 1 Step -1
reversed = reversed & Mid(text, i, 1)
Next i
ReverseString = reversed
End Function
MsgBox ReverseString("VBA") ' 輸出:"ABV"
6. 實際應用範例
(1) 從儲存格讀取字串並處理
Sub ProcessCellText()
Dim cellText As String
cellText = Range("A1").Value ' 讀取 A1 儲存格內容
' 檢查是否包含特定關鍵字
If InStr(cellText, "重要") > 0 Then
MsgBox "此訊息包含「重要」!"
End If
End Sub
(2) 格式化日期字串
Sub FormatDateExample()
Dim today As Date
today = Date ' 取得今天日期
Dim dateStr As String
dateStr = Format(today, "yyyy/mm/dd") ' 格式化為 "2023/10/25"
MsgBox "今天是:" & dateStr
End Sub
總結
- 字串 (String) 用於儲存文字,使用
" "
包圍。 - 常用操作:連接 (
&
)、截取 (Left
,Right
,Mid
)、搜尋 (InStr
)、替換 (Replace
)。 - 轉換函數:
CStr
(轉字串)、CInt
/Val
(轉數字)。 - 特殊字元:
vbNewLine
(換行)、""
(表示雙引號)。 - 進階應用:字串比較 (
StrComp
)、分割 (Split
)、反轉 (自訂函數)。