Results 1 to 2 of 2

Code fails in one file, works in copy?

Threaded View

  1. #1
    Registered User
    Join Date
    11-29-2012
    Location
    Madison, WI
    MS-Off Ver
    MS Office 2010 (Excel, Word, Access)
    Posts
    72

    Code fails in one file, works in copy?

    I didn't even know that this was possible. I designed a macro (with help from this forum) that looks up billing codes that we match up against a large number of phone numbers used by our employees. It worked on Friday. When we tried using it on Monday, it didn't work, instead throwing a Run Time Error 429: Cannot Create ActiveX Object when I tried to assign "ThisWorkbook" to an object.

    Thinking that there was some sort of update that went through, I started talking to our service desk people, while a fellow co-worker saved the file under a new name to try and tinker with it (we both work with VBA code, I just do so a bit more often.)

    When she tried to run it... it worked. Identical code, no changes beyond the filename, but it worked. So, shrugging my shoulders and thinking I must have just introduced a typo by mistake in the course of working with it, I copied the code in the file that worked, and I pasted it over the matching code in the file that didn't.

    It still doesn't work.

    So I have two pretty-much identical files, running identical code, and one works, but the other doesn't. And I am utterly perplexed at how this is even possible, let alone what happened to make it do this.

    Here's my code in the currently-broken original. Note that this code was originally designed for another file, which is why there are a lot of variables I don't even touch, but it's working despite those most of the time. I'm posting every line and character in the module, rather than trying to "file off the serial numbers." There's nothing proprietary or identifiable in here anyway. It processes through to the line "set wbVM = ThisWorkbook" and then throws the Error 429. Except when it doesn't.

    Option Explicit                                 'Requires declaration of all variables.
    
    'Declares variables, including named objects and ranges.
    Dim strFileName, strFilePath, strBill, strPhoneNum, strRStart, strREnd, strMonth, strYear, strAppr As String
    Dim strFormula, strLead, strLabel As String
    Dim i, n, x, y, lngCol, lngRow, lngData, lngCC, lngUniques As Long
    Dim curDT100, curDF521, curDF277, curDF272, curDF239, curDF211, curDF248 As Currency
    Dim blnTwoTypes
    Dim wbTelcom, wbBill, wbVM As Workbook
    Dim wsList, wsAlloc, wsTemp, wsInput As Worksheet
    Dim Phones, Codes, Allocs, Cell As Range
    
    Sub SetVars()
        'This subroutine creates variables to link to certain data sources and identify the scope of
        'certain parts of the lookup.
    
        'Find master Inventory and open file.
        If IsRepOpen("CL Master Long Distance Inventory 2012.xls") = False Then
            Workbooks.Open Filename:="\\files1\TelcomPublicShare\CL Master Long Distance Inventory 2012.xls", ReadOnly:=True
        End If
        Set wbTelcom = Workbooks("CL Master Long Distance Inventory 2012.xls")  'Identify Master Inventory.
        Set wsList = wbTelcom.Worksheets("Outbound")            'Identify tab with number inventory.
        Set wbVM = ThisWorkbook
    End Sub
    
    Sub GetPhoneCodes()
        'Accounting code lookup; contains all other functions/processes used to look up coding for phone numbers.
        'Routine is called from button on upper left corner of page.
        
        SetVars         'Sets data constants needed to look up budget coding.  Also asks for area code if needed.
        Application.ScreenUpdating = False
        
        'Detect range of phone numbers
        lngData = wbVM.Worksheets("Master Template").Range("C9").End(xlDown).Row
        Set Phones = ThisWorkbook.Worksheets("Master Template").Range("C9:C" & lngData)
        
        'Go through phone numbers and return billing code as string to the next column to the right
        For Each Cell In Phones
            strPhoneNum = Cell.Value
            Cell.Offset(0, 1) = CStr(FindCode(strPhoneNum))
        Next
        
        'Close files and restore screen updating.
        wbTelcom.Close SaveChanges:=False           'Close the master inventory, do NOT save changes.
        Application.ScreenUpdating = True
    End Sub
    
    Function FindCode(strPhoneNum) As String
        'Sorts through the fields in the Master Inventory that may contain a phone number; when it finds the
        'number it's looking for, it returns the first 7 digits of the "account name" as the coding string,
        'and exits the function.  If it doesn't find anything, it returns No Valid Code.
    
        'Application.ScreenUpdating = True
        wsList.Activate
        
        On Error GoTo ErrorHandler
            FindCode = Left(Cells(2, Cells.Find(strPhoneNum).Column), 7)
            Application.ScreenUpdating = True
        Exit Function
    
    ErrorHandler:
        Application.ScreenUpdating = True
        'FindCode = "No Valid Code"
        FindCode = InputBox(strPhoneNum & " is not in the" & vbCr _
            & "Master Inventory.  Please input" & vbCr _
            & "the seven-digit billing code.", "No Valid Code", "XXXXXXX")
    End Function
    
    Function IsRepOpen(strFileName) As Boolean
        'Function tests to see if file (passed as strFileName) is currently open.  Returns true/false.
        On Error Resume Next
            Workbooks(strFileName).Activate
            
        If Err = 0 Then
            IsRepOpen = True
        Else
            IsRepOpen = False
        End If
    End Function
    Here's my code in the version that actually works:

    Option Explicit                                 'Requires declaration of all variables.
    
    'Declares variables, including named objects and ranges.
    Dim strFileName, strFilePath, strBill, strPhoneNum, strRStart, strREnd, strMonth, strYear, strAppr As String
    Dim strFormula, strLead, strLabel As String
    Dim i, n, x, y, lngCol, lngRow, lngData, lngCC, lngUniques As Long
    Dim curDT100, curDF521, curDF277, curDF272, curDF239, curDF211, curDF248 As Currency
    Dim blnTwoTypes
    Dim wbTelcom, wbBill, wbVM As Workbook
    Dim wsList, wsAlloc, wsTemp, wsInput As Worksheet
    Dim Phones, Codes, Allocs, Cell As Range
    
    Sub SetVars()
        'This subroutine creates variables to link to certain data sources and identify the scope of
        'certain parts of the lookup.
    
        'Find master Inventory and open file.
        If IsRepOpen("CL Master Long Distance Inventory 2012.xls") = False Then
            Workbooks.Open Filename:="\\files1\TelcomPublicShare\CL Master Long Distance Inventory 2012.xls", ReadOnly:=True
        End If
        Set wbTelcom = Workbooks("CL Master Long Distance Inventory 2012.xls")  'Identify Master Inventory.
        Set wsList = wbTelcom.Worksheets("Outbound")            'Identify tab with number inventory.
        Set wbVM = ThisWorkbook
    End Sub
    
    Sub GetPhoneCodes()
        'Accounting code lookup; contains all other functions/processes used to look up coding for phone numbers.
        'Routine is called from button on upper left corner of page.
        
        SetVars         'Sets data constants needed to look up budget coding.  Also asks for area code if needed.
        Application.ScreenUpdating = False
        
        'Detect range of phone numbers
        lngData = wbVM.Worksheets("Master Template").Range("C9").End(xlDown).Row
        Set Phones = ThisWorkbook.Worksheets("Master Template").Range("C9:C" & lngData)
        
        'Go through phone numbers and return billing code as string to the next column to the right
        For Each Cell In Phones
            strPhoneNum = Cell.Value
            Cell.Offset(0, 1) = CStr(FindCode(strPhoneNum))
        Next
        
        'Close files and restore screen updating.
        wbTelcom.Close SaveChanges:=False           'Close the master inventory, do NOT save changes.
        Application.ScreenUpdating = True
    End Sub
    
    Function FindCode(strPhoneNum) As String
        'Sorts through the fields in the Master Inventory that may contain a phone number; when it finds the
        'number it's looking for, it returns the first 7 digits of the "account name" as the coding string,
        'and exits the function.  If it doesn't find anything, it returns No Valid Code.
    
        'Application.ScreenUpdating = True
        wsList.Activate
        
        On Error GoTo ErrorHandler
            FindCode = Left(Cells(2, Cells.Find(strPhoneNum).Column), 7)
            Application.ScreenUpdating = True
        Exit Function
    
    ErrorHandler:
        Application.ScreenUpdating = True
        'FindCode = "No Valid Code"
        FindCode = InputBox(strPhoneNum & " is not in the" & vbCr _
            & "Master Inventory.  Please input" & vbCr _
            & "the seven-digit billing code.", "No Valid Code", "XXXXXXX")
    End Function
    
    Function IsRepOpen(strFileName) As Boolean
        'Function tests to see if file (passed as strFileName) is currently open.  Returns true/false.
        On Error Resume Next
            Workbooks(strFileName).Activate
            
        If Err = 0 Then
            IsRepOpen = True
        Else
            IsRepOpen = False
        End If
    End Function
    Why does this work in one file, but not in the other one? Particularly when they're *identical files*? And why would the line "set wbVM = ThisWorkbook" be the line that's causing me to throw the error?
    Last edited by JasonLeisemann; 08-13-2013 at 12:42 PM. Reason: Needed to fix thread title

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Baffled Beginner/Intermediate Excel User
    By michaelaz21 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 05-28-2013, 08:19 PM
  2. [SOLVED] Excel 2007 : Baffled by the (lack of) result of a countif
    By neil40 in forum Excel General
    Replies: 1
    Last Post: 04-01-2012, 06:43 AM
  3. Strange Vlookup result - baffled
    By andycaps in forum Excel General
    Replies: 9
    Last Post: 01-18-2012, 08:58 AM
  4. Calculation in cell not updating - Totally baffled!
    By bmunoz64 in forum Excel General
    Replies: 1
    Last Post: 01-11-2012, 12:08 PM
  5. [SOLVED] thoroughly-baffled by InStr function w/ binary compare
    By terry b in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2005, 01:06 AM

Tags for this Thread

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