VBA常用函数及记事
- '将 A 转成 1
- Function ConvertExcelNumToInt(colName As String) As Integer
- Dim i As Integer
- Dim rtn As Integer
- If Len(colName) = 0 Then
- ConvertExcelNumToInt = 0
- Exit Function
- End If
- colName = UCase(colName)
- rtn = 0
- For i = 1 To Len(colName)
- rtn = rtn * 26
- rtn = rtn + Asc(Mid(colName, i, 1)) - 64
- Next
- ConvertExcelNumToInt = rtn
- End Function
- '将 1 转成 A
- Function ConvertToLetter(iCol As Integer) As String
- Dim iAlpha As Integer
- Dim iRemainder As Integer
- iAlpha = Fix(iCol / 26)
- iRemainder = iCol - (iAlpha * 26)
- If iAlpha > 0 Then
- If iRemainder = 0 Then
- iAlpha = iAlpha - 1
- iRemainder = 26
- End If
- If (iAlpha > 0) Then
- ConvertToLetter = Chr(iAlpha + 64)
- End If
- End If
- If iRemainder > 0 Then
- ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
- End If
- End Function
- '取得AutoFilter的行号列表
- Function GetAutoFilterResult(sh As Worksheet) As VBA.Collection
- Dim rtn As New VBA.Collection
- Dim curCell As Range
- countchange = WorksheetFunction.Subtotal(3, sh.AutoFilter.Range)
- If (countchange = sh.AutoFilter.Range.Columns.Count) Then
- Exit Function
- End If
- Set curCell = sh.Cells(sh.AutoFilter.Range.Row + 1, 1)
- While Not IsEmpty(curCell) ' Check to see if row
- ' height is zero.
- If curCell.RowHeight > 0 Then
- rtn.Add (curCell.Row)
- End If
- Set curCell = curCell.Offset(1, 0)
- Wend
- Set GetAutoFilterResult = rtn
- End Function
- '记事..
- Sub dictionarySample()
- Dim dic ' As New Dictionary
- Dim d As New VBA.Collection
- d.Add ("1")
- d.Remove (1)
- Set dic = CreateObject("scripting.dictionary")
- dic.Add "key", "Dictionary"
- s = dic.Item("key")
- 'Add(key,item) 增加键/条目对到 Dictionary
- 'Exists (key) 如果指定的键存在,返回 True,否则返回 False
- 'Items() 返回一个包含 Dictionary 对象中所有条目的数组
- 'Keys() 返回一个包含 Dictionary 对象中所有键的数组
- 'Remove (key) 删除一个指定的键/条目对
- 'RemoveAll() 删除全部键/条目对
- End Sub
分类:
VBA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!