excel巨集的用法
阿新 • • 發佈:2020-10-21
在不少時間excel中並沒有一些我們想要的函式,這時候我們可以在*.xls[x]的中定義巨集,定義了巨集後需要注意兩項問題:
- 1)檔案需要儲存為*.xlsm(否則儲存為*.xls[x]會丟失巨集函式);
- 2)再次開啟*.xlsm時,會提示是否啟動巨集,必須啟用才能使用巨集,否則將會禁用巨集。
在*.xls[x]中定義巨集函式:
我這裡希望對一個字串拆分,比如:希望將A列中‘[1, 2, 3, 10, 11]’的資料拆分為C,D,E,F,G 5列。
此時在excel的選單-》工具-》巨集-》Visual Basic 編輯器
之後會開啟巨集編輯器,在選單-》插入-》模組,插入“模組1”,“模組2”
在模組1中寫入拆分函式:
Function splitFunc(Rng As Range, splitChar As String, idx As Integer) As String Dim replaceChar As String '替換[、] replaceChar = Replace(Rng.Text, "[", "") replaceChar = Replace(replaceChar, "]", "") '按照指定字串進行分割,然後返回指定分割後陣列項 splitFunc = Split(replaceChar, splitChar)(idx)End Function
用法:
在模組2中寫入是否連續判斷函式:
Function isLinkNum(Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range, Rng5 As Range) As Integer Dim c1 As Integer Dim c2 As Integer Dim c3 As Integer Dim c4 As Integer Dim c5 As Integer c1 = CInt(Rng1.Text) c2 = CInt(Rng2.Text) c3= CInt(Rng3.Text) c4 = CInt(Rng4.Text) c5 = CInt(Rng5.Text) If ((c5 - c4 = 1) And (c4 - c3 = 1) And (c3 - c2 = 1) And (c2 - c1 = 1)) Then isLinkNum = 1 Else isLinkNum = 0 End If End Function
用法: