+ Reply to Thread
Results 1 to 2 of 2

Named cells in a worksheetchange macro instead of hard cell refere

  1. #1
    Neil Goldwasser
    Guest

    Named cells in a worksheetchange macro instead of hard cell refere

    If anyone could help me here I’d be really grateful!

    I have the following code in my worksheet, so that if a cell in column 30,
    let's say AD1 has a value of "** N/A **" chosen from the drop-down list, then
    the cell to the right in column 31, AE1, automatically becomes "N/A".

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rTarget As Range
    Dim rCell As Range
    Set rTarget = Intersect(Target, Columns(30))
    If Not rTarget Is Nothing Then
    For Each rCell In rTarget
    If rCell.Value = "** N/A **" Then
    Application.EnableEvents = False
    rCell.Offset(0, 1).Value = "N/A"
    Application.EnableEvents = True
    End If
    Next rCell
    End If
    End Sub

    However, I have come to realise that if I were to insert a column before AD,
    this would mess it all up.
    If I could name the cells instead of using hard cell references, so that the
    cells in AD are named "Supported_By_2", and the automatically-changing cells
    in AE "Support_Type_2", is there a way of adapting the code above?

    It would be great if a message box could pop up as well to explain the
    reason behind the automatic change, e.g. “Support type 2 has automatically
    changed to N/A because a second support plan was not selected”. Is there a
    way of doing this?

    ----------------------------------------------
    Ideally, it would be really, really useful to have two or three of these
    functions described above. I tried inserting another section of code as well
    as the first, as shown

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rTarget As Range
    Dim rCell As Range
    Set rTarget = Intersect(Target, Columns(25))
    If Not rTarget Is Nothing Then
    For Each rCell In rTarget
    If rCell.Value = "** N/A **" Then
    Application.EnableEvents = False
    rCell.Offset(0, 1).Value = "N/A"
    Application.EnableEvents = True
    End If
    Next rCell
    End If
    End Sub

    I had hoped that this would turn Z-column cells into "N/A" if the
    corresponding Y-column cell was "** N/A **", but when I tried it I got an
    error message in the Visual Basic Editor saying
    "Compile error:
    Ambiguos name detected: Worksheet_Change"

    I am sure it is an embarrassingly simple answer, but what do I need to do in
    order to have more than one of these functions running in the same worksheet?

    On behalf of our many students who will benefit from the new database, I
    thank all those in advance who may be able to help me!
    Neil Goldwasser

  2. #2
    Bob Phillips
    Guest

    Re: Named cells in a worksheetchange macro instead of hard cell refere

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rTarget As Range
    Dim rCell As Range
    Set rTarget = Intersect(Target, Range("Supported_By_2"))
    If Not rTarget Is Nothing Then
    For Each rCell In rTarget
    If rCell.Value = "** N/A **" Then
    Application.EnableEvents = False
    rCell.Offset(0, 1).Value = "N/A
    Msgbox "Cell " & rCell.Offset(0, 1).address(False,False)
    & _
    " has automatically changed to N/A because
    a " & _
    "second support plan was not selected"
    Application.EnableEvents = True
    End If
    Next rCell
    End If
    End Sub

    this may get annoying witrh all those popups.

    Replace the exsisting change proc, don't add this as a new.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Neil Goldwasser" <[email protected]> wrote in
    message news:[email protected]...
    > If anyone could help me here I'd be really grateful!
    >
    > I have the following code in my worksheet, so that if a cell in column 30,
    > let's say AD1 has a value of "** N/A **" chosen from the drop-down list,

    then
    > the cell to the right in column 31, AE1, automatically becomes "N/A".
    >
    > Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    > Dim rTarget As Range
    > Dim rCell As Range
    > Set rTarget = Intersect(Target, Columns(30))
    > If Not rTarget Is Nothing Then
    > For Each rCell In rTarget
    > If rCell.Value = "** N/A **" Then
    > Application.EnableEvents = False
    > rCell.Offset(0, 1).Value = "N/A"
    > Application.EnableEvents = True
    > End If
    > Next rCell
    > End If
    > End Sub
    >
    > However, I have come to realise that if I were to insert a column before

    AD,
    > this would mess it all up.
    > If I could name the cells instead of using hard cell references, so that

    the
    > cells in AD are named "Supported_By_2", and the automatically-changing

    cells
    > in AE "Support_Type_2", is there a way of adapting the code above?
    >
    > It would be great if a message box could pop up as well to explain the
    > reason behind the automatic change, e.g. "Support type 2 has automatically
    > changed to N/A because a second support plan was not selected". Is there a
    > way of doing this?
    >
    > ----------------------------------------------
    > Ideally, it would be really, really useful to have two or three of these
    > functions described above. I tried inserting another section of code as

    well
    > as the first, as shown
    >
    > Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    > Dim rTarget As Range
    > Dim rCell As Range
    > Set rTarget = Intersect(Target, Columns(25))
    > If Not rTarget Is Nothing Then
    > For Each rCell In rTarget
    > If rCell.Value = "** N/A **" Then
    > Application.EnableEvents = False
    > rCell.Offset(0, 1).Value = "N/A"
    > Application.EnableEvents = True
    > End If
    > Next rCell
    > End If
    > End Sub
    >
    > I had hoped that this would turn Z-column cells into "N/A" if the
    > corresponding Y-column cell was "** N/A **", but when I tried it I got an
    > error message in the Visual Basic Editor saying
    > "Compile error:
    > Ambiguos name detected: Worksheet_Change"
    >
    > I am sure it is an embarrassingly simple answer, but what do I need to do

    in
    > order to have more than one of these functions running in the same

    worksheet?
    >
    > On behalf of our many students who will benefit from the new database, I
    > thank all those in advance who may be able to help me!
    > Neil Goldwasser




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1