Results 1 to 8 of 8

Thread: [RESOLVED] Excel VBA - Worksheet_Change Function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    20

    Resolved [RESOLVED] Excel VBA - Worksheet_Change Function

    Hi,

    I am setting up a worksheet that contains some vba functions and among them, I wrote a code with Worksheet_change in order to call up a userform if the cell value in the target changes to 'sent'. Here is my code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D531")) Is Nothing Then

    Select Case Target.Cells.Value
    Case "Sent"
    OrderSentTime_userform.Show
    End Select

    Else
    Unload OrderSentTime_userform
    Exit Sub

    End If

    End Sub

    The code works perfectly on its own. However, it doesn't when i fit it into the whole worksheet code, especially when i run another code that trying to clear the content of selected rows. And the error has been referred to the (case "Sent") as above. I couldn't figure out the reason for that. Could you please advice what's wrong to the code?

    Apologies if my explanation isn't clear enough, but i hope my sample worksheet attached help better.

    Thank you.

    xwxc
    Attached Files Attached Files

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA - Worksheet_Change Function

    I would start with formatting the code so its easier to read.

    Code:
        If Not Intersect(Target, Range("C5:C31")) Is Nothing Then
            Select Case Target.Cells.Value
                Case "Sent"
                    MsgBox ("Input Time")
            End Select
        Else
            Exit Sub
        End If
    But when you look at it you can see that a select case is not needed since there is only one case statement.

    Code:
        If Not Intersect(Target, Range("C5:C31")) Is Nothing Then
            If Target.Cells.Value = "Sent" Then
                MsgBox ("Input Time")
            End If
        Else
            Exit Sub
        End If
    What do you mean "whole worksheet code"? Do you mean if you place it in the "ThisWorkbook" class?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    20

    Re: Excel VBA - Worksheet_Change Function

    Hi,

    thanks for the amended code.

    I meant to put the worksheet_change code into the active worksheet, not the 'this workbook'.

    When i change the cell value in C5:C31 to 'sent', the code works perfectly, and that's what i wanted.
    But when i check the box to clear the content of row(s), with the 'clear' command button, a debug window pops out saying [run-time error'13': type mismatch], and then highlight the code [If Target.Cells.Value = "Sent" Then].

    I am not sure if the command button code contains anything that crash with the 'worksheet change code'?

    thx

    xwxc

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Worksheet_Change Function

    problem arises when target is multiple cells, limit code to only work if target is single cell
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Worksheet_Change Function

    Code:
        If Not Intersect(Target(1), Range("C5:C31")) Is Nothing Then
            If Target(1).Value = "Sent" Then
                MsgBox ("Input Time")
            End If
        End If
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    20

    Re: Excel VBA - Worksheet_Change Function

    Hi, many thanks, the code works perfectly.

    Do you mind to explain to me why the additional of #1 next to "target" makes such a different? I tried to do a search but couldn't be satisfied.

    Appreciate your assistance

    Thx,

    xwxc

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Worksheet_Change Function

    Target is a range, it may have only one cell (such as when user change a cell in column C) or a range of cells (such as when the code clear a row).

    When Target is a single cell, Target.Value returns a single value that can be used to compare with string value "Sent" without problem.
    When Target has two or more cells, Target.Value returns an array that CANNOT be used to compare with string value "Sent". This is why you hit the error on clearing.

    The use of Target(1) to make sure that you test only a single cell, that is the first cell in the Target range.

    Target(1) is the shortcut of Target.Cells(1,1).

    In more general cases, such as when user copy and paste to multiple cells at the same time then we need to test every cell in the Target range (but need to prompt only one MsgBox):
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim CheckRange As Range
        Dim aCell As Range
        
        Set CheckRange = Intersect(Target, Range("C5:C31"))
        If Not CheckRange Is Nothing Then
            For Each aCell In CheckRange
                If aCell.Value = "Sent" Then
                    MsgBox ("Input Time")
                    Exit For
                End If
            Next
        End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    20

    Re: Excel VBA - Worksheet_Change Function

    Many thanks

    xwxc

Posting Permissions

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



Click Here to Expand Forum to Full Width