Excel中VBA相关函数的使用
生活随笔
收集整理的這篇文章主要介紹了
Excel中VBA相关函数的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自定義函數實例-單元顏色求和(字典+自定義函數)
Function COLORSUM(單元格區域 As range, 匯總的顏色 As range) Set d = CreateObject("Scripting.Dictionary") For Each Rng In 匯總的顏色d(Rng.Interior.ColorIndex) = "" Next For Each ci In d.keysFor Each Rng In 單元格區域If Rng.Interior.ColorIndex = ci Thenr = r + Rng.ValueEnd IfNextNext COLORSUM = r End Function Sub test() Set d = CreateObject("Scripting.Dictionary") Set 區域 = Application.InputBox("區域選擇", , , , , , , 8) Set 顏色 = Application.InputBox("顏色選擇", , , , , , , 8) For Each Rng In 顏色d(Rng.Interior.ColorIndex) = "" Next For Each ci In d.keysFor Each Rng In 區域If Rng.Interior.ColorIndex = ci Thenr = r + Rng.ValueEnd IfNextNext MsgBox rEnd Sub自定義函數實例-反轉字符與數字求和(正則+自定義函數)
Function 求和(rng As Range, Optional s As String = "") Application.VolatileSet regx = CreateObject("vbscript.regexp") With regx.Global = True.Pattern = "\d" & sSet mat = .Execute(rng) End With For Each m In mat n = n + m * 1 Next 求和 = n End Function Function DD(rng As Range) '反轉字符 For i = Len(rng) To 1 Step -1a = Mid(rng, i, 1)b = b & a NextDD = b End Function自定義函數實例-提取不重復值(字典+正則+自定義函數)
Function 不重復2(rng As Range, Optional num As Integer = 0) Set d = CreateObject("scripting.dictionary") Set regx = CreateObject("vbscript.regexp") With regx.Global = TrueIf num = 0 Then.Pattern = ".+" '所有值的不重復ElseIf num = 1 Then.Pattern = "[一-龢]+" '漢字不重復ElseIf num = 2 Then.Pattern = "[a-zA-Z]+" '字母不重復ElseIf num = 3 Then.Pattern = "\d+" '數字不重復End If For Each rn In rngFor Each m In .Execute(rn)d(m.Value) = ""Next Next 不重復2 = d.keys End With End Function Function 不重復值(rng As Range) Set d = CreateObject("scripting.dictionary") For Each rn In rngd(rn.Value) = "" Next 不重復值 = d.keys End Function總結
以上是生活随笔為你收集整理的Excel中VBA相关函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kali安装magescan评估工具
- 下一篇: [react] 举例说明在react中怎