'****************************************************************************
' Date: 2/26/08
' Author: Michael T. Smith
' Description: This workbook works in conjunction with Crytal Reports.
' Specifically to ???????????????????????????
' ????????????????????????????????????????????
' ????????????????????????????????????????????
'****************************************************************************
Sub Auto_Run()
' Check and see if a Crystal Report Excel output file <????? NAME ?????> exists. This file
' is used to populate the raw data worksheet in this book.
Dim sPath As String
sPath = "C:\Temp\Dougs Files Duns Load\ExcelLoading\CrExportdata.xls"
' Test if directory or file exists
If FileOrDirExists(sPath) Then
MsgBox sPath & " exists!"
Else
MsgBox sPath & " does not exist."
Application.DisplayAlerts = False
Application.Quit
End If
' Declare and populate an array with all worksheet names that need to be repopulated.
Dim aIndWrkShtArr(13)
aIndWrkShtArr(1) = "Finance"
aIndWrkShtArr(2) = "Comm-Media"
aIndWrkShtArr(3) = "Gov"
aIndWrkShtArr(4) = "Healthcare"
aIndWrkShtArr(5) = "Insurance"
aIndWrkShtArr(6) = "Mfg"
aIndWrkShtArr(7) = "Retail"
aIndWrkShtArr(8) = "Transportation"
aIndWrkShtArr(9) = "Travel"
aIndWrkShtArr(10) = "Non-Targeted"
aIndWrkShtArr(11) = "OtherIndustries"
aIndWrkShtArr(12) = "Partners+Influencers"
aIndWrkShtArr(13) = "AllIndustries"
' Unhide raw data sheet and make sure it's empty. Open new data workbook from Crystal Reports.
' Copy in new workbook data. Close new workbook.
Sheets("RawData").Visible = True
Sheets("RawData").Select
Cells.Select
Selection.ClearContents
Range("A1").Select
Workbooks.Open Filename:= _
"C:\Temp\Dougs Files Duns Load\ExcelLoading\CrExportData.xls"
Cells.Select
Selection.Copy
Windows("GetCrData.xls").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
Windows("CrExportData.xls").Close
' Call a procedure that will delete all data from the worksheets.
EmptyWorksheets (aIndWrkShtArr)
' Sort data in raw data sheet by industry - in preparation for populating individual industry sheets.
Sheets("RawData").Select
Cells.Sort Key1:=Range("C1"), Order1:=xlAscending, Key2:=Range("M1"), Order2:=xlDescending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
' Loop through the array of sheets and determine which RMDB industry name
' each sheet maps to. Then call a procedure that will populate each sheet.
Dim sWrkShtName As String
For i = 1 To UBound(aIndWrkShtArr)
Select Case aIndWrkShtArr(i)
Case "AllIndustries"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "AllIndustries"
Case "Finance"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Financial"
Case "Comm-Media"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Comm & Media/Ent"
Case "Gov"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Government"
Case "Healthcare"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Healthcare"
Case "Insurance"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Insurance"
Case "Mfg"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Manuf"
Case "Retail"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Retail"
Case "Transportation"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Transportation"
Case "Travel"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Travel"
Case "Non-Targeted"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Non-Target"
Case "OtherIndustries"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Other Industries"
Case "Partners+Influencers"
sWrkShtName = aIndWrkShtArr(i)
PopWorkSheets sWrkShtName, "Partners+Influencers"
End Select
Next i
' Call a procedure that will sort format the data in all worksheets.
FormatWrkSheets (aIndWrkShtArr)
' Make sure clipboard is empty before ending.
Application.CutCopyMode = False
Set fso = Nothing
End Sub
' Function is passed a path/file name or a directory name and the function returns TRUE if the
' file or directory exists FALSE if it doesn't.
' PathName : Supports Windows mapped drives or UNC
' File usage : Provide full file path and extension
' Folder usage : Provide full folder path
' Accepts with/without trailing "\" (Windows)
' Accepts with/without trailing ":" (Macintosh)
' In this instance it is passed a path and name of the Crystal Report Excel output file that
' that this workbook uses to populate the hidden RawData tab.
Function FileOrDirExists(PathName As String) As Boolean
Dim iTemp As Integer
'Ignore errors to allow for error evaluation
On Error Resume Next
iTemp = GetAttr(PathName)
'Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0
FileOrDirExists = True
Case Else
FileOrDirExists = False
End Select
'Resume error checking
On Error GoTo 0
End Function
' Procedure takes in an array of sheet names and removes all data from each sheet. And activate
' cell A1 in each sheet in preperation for pasting in new industry specific data.
Sub EmptyWorksheets(IndWrkShtArr As Variant)
For i = 1 To UBound(IndWrkShtArr)
With Sheets(IndWrkShtArr(i))
.Cells.ClearContents
' Sheets("RawData").Range("A:A").Copy Destination:=.Range("A1")
Sheets("RawData").Range("1:1").Copy Destination:=.Range("A1")
End With
Next i
End Sub
' Procedure is passed an industry worksheet name and a corresponding RMDB industry name. The
' procedure determines the start and row of the industry data in the raw worksheet, copies those
' rows into the appropriate industry worksheet.
Sub PopWorkSheets(sWrkShtName As String, sIndName As String)
Dim iRowStartNo As Integer
Dim iRowLastNo As Integer
' If the All Industries worksheet is being worked copy in all industry data otherwise just
' copy in industry specific data.
If sWrkShtName = "AllIndustries" Then
Cells.Select
Selection.Copy
Sheets(sWrkShtName).Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Sheets("RawData").Select
Else
' Call functions to determine start and end rows. Then select and copy rows into target
' industry worksheeet. Put focus back on cell A1 in raw data worksheet (prep for next
' pass through).
iRowStartNo = IndStartRowNo(sIndName)
iRowLastNo = IndLastRowNo(sIndName)
Range("A" & iRowStartNo, "N" & iRowLastNo).Select
Selection.Copy
Sheets(sWrkShtName).Select
Range("A2").Select
ActiveSheet.Paste
Range("A1").Select
Sheets("RawData").Select
Range("A1").Select
End If
End Sub
' Function returns the row # of the first row that contains the specified
' industry in the industry column of the raw data sheet.
Function IndStartRowNo(sIndustry As String) As Integer
Columns("C:C").Select
Selection.Find(What:=sIndustry, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
IndStartRowNo = ActiveCell.Row
End Function
' Function returns the row # of the last row that contains the specified
' industry in the industry column of the raw data sheet.
Function IndLastRowNo(sIndustry As String) As Integer
Columns("C:C").Select
Selection.Find(What:=sIndustry, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:= _
False, SearchFormat:=False).Activate
IndLastRowNo = ActiveCell.Row
End Function
' Procedure takes in an array of worksheet names and sorts and formats each.
Sub FormatWrkSheets(IndWrkShtArr As Variant)
For i = 1 To UBound(IndWrkShtArr)
Sheets(IndWrkShtArr(i)).Select
If IndWrkShtArr(i) = "AllIndustries" Then
Cells.Sort Key1:=Range("M1"), Order1:=xlDescending, Key2:=Range("B1"), Order2:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Rows("1:1").Select
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:B").Select
Selection.ColumnWidth = 34.29
Range("A2").Select
ActiveWindow.FreezePanes = True
Range("A1").Select
End If
Rows("1:1").Select
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:B").Select
Selection.ColumnWidth = 34.29
Range("A2").Select
ActiveWindow.FreezePanes = True
Range("A1").Select
Next i
End Sub