OK, Here it is...
Option Explicit
Sub Renumber() 'Reformat in Telephone Number Format
'This applet is to allow users to take phone numbers from up to two area
codes from a Revers Directory and _
Match them against the the latest Canadian Do Not Call List, to get a list
of callable numbers.
Dim n As Long
Dim j As Long
Dim i As Long
Dim PctDone As Double
Dim q As Long
Dim ws As Worksheet
Dim wb As Workbook
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb3 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim z As Long
Dim areaCode1, areaCode2 As String
Dim arr As Variant
Dim arr2 As Variant
'Dim arr3 As Variant
Dim arr4 As Variant
Dim ws4 As Worksheet
Dim Response As Variant
Dim myErrorRng As Range
Dim myRegExp As Object, myMatches As Object
Dim rg As Range, c As Range
ClearOldData
DoEvents
Application.ScreenUpdating = False
'allow the user to enter a couple of area codes
areaCode1 = InputBox("Enter an Area Code", "Area Code 1", "", 80)
areaCode2 = InputBox("Enter a Second Area Code, (if required)", "Area
Code 2", "", 80)
'Start the Progress Bar
Progress.Show
'Make Calculation manual to speed up the application
Application.Calculation = xlCalculationManual
'Open the data file from the first Area Code
Set wb = ThisWorkbook
Set wb1 = Workbooks.Open(Filename:=Environ("Userprofile") &
"\Desktop\" & areaCode1 & ".csv", ReadOnly:=True)
Set ws = wb.Worksheets("Do Not Call List")
Set ws1 = wb1.Worksheets(areaCode1)
PctDone = 0.07
Call UpdateProgress(PctDone)
'get the last row of thefirst area code's data
n = ws1.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
ws.Activate
arr = ws1.Range("A1:a" & n).Value ' area code
arr2 = ws1.Range("b1:b" & n).Value ' phone number
'dimension the arrays
ReDim arr3(1 To n, 1 To 1)
ReDim arr4(1 To n, 1 To 1)
For z = LBound(arr) To UBound(arr)
' concatenate and format the area code and phone number for
areaCode1
arr4(z, 1) = "(" & Left(arr(z, 1), 3) & ") " & Left(arr2(z, 1),
3) & "-" & Right(arr2(z, 1), 4)
If z = Int(UBound(arr) * Round(z / UBound(arr), 1)) Then
Debug.Print Int(UBound(arr) * Round(z / UBound(arr), 1))
' Call UpdateProgress(PctDone)
End If
Next
PctDone = 0.3
Call UpdateProgress(PctDone)
' populate the phone numbers starting in row 1
ws.Range("A1:A" & n) = arr4
' close the areaCode1 workbook
wb1.Close savechanges:=False
' clear the arrays
Erase arr
Erase arr2
Erase arr4
' open areaCode2.csv (if necessary)
PctDone = 0.5
Call UpdateProgress(PctDone)
If areaCode2 = "" Then GoTo XXX
Set wb2 = Workbooks.Open(Filename:=Environ("Userprofile") &
"\Desktop\" & areaCode2 & ".csv", ReadOnly:=True)
Set ws2 = wb2.Worksheets(areaCode2)
'get the last row of areaCode2 data
q = ws2.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
'dimension the arrays
arr = ws2.Range("A1:a" & q).Value ' area code
arr2 = ws2.Range("b1:b" & q).Value ' phone number
ReDim arr4(1 To q, 1 To 1)
For z = LBound(arr) To UBound(arr)
' concatenate and format the area code and phone number for
areaCode2
arr4(z, 1) = "(" & Left(arr(z, 1), 3) & ") " & Left(arr2(z, 1),
3) & "-" & Right(arr2(z, 1), 4)
Next
' populate the phone numbers below areaCode1
ws.Range("A" & n + 1 & ":A" & n + q).Value = arr4
' close the areaCode2 workbook
wb2.Close savechanges:=False
XXX:
ws.Columns("A:A").Columns.AutoFit
Application.Calculation = xlCalculationAutomatic
Application.Goto ws.Range("A1"), Scroll:=True
PctDone = 0.6
Call UpdateProgress(PctDone)
'From this point on, we'll be working on the second worksheet
'Open the PhoneNumbers.txt file from the desktop and find out how many
rows there are
Set wb3 = Workbooks.Open(Filename:=Environ("Userprofile") &
"\Desktop\PhoneNumbers.txt", ReadOnly:=True)
wb3.Activate
n = Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
ActiveSheet.Range(Cells(1, 1), Cells(n, 1)).Select
'Copy and paste the data into the DNCL3 workbook
Application.CutCopyMode = False
Selection.Copy
Windows("DNCL3.xlsm").Activate
Worksheets("Numbers from Reverse Directory").Activate
Range("A1").Select
ActiveSheet.Paste
'Sort the data
ActiveWorkbook.Worksheets("Numbers from Reverse
Directory").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Numbers from Reverse
Directory").Sort.SortFields. _
Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Numbers from Reverse Directory").Sort
.SetRange Range("A1:A5001")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Find out the number of rows in the sorted list
Set ws4 = wb.Worksheets("Numbers from Reverse Directory")
n = ws4.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
PctDone = 0.7
Call UpdateProgress(PctDone)
'Get rid of extraneous data from the Reverse Telephone Directory
With ws4
q = ws4.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
.Columns("A").Replace What:="begin_of_the_skype_highlighting
", Replacement:="" _
, LookAt:=xlPart, SearchOrder:=xlByRows,
MatchCase:=False, SearchFormat _
:=False, ReplaceFormat:=False
.Columns("A").Replace What:=" end_of_the_skype_highlighting ",
Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows,
MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
End With
PctDone = 0.8
Call UpdateProgress(PctDone)
'This is the heart of the program. It takes the phone number from
each line and determines if there is a match in the DNCL. _
If so, the corresponding row number is placed in column E; otherwise
#N/A is shown
With ws4
.Range("C2:C" & q).FormulaR1C1 =
"=MID(R[0]C[-2],INDEX(FIND({""(" & areaCode1 & """,""(" & areaCode2 & _
"""},R[0]C[-2]),MATCH(TRUE,ISNUMBER(FIND({""(" & areaCode1 &
""",""(" & areaCode2 & """},R[0]C[-2])),0)),14)"
.Range("E2:E" & q).FormulaR1C1 = "=MATCH(RC[-2],'Do Not Call
List'!C[-4],0)"
.Range("E2:E" & q).Value = .Range("E2:E" & q).Value
' The headings are erased, and the number of rows is determined and
sorted again.
.Range("A1:F1").ClearContents
q = .Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("E2:E" & q),
SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ws4.Sort
.SetRange Range("A1:E" & q)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
PctDone = 0.9
Call UpdateProgress(PctDone)
'Dave Peterson's suggestions on copying the rows I want
With ws4
Set myErrorRng = Nothing
On Error Resume Next
Set myErrorRng = .Range("E:E") _
.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If myErrorRng Is Nothing Then
MsgBox "No cells with constant errors in them"
Else
myErrorRng.EntireRow.Select
'myErrorRng.Offset(0, -4).Select
'or to copy them to the clipboard
myErrorRng.Offset(0, -4).Copy
End If
End With
PctDone = 1#
Call UpdateProgress(PctDone)
Application.ScreenUpdating = True
Unload Progress
Call MsgBox("Call List Complete", vbInformation, "Do Not Call List")
End Sub
Sub ClearOldData()
Application.ScreenUpdating = False
Worksheets("Do Not Call List").Columns("A:B").ClearContents
Sheets("Numbers from Reverse Directory").Columns("A:F").ClearContents
Application.ScreenUpdating = True
End Sub
Sub UpdateProgress(Pct)
With Progress
.FrameProgress.Caption = Format(Pct, "0%")
.LabelProgress.Width = Pct * (.FrameProgress.Width - 0)
End With
' The DoEvents statement is responsible for the form updating
DoEvents
End Sub