从公式写入函数

我试图将下面的公式转换为Excel中的函数:

=REPLACE(REPLACE(R1C1,10,3,(ROUND((RIGHT(R1C1,3)*29.97)/1000,0))),9,1,"";"") 

这样做的时间码是这样的: 00:01:35,748把它变成这样: 00:01:35;22

我不知道如何把现有的函数放到一个自定义的函数中,结果如何。

打开VBA编辑器(Alt-F11),从插入菜单中select“插入模块”,然后键入以下内容(更新为更好的代码实践 – 谢谢@ ja72input):

 Option Explicit Public Const fps = 29.97 ' frames per second Function timeCode(s) ' take a string as input ' find the fractional time ' and convert to frame number like 00:01:35,748 ' and turn it into this: 00:01:35;22 Dim comma, fraction, frameNum, frameInSec comma = InStr(1, s, ",") fraction = Mid(s, comma + 1, Len(s)) ' convert to seconds regardless of number of digits: frameInSec = Val(fraction) / (10 ^ Len(fraction)) frameNum = Application.WorksheetFunction.Round(frameInSec * fps, 0) timeCode = Left(s, comma - 1) & ";" & frameNum End Function 

现在你将能够input一个像

 =timeCode("00:01:35,748") 

到您的电子表格,结果将是

 00:01:35;22 

这是你想要的…

您可以使用单元格引用而不是string,因此如果在单元格A1中有"00:01:35,748" ,则可以键入

 =timeCode(A1) 

在B1单元格(例如),并得到你想要的结果。 自定义function – 强大的东西。

编辑特别要求(两个为一个的价格…):改变格式输出string取代:; ,并使帧号码总是两位数(如果需要,前导零):

 Dim tempString tempString = Left(s, comma - 1) & ";" & Format(frameNum, "00") timeCode = Replace(tempString, ":", ";") 

这应该是显而易见的,你把这个在上面的代码…