VBA常用函数及记事

  1.  '将 A 转成 1
  2. Function ConvertExcelNumToInt(colName As StringAs Integer
  3.     Dim i As Integer
  4.     Dim rtn As Integer
  5.     If Len(colName) = 0 Then
  6.         ConvertExcelNumToInt = 0
  7.         Exit Function
  8.     End If
  9.     colName = UCase(colName)
  10.     rtn = 0
  11.     For i = 1 To Len(colName)
  12.         rtn = rtn * 26
  13.         rtn = rtn + Asc(Mid(colName, i, 1)) - 64
  14.     Next
  15.     ConvertExcelNumToInt = rtn
  16. End Function
  17. '将 1 转成 A
  18. Function ConvertToLetter(iCol As IntegerAs String
  19.    Dim iAlpha As Integer
  20.    Dim iRemainder As Integer
  21.    iAlpha = Fix(iCol / 26)
  22.    iRemainder = iCol - (iAlpha * 26)
  23.    If iAlpha > 0 Then
  24.      If iRemainder = 0 Then
  25.         iAlpha = iAlpha - 1
  26.         iRemainder = 26
  27.       End If
  28.       If (iAlpha > 0) Then
  29.         ConvertToLetter = Chr(iAlpha + 64)
  30.       End If
  31.    End If
  32.    If iRemainder > 0 Then
  33.       ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
  34.    End If
  35. End Function

  36. '取得AutoFilter的行号列表
  37. Function GetAutoFilterResult(sh As Worksheet) As VBA.Collection
  38. Dim rtn As New VBA.Collection
  39. Dim curCell As Range
  40. countchange = WorksheetFunction.Subtotal(3, sh.AutoFilter.Range)
  41. If (countchange = sh.AutoFilter.Range.Columns.Count) Then
  42.     Exit Function
  43. End If
  44. Set curCell = sh.Cells(sh.AutoFilter.Range.Row + 1, 1)
  45. While Not IsEmpty(curCell)           ' Check to see if row
  46.                                          ' height is zero.
  47.     If curCell.RowHeight > 0 Then
  48.         rtn.Add (curCell.Row)
  49.     End If
  50.     Set curCell = curCell.Offset(1, 0)
  51. Wend
  52. Set GetAutoFilterResult = rtn
  53. End Function
  1. '记事..
  2. Sub dictionarySample()
  3.     Dim dic ' As New Dictionary
  4.     Dim d As New VBA.Collection
  5.     
  6.     
  7.     d.Add ("1")
  8.     
  9.     d.Remove (1)
  10.     
  11.     Set dic = CreateObject("scripting.dictionary")
  12.     
  13.     
  14.     dic.Add "key""Dictionary"
  15.     s = dic.Item("key")
  16.     
  17.     'Add(key,item) 增加键/条目对到 Dictionary
  18.     'Exists (key) 如果指定的键存在,返回 True,否则返回 False
  19.     'Items() 返回一个包含 Dictionary 对象中所有条目的数组
  20.     'Keys()  返回一个包含 Dictionary 对象中所有键的数组
  21.     'Remove (key) 删除一个指定的键/条目对
  22.     'RemoveAll() 删除全部键/条目对
  23.     
  24.     
  25.     
  26. End Sub

原文地址:https://www.cnblogs.com/huigll/p/1993595.html