I have this Excel/VBA code and here is what it does:
- I have 3 sheets, where first sheet has two columns that will be used (A,C).
- 2nd Sheet is just the raw data file that will be imported from external source.
- 3rd sheet is the final output.
- Column A in the first sheet is "Raw Column Headings" where the column headers from raw data table are copied, transposed, and pasted. Third column in the first sheet is the final headers that I need the raw ones to be changed to. Then, I will run a macro which will copy the final headers into the first row of second sheet and then copy/paste the entire columns with the certain headers specified in the code to the final output.
The problem is that this process takes about 20~30 seconds per file and I have so many files to be processed. Can anyone take a look at the code and tell me how it can be done faster?
Option Explicit
Private Function GetHeaders() As Collection
Dim result As New Collection
With result
.Add "Account_ID"
.Add "Claim_ID"
.Add "Account_Name"
.Add "Claim_Type"
.Add "Coverage"
.Add "Claim_Level"
.Add "Claim_Count"
.Add "File_Date"
.Add "File_Year"
.Add "Resolution_Date"
.Add "Resolution_Year"
.Add "Claim_Status"
.Add "Indemnity_Paid"
.Add "Disease_Category"
.Add "State_Filed"
.Add "First_Exposure_Date"
.Add "Last_Exposure_Date"
.Add "Claimant_Employee"
.Add "Claimant_DOB"
.Add "Claimant_Deceased"
.Add "Claimant_Name"
.Add "Claimant_DOD"
.Add "Claimant_Diagnosis_Date"
.Add "Product_Type"
.Add "Product_Line"
.Add "Company/Entity/PC"
.Add "Plaintiff_Law_Firm"
.Add "Asbestos_Type"
.Add "Evaluation_Date"
.Add "Tier"
.Add "Data_Source"
.Add "Data_Source_Category"
.Add "Jurisdiction/County"
.Add "Settlement_Demand"
.Add "Jury_Verdict"
.Add "Exposure_Site"
.Add "National_Defendant_Firm"
.Add "Local_Defendant_Firm"
.Add "Expense_Amount"
.Add "NCC_Expense_Amount"
.Add "Non_NCC_Expense_Amount"
End With
Set GetHeaders = result
End Function
Private Function FindHeaderRange(ByVal ws As Worksheet, ByVal header As String) As Range
Set FindHeaderRange = ws.Cells.Find(header, , , xlWhole)
End Function
Private Function BuildMessage(ByVal currentMessage As String, ByVal ws As Worksheet, ByVal header As String) As String
BuildMessage = currentMessage & vbLf & header & Space(1) & ws.Name
End Function
Public Sub ProjectionTemplateFormat()
On Error GoTo ExitSub
Sheets(1).Range("C2", Cells(Rows.Count, "C").End(xlUp)).Copy
Sheets(2).Range("A1").PasteSpecial transpose:=True
Range("A1").ClearOutline
Dim headers As Collection
Set headers = GetHeaders
Dim msg As String
Dim wsImport As Worksheet, wsMain As Worksheet
Set wsImport = ThisWorkbook.Sheets(2)
Set wsMain = ThisWorkbook.Sheets(3)
Application.ScreenUpdating = False
Dim header As Variant
Dim source As Range
Dim dest As Range
For Each header In headers
Set source = FindHeaderRange(wsImport, header)
If source Is Nothing Then
msg = BuildMessage(msg, wsImport, header)
Else
Set dest = FindHeaderRange(wsMain, header)
If dest Is Nothing Then
msg = BuildMessage(msg, wsMain, header)
Else
wsImport.Range(source.Offset(1), wsImport.Cells(Rows.Count, source.Column).End(xlUp)).Copy _
wsMain.Cells(Rows.Count, dest.Column).End(xlUp)(2)
End If
End If
Next
With wsMain
.Columns("A:AO").AutoFit
.Cells.ClearFormats
.Rows(1).Font.Bold = True
.Cells.Font.Name = "Georgia"
.Cells.Font.Color = RGB(0, 0, 225)
.Cells.Resize(wsMain.Rows.Count - 1).Offset(1).Interior.Color = RGB(216, 228, 188)
End With
'Apply Style
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
ActiveWindow.Zoom = 85
Next ws
Dim rng As Range
Set rng = wsMain.Cells
With rng.Borders
.LineStyle = xlDot
.Weight = xlThin
End With
Dim cell As Range
For Each cell In Range("$A$2:" & Range("$A$2").SpecialCells(xlLastCell).Address)
If Len(cell) > 0 Then cell = UCase(cell)
Next cell
ExitSub:
Application.ScreenUpdating = True
End Sub