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?
Bookmarks