如何在Excel VBA中将string格式化为UUID?
我有一个小VBA项目,但我有一个问题。
我需要一个32个字符的hexstring,并把它变成一个UUID。
Input: b10a8db164e0754105b7a99be72e3fe5 Output: b10a8db1-64e0-7541-05b7-a99be72e3fe5
我不知道从哪里开始,但是我想我需要在这么多的字符之后拆分string,然后重新组合它。 任何指针或方法来有效地做到这一点?
您可以使用Left
, Right
和Mid
,使其成为你想要的格式。
Function makeUuid(asString As String) As String Dim asUuidFormat As String If Len(asString) <> 32 Then makeUuid = asString Exit Function End If Dim firstPart As String, secondPart As String, thirdPart As String, fourthPart As String, fifthPart As String firstPart = Left(asString, 8) secondPart = Mid(asString, 9, 4) thirdPart = Mid(asString, 13, 4) fourthPart = Mid(asString, 17, 4) fifthPart = Right(asString, 12) makeUuid = firstPart & "-" & secondPart & "-" & thirdPart & "-" & fourthPart & "-" & fifthPart End Function
你可以在工作表中使用`= makeUuid(A4)'(或任何单元格)
如果你传递一个非32长度的string,它不会格式化,只是返回原来的string。
用另一个单元格中的A1数据:
=MID(A1,1,8) & "-" & MID(A1,9,4) & "-" & MID(A1,13,4) & "-" & MID(A1,17,4) & "-" & MID(A1,21,9999)