Excel VBA中,500 486 485 470个字节
匿名VBE立即窗口功能
匿名VBE即时窗口函数,将输入作为年份,从[A1]
年月开始[B1]
,从天开始[C1]
,从天开始,从小时开始[D1]
,从分钟开始[E1]
,从分钟开始,从秒开始[F1]
,从中[G1:Z1]
选出一个额外的精度数组,计算RFC2550时间戳并将其输出到VBE立即窗口。在下面利用已声明的辅助函数。
n=Left([A1],1)="-":y=Mid([A1],1-n):l=Len(y):o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y):For Each c In[B1:Z1]:j=j+1:p=p+IIf(c,Format(c,IIf(j>5,"000","00")),""):Next:If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:?IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))Replace(o,"=","!")p:Else?o;p
辅助功能
声明的辅助函数,该函数接受输入数字并以26为底的基数返回该数字,使得1->A
和26->Z
必须放置在公共模块中。
Function b(n)
While n
n=n-1
d=n Mod 26+1
n=Int(n/26)
d=d+26*(d<0):n=n-(d<0)
b=Chr(64+d)+b
Wend
End Function
用法
必须在clear模块中使用,或者必须在执行之前以vars的形式清除该模块j
,o
并p
假定它们在代码执行开始时处于默认的未初始化状态。对于j
,这是一个Variant\Integer
变量,该默认值是0
,对于o
和p
,这是Variant\String
变量,该默认值是空字符串(""
)。
输入(字符串数组)从1:1
ActiveSheet上获取,输出到VBE立即窗口。
样品I / O
[A1:F1]=Split("4052107100422150625478207675901330514555829957419806023121389455865117429470888094459661251.2.3.5.7.11",".")
n=Left([A1],1)="-":y=Mid([A1],1-n):l=Len(y):o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y):For Each c In[B1:ZZ1]:j=j+1:p=p+IIf(c,Format(c,IIf(j>5,"000","00")),""):Next:If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:?IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))Replace(o,"=","!")p:Else?o;p
^^BI40521071004221506254782076759013305145558299574198060231213894558651174294708880944596612510203050711021028
Cells.Clear:j=0:o="":p="" '' clear the worksheet and vars
[A1:H1]=Array("-696443266","1","3","6","10","15","21","28")
n=Left([A1],1)="-":y=Mid([A1],1-n):l=Len(y):o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y):For Each c In[B1:ZZ1]:j=j+1:p=p+IIf(c,Format(c,IIf(j>5,"000","00")),""):Next:If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:?IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))Replace(o,"=","!")p:Else?o;p
*V3035567330103061015021028
Cells.Clear:j=0:o="":p="" '' clear the worksheet and vars
[A1]="45941"
n=Left([A1],1)="-":y=Mid([A1],1-n):l=Len(y):o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y):For Each c In[B1:ZZ1]:j=j+1:p=p+IIf(c,Format(c,IIf(j>5,"000","00")),""):Next:If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:?IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))Replace(o,"=","!")p:Else?o;p
A45941
Cells.Clear:j=0:o="":p="" '' clear the worksheet and vars
[A1:F1]=Split("4052107100422150625478207675901330514555829957419806023121389455865117429470888094459661251.2.3.5.7.11",".")
n=Left([A1],1)="-":y=Mid([A1],1-n):l=Len(y):o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y):For Each c In[B1:ZZ1]:j=j+1:p=p+IIf(c,Format(c,IIf(j>5,"000","00")),""):Next:If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:?IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))Replace(o,"=","!")p:Else?o;p
^^BI40521071004221506254782076759013305145558299574198060231213894558651174294708880944596612510203050711
Sub
常规版
声明的子例程将输入作为年份[A1]
,从年开始,从月开始[B1]
,从天开始[C1]
,从小时开始[D1]
,从分钟开始[E1]
,从分钟开始,从秒开始,从中输入[F1]
一个可选的额外精度数组[G1:Z1]
,以计算RFC2550时间戳并将其输出到VBE立即窗口。
Sub R(x)
a=x(0)
n=Left(a,1)="-"'<- that `"` is only there to make sure highlighting is correct
y=Mid(a,1-n)
l=Len(y)
o=IIf(l<5,Right("000"&y,4),IIf(l<31,"",String(Len(b(l-30)),94))&B(l-IIf(l<31,4,30))&y)
If n Then For i=1To Len(o):c=Asc(Mid(o,i,1)):Mid$(o,i,1)=Chr(IIf(c<60,105,155)-c):Next:o=IIf(l<5,"/",IIf(InStr(1,o,"="),"","*"))&Replace(o,"=","!")
For j=1To UBound(x)
o=o+IIf(x(j),Format(x(j),IIf(j>5,"000","00")),"")
Next
[A2]=o
End Sub
Function b(n)
While n
n=n-1
d=n Mod 26+1
n=Int(n/26)
d=d+26*(d<0):n=n-(d<0)
b=Chr(64+d)+b
Wend
End Function
用法
[A1:ZZ1]
可以根据需要手动输入范围,也可以根据需要从最左边到最右边键入单元格,也可以通过从VBE立即窗口分配来进行输入。
值得注意的是,由于Excel将数字自动转换为科学计数法,因此任何以10为基数的长度等于或大于12位数字的数字都必须通过将单元格设置为文本单元格或将单元格显式地插入文本单元格中。通过将文字'
放在单元格值的开头
样品I / O
r Split("4052107100422150625478207675901330514555829957419806023121389455865117429470888094459661251.2.3.5.7.11",".")
?[A2] '' <- print output to VBE console
^^BI40521071004221506254782076759013305145558299574198060231213894558651174294708880944596612510203050711 '' <- Output
r Array("47883552573911529811831375872990","1","1","2","3","5","8","13")
?[A2]
^B478835525739115298118313758729900101020305008013
r Array("-696443266","1","3","6","10","15","21","28")
?[A2]
*V3035567330103061015021028
r Array("45941")
?[A2]
A45941
脱胎换骨和解释
'' Returns RFC2550 timestamp corresponding to passed vars
Public Function RFC2550(ByVal pYear As String, ParamArray Extra() As Variant) As String
'' Declare Vars
Dim Negative As Boolean, _
leny As Long, _
i As Long, _
c As Byte, _
s As Variant, _
out As String
'' Check if year is negative and store the absolute value of the year
Let Negative = Left(pYear, 1) = "-"
Let pYear = Mid(pYear, 1 - Negative)
'' Take the length of the year
Let leny = Len(pYear)
If leny < 5 Then
'' If the length is less than 5, pad the year left to 4 characters
'' using zeros
Let out = Format("0000", pYear)
Else
'' If the length of the year is greater than 30, then set out to be
'' as string comprised of length-30 instances of `^`
Let out = IIf(leny < 31, "", String(Len(Base26(leny - 30)), 94))
'' append the Base26 representation of the length of the year,
'' minus 30, if the length is greater than 30
Let out = out & Base26(leny - IIf(leny < 31, 4, 30))
'' append the year to out
Let out = out & pYear
End If
If Negative Then
'' iterate across out
For i = 1 To Len(out)
'' store the char code for the current char
Let c = Asc(Mid(out, i, 1))
'' swap letter/number with its inverse (0->9,A->Z)
Mid$(out, i, 1) = Chr(IIf(c < 60, 105, 155) - c)
Next i
'' replace `=` (the inverse of `^`) with `!`
Let out = Replace(out, "=", "!")
'' Prepend either `/`, `*`, or nothing depending on length and
'' start of out
Let out = IIf(leny < 5, "/", IIf(InStr(1, out, "!"), "", "*")) & out
End If
Let i = 1
For Each s In Extra
'' append all of the extra precision data - padding to 2 chars for
'' the first 5 elements in the array (month, day, hour, minute and
'' second) and to 3 chars for all following elements (milli, micro,
'' nano, pico, femto, atto, zepto, yocto - seconds) with the char 0
Let out = out & IIf(s, Format(s, IIf(i > 5, "000", "00")), "")
Let i = i + 1
Next
'' return out
Let RFC2550 = out
End Function
'' returns non-standard base26 version of input number
'' 1->A, 2->B,... 26->Z
Function Base26(ByVal n As Long) As String
'' declare vars
Dim out As String, _
digit As Integer
'' init out, digit
Let out = ""
Let digit = 0
'' iterate through n
While n
'' Decrement, hold the value of the digit
Let n = n - 1
Let digit = n Mod 26 + 1
'' divide by 26
Let n = Int(n / 26)
'' correct for negative numbers
If digit < 0 Then Let n = n + 1: Let digit = digit - 26
'' prepend char corresponding to the digit to out
Let out = Chr(64 + digit) & out
Wend
'' return out
Let Base26 = out
End Function
-696443266.1.3.6.10.15.21.28
应该*V3035567339896938984978971
吗?