AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1812761
Accepted
vorpal
vorpal
Asked: 2023-10-16 09:47:27 +0800 CST2023-10-16 09:47:27 +0800 CST 2023-10-16 09:47:27 +0800 CST

创建后切勿更新 Excel UDF

  • 772

在 Excel 中,我尝试使用 vba UDF 在表中生成 UUID 列,但我只希望它在第一次调用时运行,以便它生成的 UUID 永远不会改变。

制作 UUID 很容易(从https://nolongerset.com/createguid/修改-编辑:添加了对单元格 A1 的检查,以便与下面@MGonet 的答案正常工作):

'These Declare lines go at the very top of the code module
#If VBA7 Then
    Private Declare PtrSafe Function CoCreateGuid Lib "ole32" (id As Any) As Long
#Else
    Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long
#End If


' ----------------------------------------------------------------
' Procedure  : CreateGUID
' Author     : Dan ([email protected])
' Source     : http://allapi.mentalis.org/apilist/CDB74B0DFA5C75B7C6AFE60D3295A96F.html
' Adapted by : Mike Wolfe
' Republished: https://nolongerset.com/createguid/
' Date       : 8/5/2022
' ----------------------------------------------------------------
Private Function CreateGUID() As String
    'Application.OnRepeat Text:="Paste Next Date", _
                          'Procedure:="Application.thisCell.Text"
     If (Application.thisCell.Address <> "$A$1") Then
            Const S_OK As Long = 0
            Dim id(0 To 15) As Byte
            Dim Cnt As Long, GUID As String
            If CoCreateGuid(id(0)) = S_OK Then
                For Cnt = 0 To 15
                    CreateGUID = CreateGUID & IIf(id(Cnt) < 16, "0", "") + Hex$(id(Cnt))
                Next Cnt
                CreateGUID = LCase(Left$(CreateGUID, 8) & "-" & _
                                   Mid$(CreateGUID, 9, 4) & "-" & _
                                   Mid$(CreateGUID, 13, 4) & "-" & _
                                   Mid$(CreateGUID, 17, 4) & "-" & _
                                   Right$(CreateGUID, 12))
            Else
                MsgBox "Error while creating GUID!"
            End If
        Else
            CreateGUID = Application.thisCell.Text
        End If
End Function

我想要的是将其包装在一个If语句中以确定该函数是否已经被计算过。经过一番折腾后我尝试过:

Public Function CreateGUID() As String
     If (Application.thisCell.Characters = "") Then
            'Generate UUID
     Else
            CreateGUID = Application.thisCell.Value
     End If
End Function

然而这似乎不起作用。我还尝试使用application.onrepeat进行修改,但这似乎也不起作用。是否有其他方法来确定公式是否已被评估?

microsoft-excel
  • 1 1 个回答
  • 74 Views

1 个回答

  • Voted
  1. Best Answer
    MGonet
    2023-10-16T22:44:14+08:002023-10-16T22:44:14+08:00

    我不知道你想在实践中如何使用这个公式。

    如果您想将公式保留在单元格中,这使您可以复制带有公式的单元格,但是它们将包含相同的值,因此您需要激活它们才能获取新值。您可以使用 UDF 来实现此目的:

    Function OneCall()
       If Application.ThisCell.Text = 0 Then
          OneCall = CreateGUID
       Else
           OneCall = Application.ThisCell.Text
       End If
    End Function  
    

    如果重新计算工作表,单元格内容保持不变,但是如果编辑单元格,则会生成新值。

    如果您更喜欢一次性功能,可以尝试其他方法。在这种情况下,函数结果被替换为常量,因此它不会因重新计算而自动更改。但是每次你想生成一个新值时,你必须对单元格输入新的函数调用=EOneCall()

    这是两个函数的集合,因为我们的函数应该被间接调用。

    Function OneCall2() As String
       With Application.ThisCell
          If .Address <> "$A$1" Then
             .Value = CreateGUID
          End If
       End With
    End Function
    
    Function EOneCall() As String
       EOneCall = Evaluate("OneCall2()")
    End Function
    
    • 2

相关问题

  • 带有“和”运算符的 Excel 数据透视表

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve