Consulting

Results 1 to 9 of 9

Thread: Solved: Event Macro to add text to cell

  1. #1
    VBAX Newbie
    Joined
    Sep 2012
    Posts
    3
    Location

    Solved: Event Macro to add text to cell

    Hi,

    I'm trying to make an event macro to be triggered when a cell in a range is changed or deleted.

    For example, if the target range is set to B1:B10 and cell b5 is changed, then c5 = "text". If cell b9 is empty or deleted, then c9 = "".

    Any ideas/help?

    Let me know if you need more detail.
    Nick

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1:B10")) Then
    If Target.Text <> "" Then
    Target.Offset(0, 1).Value = "Text"
    Else
    Target.Offset(0, 1).Value = ""
    End If
    End If
    End Sub[/VBA]
    ------------------------------------------------
    Happy Coding my friends

  3. #3
    VBAX Newbie
    Joined
    Sep 2012
    Posts
    3
    Location
    Thanks for quick reply CatDaddy,

    I get the following error when I run your code: Object variable or With block variable not set. I added the following to your code to prevent it from coming up with that error but the "text" in column c is not replaced with "" when text in column b is deleted.

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Intersect(Target, Range("B1:B10")) Then
    If Target.Text <> "" Then
    Target.Offset(0, 1).Value = "Text"
    Else
    Target.Offset(0, 1).Value = ""
    End If
    End If
    Application.EnableEvents = True
    End Sub
    [/VBA]

    Did it work for you?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    first run this macro:

    [VBA]Sub tst()
    Application.EnableEvents = True
    End Sub
    [/VBA]

    Then use:
    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B1:B10")) Is Nothing Then Target.Offset(, 1) = IIf(Target.Value = "", "", "text")
    End Sub
    [/VBA]

  5. #5
    VBAX Newbie
    Joined
    Sep 2012
    Posts
    3
    Location
    Works perfect snb thanks a bunch!

    Nick

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Instead of

    [VBA]
    Target.Offset(0, 1).Value = ""
    [/VBA]

    I'd suggest

    [VBA]
    Target.Offset(0, 1).ClearContents
    [/VBA]

    since "" is still 0 length string, and not an empty cell

    Paul

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    @Paul

    Can you elaborate ?

    I ran this macro.

    [VBA]Sub snb()
    Cells(2, 6).Value = ""
    Cells(4, 6).Value = vbNullString
    Cells(6, 6).ClearContents
    Columns(6).SpecialCells(4).Select
    End Sub
    [/VBA]

    VBA doesn't discriminate between all empty cells ( no matter how they have been (not) created).

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    snb --

    I do believe you are correct sir

    I was most likely thinking of the old 1-2-3 trick of using <space> to clear a cell.

    My habit is to use .ClearContents, and I guess I forgot why

    http://www.aivosto.com/vbtips/stringopt.html#whyslow

    Color me embarrassed

    Paul

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Hi Paul,

    I was only intrigued by your remarks, so I became curious what you knew what I didn't.
    Thank you for your reply.
    It cools me down again.

    Cheers.

Posting Permissions

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