Consulting

Results 1 to 2 of 2

Thread: Trying to reference a range of cells inside of a table and call a function OnChange

  1. #1
    VBAX Regular
    Joined
    Jul 2019
    Posts
    51
    Location

    Trying to reference a range of cells inside of a table and call a function OnChange

    Hello,

    I am trying to call a function anytime a cell changes within a certain column inside of a table. But, I need to call that column by it's column name and not index. Here is the code I have so far but I keep getting an "Object Required" error.

    Here is my code..

    Private Sub Worksheet_Change(ByVal Target As Range)    'Instansiate ListObject
        Dim tbl As ListObject
        Set tbl = Worksheets("Prod. Data").ListObjects("Table3")
        
        Dim KeyCells As ListColumns
        Set KeyCells = tbl.ListColumns("Shipping Method").Range.Select
    
    
        ' The variable KeyCells contains the cells that will cause an alert when they are changed.
        'Set KeyCells = Range("A1:C10")
    
    
        If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    
    
            ' Display a message when one of the designated cells has been changed. Place your code below.
            MsgBox "Cell " & Target.Address & " has changed."
    
    
        End If
    End Sub
    Thank you everyone.

  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi mongoose!
    something like below:
    Private Sub Worksheet_Change(ByVal Target As Range)    'Instansiate ListObject
        Dim tbl As ListObject
        Set tbl = Worksheets("Prod. Data").ListObjects("Table1")
        Dim KeyCells As Range
        Set KeyCells = tbl.ListColumns("Shipping Method").Range
    
        ' The variable KeyCells contains the cells that will cause an alert when they are changed.
        'Set KeyCells = Range("A1:C10")
    
        If Not Application.Intersect(KeyCells, Worksheets("Prod. Data").Range(Target.Address)) Is Nothing Then
    
            ' Display a message when one of the designated cells has been changed. Place your code below.
            MsgBox "Cell " & Target.Address & " has changed."
    
        End If
    End Sub

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
  •