+ Reply to Thread
Results 1 to 2 of 2

VBA Compile Error: Ambiguous name detected

  1. #1
    Registered User
    Join Date
    08-08-2013
    Location
    North Pole
    MS-Off Ver
    Excel 2010
    Posts
    5

    VBA Compile Error: Ambiguous name detected

    Hi guys,

    I'm just having a little problem with some code. A colleague and I wrote two separate pieces of code and we're now trying to combine it into the one worksheet.
    I believe the problem lies with the fact that we have Two "Private Sub Worksheet_Change(ByVal Target As Range)" events in the same worksheet.
    When I remove one of the "Private Sub Worksheet_Change(ByVal Target As Range)" - I still can't get things to work.
    Does anyone have a solution? If so it would be much appreciated.

    Here it is:

    Private Sub Worksheet_Change(ByVal Target As Range)
    'Hard-coded ranges may be a maintenance issue

    On Error GoTo ErrHandler

    'Create a dialog which appears when Fail or Retest is selected for a test case and the corresponding JIRA column is blank
    If Not Intersect(Target, Range("$M$2:$M$1048576")) Is Nothing Then
    Application.EnableEvents = False
    If (Target.Value = "Fail" Or Target.Value = "Retest") And IsEmpty(Range("$R$" & Target.Row)) Then
    MsgBox "A JIRA must be present for a test of status Fail or Retest"
    'Range("$R$" & Target.Row).Font.Color = vbRed
    'Range("$R$" & Target.Row).Font.Bold = True
    'Range("$R$" & Target.Row).Value = "A JIRA number is required"
    End If
    Application.EnableEvents = True
    End If

    'Update the number of test cases without priority
    If Not Intersect(Target, Range("$I$2:$I$1048576")) Is Nothing Then
    Application.EnableEvents = False
    Dim numberWithoutPriority As Integer
    'Number test cases without priority = (number of non-blank test case ids) - (number of non-blank priorities)
    numberWithoutPriority = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("02. Test Cases").Range("$A$2:$A$1048576")) _
    - Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("02. Test Cases").Range("$I$2:$I$1048576"))
    ThisWorkbook.Sheets("01. Document Info").Range("$B$10").Value = numberWithoutPriority
    Application.EnableEvents = True
    End If

    'Update the number of test cases without status and number of test cases missing JIRAs
    If (Not Intersect(Target, Range("$M$2:$M$1048576")) Is Nothing) Or (Not Intersect(Target, Range("$R$2:$R$1048576")) Is Nothing) Then
    Application.EnableEvents = False
    Dim numberWithoutStatus As Integer
    Dim numberMissingJIRAs As Integer
    'Number test cases without status = (number of non-blank test case ids) - (number of non-blank statuses)
    numberWithoutStatus = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("02. Test Cases").Range("$A$2:$A$1048576")) _
    - Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("02. Test Cases").Range("$M$2:$M$1048576"))
    'Number test cases missing JIRA
    numberMissingJIRAs = 0
    For Each c In ThisWorkbook.Sheets("02. Test Cases").Range("$M$2:$M$1048576").Cells
    If IsEmpty(ThisWorkbook.Sheets("02. Test Cases").Range("$A$" & c.Row)) Then
    Exit For
    ElseIf ((c.Value = "Retest" Or c.Value = "Fail") And IsEmpty(ThisWorkbook.Sheets("02. Test Cases").Range("$R$" & c.Row))) Then
    numberMissingJIRAs = numberMissingJIRAs + 1
    End If
    Next
    ThisWorkbook.Sheets("01. Document Info").Range("$B$9").Value = numberWithoutStatus
    ThisWorkbook.Sheets("01. Document Info").Range("$B$11").Value = numberMissingJIRAs
    Application.EnableEvents = True
    End If

    ErrHandler:
    Application.EnableEvents = True
    End Sub


    Private Sub Worksheet_Activate()
    Application.CommandBars.FindControl(ID:=847).Enabled = False
    End Sub


    Private Sub Worksheet_Deactivate()
    Application.CommandBars.FindControl(ID:=847).Enabled = True
    Me.Name = "02. Test Cases"
    End Sub


    Private Sub Worksheet_Change(ByVal Target As Range)
    ' Does the validation range still have validation?
    If Not Intersect(Range("ValidationRange"), Target) Is Nothing Then
    Application.EnableEvents = False
    If HasValidation(Intersect(Range("ValidationRange"), Target)) = False Then
    Application.Undo
    MsgBox "Your last operation was canceled." & vbCrLf & _
    "It would have deleted data validation rules.", vbCritical
    End If
    End If
    Application.EnableEvents = True
    End Sub


    Private Function HasValidation(r As Range) As Boolean
    ' Returns True if every cell in Range r uses Data Validation
    Dim c As Range
    Dim x As XlDVType
    On Error Resume Next
    For Each c In r.Cells
    x = r.Validation.Type
    If Err Then
    HasValidation = False
    Exit Function
    End If
    Next c
    HasValidation = True
    End Function

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,539

    Re: VBA Compile Error: Ambiguous name detected

    Hello, and welcome to the forum. Unfortunately:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    I would suggest that, while you are adding the Code Tags, you also upload a sample workbook with the code, some typical (but non-sensitive data) and instructions as to what you are trying to achieve (in this case, primarily to combine the two Worksheet Change event handlers)


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] compile error ambiguous name detected!?
    By Margate in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2013, 08:49 PM
  2. [SOLVED] Compile error: Ambiguous name detected: Worksheet_Change
    By ericwilder in forum Excel Programming / VBA / Macros
    Replies: 32
    Last Post: 01-30-2013, 12:49 PM
  3. compile error: Ambiguous name detected :worksheet_change
    By ckz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-24-2012, 12:41 PM
  4. Compile error: Ambiguous name detected
    By Aletha in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2008, 05:48 PM
  5. Compile Error: Ambiguous Name Detected: Workbook_Open
    By Gos-C in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-19-2008, 10:46 AM

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