Consulting

Results 1 to 3 of 3

Thread: function that will set a specified cell to a specified value.

  1. #1
    VBAX Newbie
    Joined
    Jun 2024
    Posts
    1
    Location

    function that will set a specified cell to a specified value.

    Please help me.

    I am trying to write a function that will set a specified cell to a specified value. (Code is below).

    Let's say my current location is A1. In A1, I enter =SetCellValue(M2, "Bob") expecting M2 to be set to "Bob".
    Instead A1 is set to #VALUE!.
    Same with =SetCellValue("M2", "Bob"). Debugging shows that the problem occurs at the TargetCell.Value = NewValue
    Debugging also shows that TargetCell and NewValue are being passed to my function. Everything is in the same Sheet.
    I have made this as simple as I could. but I can't figure out what I am doing wrong.

    Function SetCellValue(TargetCell As Range, NewValue As Variant) 
        ' Set the value of the target cell 
        TargetCell.Value = NewValue 
    End Function
    Last edited by georgiboy; 06-06-2024 at 01:42 AM. Reason: Added code tags

  2. #2
    This is working as expected given the constraints of a UDF Description of limitations of custom functions in Excel - Microsoft Support

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,747
    Location
    User Defined Functions (UDF) cannot change a worksheet, they can only return a value to the cell(s), in this case A1

    You would need to use a Sub

    The best alternative might be to use a Worksheet_Change event and that's not great

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim iRow As Long
        Dim r1 As Range
        
        If Target.Column > 2 Then Exit Sub
        
        Set r1 = Target.Cells(1, 1).CurrentRegion
        
        With r1
        
            If Target.Row > .Rows.Count Then Exit Sub
    
    
            Application.EnableEvents = False
    
    
            For iRow = 2 To .Rows.Count
                If Len(.Cells(iRow, 2).Value) = 0 Then
                    Worksheets("Sheet2").Range(.Cells(iRow, 1).Value).ClearContents
                Else
                    Worksheets("Sheet2").Range(.Cells(iRow, 1).Value).Value = .Cells(iRow, 2).Value
                End If
            Next iRow
        
            Application.EnableEvents = True
    
    
        End With
    
    
    End Sub

    Sheet1 has addresses and values to go onto Sheet2
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •