1. 将 互换 Excel 列号(数字/字母)

Public Function excelColumn_numLetter_interchange(numOrLetter) As String
  Dim i, j, idx As Integer
  Dim letterArray

  letterArray = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")

  If IsNumeric(numOrLetter) Then
    If numOrLetter > 702 Then
      MsgBox "只允许输入小于“703”的数字。"
      Exit Function
    End If

    If numOrLetter > 26 Then
      idx = 26
      For i = 0 To 25
        For j = 0 To 25
          idx = idx + 1
          If idx = numOrLetter Then
            excelColumn_numLetter_interchange = letterArray(i) & letterArray(j)
            Exit For
          End If
        Next j
      Next i
    Else
      excelColumn_numLetter_interchange = letterArray(numOrLetter - 1)
    End If
  Else
    numOrLetter = UCase(numOrLetter) '转换为大写
    If Len(numOrLetter) > 1 And Len(numOrLetter) < 3 Then
      idx = 26
      For i = 0 To 25
        For j = 0 To 25
          idx = idx + 1
          If letterArray(i) & letterArray(j) = numOrLetter Then
            excelColumn_numLetter_interchange = idx
            Exit For
          End If
        Next j
      Next i
    ElseIf Len(numOrLetter) = 1 Then
      For i = 0 To 25
        If letterArray(i) = numOrLetter Then
          excelColumn_numLetter_interchange = i + 1
          Exit For
        End If
      Next i
    Else
      MsgBox "最多只允许输入2个“字母”。"
    End If
  End If
End Function


2. '将 字符串中的 html实体 转换成正常字符(可用)

Public Function htmlDecodes(str As String) As String
  If str = "" Then
    htmlDecodes = ""
  Else
    str = Replace(str, "&lt;", "<")
    str = Replace(str, "&gt;", ">")
    str = Replace(str, "&amp;", "&")
    str = Replace(str, "&quot;", Chr(34))
    str = Replace(str, "&gt;", Chr(39))

    htmlDecodes = str
  End If
End Function


3. '返回指定元素值在数组中的 数字下标

Public Function getArrayEleId(arr, val) As Integer
  Dim i As Integer

  For i = 0 To UBound(arr)
    If val = arr(i) Then
      getArrayEleId = i
      Exit For
    End If
  Next i
End Function


4. '打开“自动计算”

Public Sub openAutoCompute()
  Application.ScreenUpdating = True
  Application.DisplayStatusBar = True
  Application.Calculation = xlAutomatic
  Application.EnableEvents = True
  ActiveSheet.DisplayPageBreaks = True
End Sub


5. '关闭“自动计算”

Public Sub closeAutoCompute()
  Application.ScreenUpdating = False
  Application.DisplayStatusBar = False
  Application.Calculation = xlCalculationManual
  Application.EnableEvents = False
  ActiveSheet.DisplayPageBreaks = False
End Sub


6. '切换打印机

Public Sub changePrinter()
  Application.Dialogs(xlDialogPrinterSetup).Show

  ThisWorkbook.Sheets("setting").Range("C8") = Application.ActivePrinter
End Sub


7. '数值型 一维数组 排序(冒泡0→1)

Public Function sortUp_numberArray(arr) As Variant
  Dim i, j As Integer
  Dim t

  For i = 0 To UBound(arr)
    For j = i + 1 To UBound(arr)
      If CDbl(arr(i)) > CDbl(arr(j)) Then
        t = arr(i)
        arr(i) = arr(j)
        arr(j) = t
      End If
    Next j
  Next i

  sortUp_numberArray = arr
End Function


8. '数值型 二维数组 排序(冒泡0→1)**未验证**

Public Function sortUp_array2d(arr, keyIdxArray) As Variant
  Dim h, i, j As Integer
  Dim t

  For h = 0 To UBound(keyIdxArray)
    For i = 0 To UBound(arr)
      For j = i + 1 To UBound(arr)
        If CDbl(arr(i, keyIdxArray(h))) > CDbl(arr(j, keyIdxArray(h))) Then
          t = arr(i)
          arr(i) = arr(j)
          arr(j) = t
        End If
      Next j
    Next i
  Next h

  sortUp_array2d = arr
End Function


9. '删除 一维数组中的 重复值

Function del_arraySameValue(arr As Variant) As Variant
  Dim i, j As Long
  Dim arr2()
  Dim is_same As Boolean

  ReDim Preserve arr2(0)
  arr2(0) = arr(0)

  For i = 1 To UBound(arr)
    is_same = False
    For j = 0 To UBound(arr2)
      If arr2(j) = arr(i) Then
        is_same = True
        Exit For
      End If
    Next j

    If is_same = False Then
      ReDim Preserve arr2(UBound(arr2) + 1)
      arr2(UBound(arr2)) = arr(i)
    End If
  Next i

  del_arraySameValue = arr2
End Function


10. '检测 一维数组中 是否包含 某值(仅 Double 类型)(不稳定……原因不明)

Function is_inArray(arr As Variant, ele As Double) As Boolean
  Dim i As Long
  Dim eles As String

  On Error Resume Next
  eles = Join(arr, ",")

  i = Application.WorksheetFunction.Match(ele, arr, 0)
  If Err = 0 Then
    is_inArray = True
    Exit Function
  End If

  is_inArray = False
End Function


11. '检测 一维数组中 是否包含 某值

Function is_inArray3(arr, ele) As Boolean
  Dim arr1
  Dim arr_str As String

  is_inArray = False

  arr1 = VBA.Filter(arr, ele, True) '筛选所有含 ele 的数值组成一个新数组
  arr_str = Join(arr1, ",")
  If Len(arr_str) > 0 Then
    is_inArray = True
  End If

  ' If Not is_emptyArray(arr1) Then
  ' is_inArray = True
  ' End If
End Function


12. '检测 二维数组中 是否包含 某值

Function is_in2dArray(arr() As Variant, ele) As Boolean
  If WorksheetFunction.CountIf(Application.Index(arr, 1, 0), ele) > 0 Then
    is_inArray = True
  Else
    is_inArray = False
  End If
End Function


13. '判断是否为 “空数组”

'需 api 引用:Public Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Function is_emptyArray(ByRef X() As String) As Boolean
  Dim tempStr As String

  tempStr = Join(X, ",")
  is_emptyArray = LenB(tempStr) <= 0
End Function


14. 日期处理 函数

'将时间戳(10或13位整数)转换成 yyyy-mm-dd hh:mm:ss 格式的日期
Public Function timeStamp2date(timeStamp As Double, Optional beginDate = "01/01/1970 08:00:00")
  If Len(CStr(timeStamp)) = 13 Then timeStamp = timeStamp / 1000
  timeStamp2date = DateAdd("s", timeStamp, beginDate)
End Function

'将 yyyy-mm-dd hh:mm:ss 转换成 时间戳(10位整数)
Public Function date2timeStamp(theDate As Date, Optional timeDiff = 28800)
  date2timeStamp = DateDiff("s", "01/01/1970 00:00:00", theDate) - timeDiff
End Function

'获取 yyyy-mm-dd hh:mm:ss 中的 yyyy-mm-dd
Public Function getDate(theDate As Date)
  getDate = year(theDate) & "-" & month(theDate) & "-" & day(theDate)
End Function

VBA Excel 常用 自定义函数的更多相关文章

  1. [VBA]用一个简单例子说明如何在Excel中自定义函数

    Excel中的函数无疑是强大的,但是再强大的战士也有他脆弱的脚后跟[1].这两天在使用Excel的时候遇到了一个需求,要在某一个单元格里面自动计算今天是星期几(如显示 Today is Tuesday ...

  2. JS常用自定义函数总结

    JS常用自定义函数总结   1.原生JavaScript实现字符串长度截取 2.原生JavaScript获取域名主机 3.原生JavaScript清除空格 4.原生JavaScript替换全部 5.原 ...

  3. 浅谈Excel开发:六 Excel 异步自定义函数

    上文介绍了Excel中的自定义函数(UDF ),它极大地扩展了Excel插件的功能,使得我们可以将业务逻辑以Excel函数的形式表示,并可以根据这些细粒度的自定义函数,构建各种复杂的分析报表. 普通的 ...

  4. mysql 常用自定义函数解析

    -- /* -- * 用于获取一记录数据,根据传入的分隔字符delim,索引位置pos,返回相对应的value -- * SELECT Json_getKeyValue({"A": ...

  5. php常用自定义函数

    1,写一个函数,算出两个文件的相对路径 有两种方法,一种是利用array的相关方法,如例1,另外一种是使用?:;运算符 先看第一种方法 function getrelativepath2($path1 ...

  6. oracle常用自定义函数集合

    1.Oracle 判断值是否为数字的函数CREATE OR REPLACE FUNCTION ISNUMBER(MyStr VARCHAR2) RETURN NUMBERIS  STR VARCHAR ...

  7. Sql Server 常用自定义函数

    -- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...

  8. python 几个常用自定义函数在dataframe上的应用

    最小值与最大值 def f(x): return pd.Series([x.min(),x.max(),index=['min','max']) frame.apply(f) 浮点值的格式化 form ...

  9. SQL常用自定义函数

    1.字符串转Table(Func_SplitToTable) CREATE FUNCTION [dbo].[Func_SplitToTable]      (        @SplitString ...

随机推荐

  1. Cocos2d-x使用iOS游戏内付费IAP(C++篇)

    本文章转载 http://www.ityran.com/archives/5515.非本人原创! 前期准备 设备与账号 在开始编码之前我们需要准备测试环境. IAP只能真机测试,准备一台iOS设备是必 ...

  2. Polymer——Web Components的未来

    什么是polymer? polymer由谷歌的Palm webOS团队打造,并在2013 Google I/O大会上推出,旨在实现Web Components,用最少的代码,解除框架间的限制的UI 框 ...

  3. Java-note-调试小技巧

    使用 Eclipse 调试 Java 程序的 10 个技巧 英文原文:Again! – 10 Tips on Java Debugging with Eclipse 你应该看过一些如<关于调试的 ...

  4. storm,hbase和storm-kafka-0.8-plus兼容性问题

    1  org.slf4j.impl.StaticLoggerBinder.SINGLETON错误 方案: 确保slf4j-api-1.5.6.jar 和slf4j-log4j12-1.5.6.jar ...

  5. C# 颜色转换

    十六进制颜色与Color对象的互相转换[C#]   C#十六进制颜色与Color对象的互相转换 把十六进制颜色转化为color对象ColorTranslator.FromHtml("#FF0 ...

  6. 对unsigned int和int进行移位操作的区别

    1. 无符号整数 unsigned int 对unsigned int进行移位操作时,最高位不会有任何特殊性. 无符号整数必须使用%u来打印 #include <stdio.h> int ...

  7. Linux下搭建jdk

    1.上oracle下载jdk-8u73-linux-x64.gz文件(其他版本的也可以,不过本教程以.gz格式为准,其他格式没有尝试过,不造怎么弄) 2.在linux桌面上右键->打开终端 3. ...

  8. homework09-虐心的现程设终于要告一段落了

    V3.0版本今天凌晨出炉 添加了随机生成 添加了文件打开 完全按照老师的要求搞定了 V2.0版本更新 添加了中间数组变量显示 这次作业写了整整一天,把以前能用的代码都改了一个遍 最后变成了网页版的小程 ...

  9. Xcode 的正确打开方式——Debugging

    程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式. “If debuggi ...

  10. iOS学习之触摸事件

    触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...