Using a UDF in Excel to update the worksheet

后端 未结 3 1467
心在旅途
心在旅途 2020-11-22 02:01

Not really a question, but posting this for comments because I don\'t recall seeing this approach before. I was responding to a comment on a previous answer, and tried some

3条回答
  •  囚心锁ツ
    2020-11-22 02:44

    Posting a response so I can mark my own "question" as having an answer.

    I've seen other workarounds, but this seems simpler and I'm surprised it works at all.

    Sub ChangeIt(c1 As Range, c2 As Range)
        c1.Value = c2.Value
        c1.Interior.Color = IIf(c1.Value > 10, vbRed, vbYellow)
    End Sub
    
    
    '########  run as a UDF, this actually changes the sheet ##############
    ' changing value in c2 updates c1...
    Function SetIt(src, dest)
    
        dest.Parent.Evaluate "Changeit(" & dest.Address(False, False) & "," _
                            & src.Address(False, False) & ")"
    
        SetIt = "Changed sheet!" 'or whatever return value is useful...
    
    End Function
    

    Please post additional answers if you have interesting applications for this which you'd like to share.

    Note: Untested in any kind of real "production" application.

提交回复
热议问题