乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 53|回复: 0

[编程交流] Excel VBA:dea的7个函数

[复制链接]

7

主题

30

帖子

36

银币

初来乍到

Rank: 1

铜币
22
发表于 2022-7-6 19:40:05 | 显示全部楼层 |阅读模式
大家好,
这是我在这里学习和发布的一系列功能,希望它们像我在日常工作中一样对你有用。
 
Phh公司
 
 
204015yom1smexmus4sum5.png
 
 
  1. ''#################################################################
  2. ''## Functions to deal with feet-inches format                   ##
  3. ''## in a form of [#'-#"] or [#'-# #/##"] or [#'-#.##"]          ##
  4. ''## By Phh, 2010, last update 2021                              ##
  5. ''#################################################################
  6. ''## todec()     Convert to decimal                              ##
  7. ''## toimpe()    Convert to imperial, engineering format         ##
  8. ''##             with optional precision argument, default 1/16" ##
  9. ''## toimpa()    Convert to imperial, architectural format       ##
  10. ''##             with optional precision argument, default 1/16" ##
  11. ''## sumtodec()  Similar to SUM function, decimal format         ##
  12. ''## sumtoimpe() Similar to SUM function, engineering format     ##
  13. ''## sumtoimpa() Similar to SUM function, architectural format   ##
  14. ''## frac2num()  Sub function, convert fraction to decimal       ##
  15. ''#################################################################
  16. Option Explicit
  17. Public Function todec(strX As String, Optional argDivBy As Double) As Double
  18.   Dim startPos As Integer, ftPos As Integer, frPos As Integer
  19.   Dim rdLen, argDivNum As Double
  20.   If argDivBy > 0 Then
  21.     argDivNum = argDivBy
  22.   Else
  23.     argDivNum = 1
  24.   End If
  25.   strX = Replace(Replace(strX, """", ""), "-", "")
  26.   strX = WorksheetFunction.Trim(strX)
  27.   startPos = 1
  28.   ftPos = InStr(startPos, strX, "'")
  29.   frPos = InStr(startPos, strX, "/")
  30.   If ftPos = 0 And frPos = 0 Then
  31.     todec = (Val(strX) / argDivNum)
  32.     Exit Function
  33.   End If
  34.   If ftPos = 0 And frPos > 0 Then
  35.     todec = (frac2num(strX) / argDivNum)
  36.     Exit Function
  37.   End If
  38.   rdLen = CDbl(Left(strX, ftPos - 1)) * 12
  39.   If frPos = 0 Then
  40.     rdLen = rdLen + (Abs(Val(Mid(strX, ftPos + 1, Len(strX)))))
  41.     todec = (rdLen / argDivNum)
  42.     Exit Function
  43.   End If
  44.   rdLen = rdLen + frac2num(Mid(strX, ftPos + 1, Len(strX)))
  45.   todec = (rdLen / argDivNum)
  46. End Function
  47. Public Function sumtodec(ParamArray Xrange() As Variant) As Double
  48.   Dim sumArray As Double
  49.   Dim theVal As Variant
  50.   Dim I As Integer
  51.   For I = LBound(Xrange) To UBound(Xrange)
  52.    If TypeOf Xrange(I) Is Range Then
  53.    For Each theVal In Xrange(I)
  54.     sumArray = sumArray + todec(CStr(theVal))
  55.    Next theVal
  56.    Else
  57.     sumArray = sumArray + CDbl(Xrange(I))
  58.    End If
  59.   Next
  60.   sumtodec = sumArray
  61. End Function
  62. Public Function toimpe(aLen As Double, Optional argRd As Variant = 16) As String
  63. Dim rdLen As Double, argRdNum As Double
  64. If argRd >= 1 Then
  65.    argRdNum = 1 / Fix(argRd)
  66. ElseIf argRd < 1 And argRd > 0 Then
  67.    argRdNum = argRd
  68. ElseIf argRd = 0 Then
  69.    toimpe = (Fix(aLen / 12)) & "'-" & (aLen - (12 * Fix(aLen / 12))) & """"
  70.    Exit Function
  71. End If
  72. rdLen = excel.WorksheetFunction.Round(aLen / argRdNum, 0) * argRdNum
  73. toimpe = (Fix(rdLen / 12)) & "'-" & (rdLen - (12 * Fix(rdLen / 12))) & """"
  74. End Function
  75. Public Function toimpa(aLen As Double, Optional argRd As Variant = 16) As String
  76. Dim rdLen As Double, argRdNum As Double
  77. If argRd >= 1 Then
  78.    argRdNum = 1 / Fix(argRd)
  79. ElseIf argRd < 1 And argRd > 0 Then
  80.    argRdNum = argRd
  81. ElseIf argRd = 0 Then
  82.    toimpa = (Fix(aLen / 12)) & "'-" & excel.WorksheetFunction.Text((aLen - (12 * Fix(aLen / 12))), "0 ##/####") & """"
  83.    Exit Function
  84. End If
  85. rdLen = excel.WorksheetFunction.Round(aLen / argRdNum, 0) * argRdNum
  86. toimpa = (Fix(rdLen / 12)) & "'-" & excel.WorksheetFunction.Text((rdLen - (12 * Fix(rdLen / 12))), "0 ##/####") & """"
  87. End Function
  88. Public Function sumtoimpe(ParamArray Xrange() As Variant) As String
  89.   Dim sumArray As Double, argRdNum As Double
  90.   Dim theVal As Variant
  91.   Dim I As Integer
  92.   For I = LBound(Xrange) To UBound(Xrange)
  93.    If TypeOf Xrange(I) Is Range Then
  94.    For Each theVal In Xrange(I)
  95.     sumArray = sumArray + todec(CStr(theVal))
  96.    Next theVal
  97.    Else
  98.     sumArray = sumArray + CDbl(Xrange(I))
  99.    End If
  100.   Next
  101.   'Set precision round of to 1/256" as default, change if required!
  102.   argRdNum = (1 / 256)
  103.   sumArray = excel.WorksheetFunction.Round(sumArray / argRdNum, 0) * argRdNum
  104.   sumtoimpe = (Fix(sumArray / 12)) & "'-" & (sumArray - (12 * Fix(sumArray / 12))) & """"
  105. End Function
  106. Public Function sumtoimpa(ParamArray Xrange() As Variant) As String
  107.   Dim sumArray As Double, argRdNum As Double
  108.   Dim theVal As Variant
  109.   Dim I As Integer
  110.   For I = LBound(Xrange) To UBound(Xrange)
  111.    If TypeOf Xrange(I) Is Range Then
  112.    For Each theVal In Xrange(I)
  113.     sumArray = sumArray + todec(CStr(theVal))
  114.    Next theVal
  115.    Else
  116.     sumArray = sumArray + CDbl(Xrange(I))
  117.    End If
  118.   Next
  119.   'Set precision round of to 1/256" as default, change if required!
  120.   argRdNum = (1 / 256)
  121.   sumArray = excel.WorksheetFunction.Round(sumArray / argRdNum, 0) * argRdNum
  122.   sumtoimpa = (Fix(sumArray / 12)) & "'-" & excel.WorksheetFunction.Text((sumArray - (12 * Fix(sumArray / 12))), "0 ##/####") & """"
  123. End Function
  124. Function frac2num(ByVal X As String) As Double
  125.   Dim P As Integer
  126.   Dim N As Double, Num As Double, Den As Double
  127.   X = Trim$(X)
  128.   P = InStr(X, "/")
  129.   If P = 0 Then
  130.     N = Val(X)
  131.   Else
  132.     Den = Val(Mid$(X, P + 1))
  133.     If Den = 0 Then Error 11
  134.       X = Trim$(Left$(X, P - 1))
  135.       P = InStr(X, " ")
  136.     If P = 0 Then
  137.       Num = Val(X)
  138.     Else
  139.       Num = Val(Mid$(X, P + 1))
  140.       N = Val(Left$(X, P - 1))
  141.     End If
  142.   End If
  143.   If Den <> 0 Then
  144.     N = N + Num / Den
  145.   End If
  146.   frac2num = N
  147. End Function
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 14:42 , Processed in 0.447011 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表