mcconnella
New Member
- Joined
- Jul 21, 2009
- Messages
- 4
I have built a macro to consolidate three unsimilar worksheets into one worksheet. Bascially the macros takes some information from an individual worksheet and adds data from the other worksheets based on the match of an item number (deal number in this case). I've tested a few times and seems to work okay with smaller data samples but when I use it on the actual data the macros just keeps running and never finishes. I left it going overnight last night to see if it would finish but it didn't.
I'm a beginner at this so I am just looking for some tips on how to make the code run faster. There's probably better ways to do what I've done and am just looking to improve!
Any suggestions would be greatly appreciated.
Here's the code:
I'm a beginner at this so I am just looking for some tips on how to make the code run faster. There's probably better ways to do what I've done and am just looking to improve!
Any suggestions would be greatly appreciated.
Here's the code:
Code:
Sub ConsolidateAllInfoNov3()
'Specify the start row for the New Workbook
Dim d
d = 2 'start at row 2
'Maximum Amount of rows to search through in each sheet, including the New Workbook
Dim MaxRows
MaxRows = 4000
'Specify the name of the new workbook
Application.ScreenUpdating = False
'Create new workbook
Workbooks.Add
ActiveWorkbook.saveas Filename:="P:\Admin\DTS\Consolidate Historial DTS\HistoricalData.xls", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'Write column headers in new workbook
Range("a1").Value = "Data Source"
Range("b1").Value = "Department"
Range("c1").Value = "Associate"
Range("d1").Value = "Complete Deal#"
Range("e1").Value = "Deal#"
Range("f1").Value = "Deal Type"
Range("g1").Value = "Property Type"
Range("h1").Value = "Deal Status"
Range("i1").Value = "Deal Date"
Range("j1").Value = "Property Address"
Range("k1").Value = "Vendor/Landlord"
Range("l1").Value = "V/L Client"
Range("m1").Value = "Purchaser/Tenant"
Range("n1").Value = "P/T Client"
Range("o1").Value = "Associate Gross"
Range("p1").Value = "Associate Bonus"
Range("q1").Value = "Office Gross"
Range("r1").Value = "Area (SF)"
Range("s1").Value = "Land Size"
Range("t1").Value = "Transaction Value"
'Copy Data from Received workbook
Workbooks.Open Filename:="P:\Admin\DTS\Consolidate Historial DTS\received.xls"
Windows("HistoricalData.xls").Activate
For i = 6 To MaxRows 'start at row 6
If Not IsEmpty(Workbooks("received.xls").Sheets("Sheet1").Range("A" & i)) Then
Range("A" & d).Value = "Received" 'Data Source
Range("B" & d).Value = "212" 'Department
Range("C" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("A" & i) 'Associate
Range("D" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("B" & i) 'Complete Deal#
'Skip Deal#
'Skip Deal Type
'Skip Property Type
Range("H" & d).Value = "Recieved" 'Deal Status
Range("I" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("E" & i) 'Deal Date
'Skip Property Address, not on received report
Range("K" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("C" & i) 'Vendor/Landlord
'Skip V/L Client, not on received report
Range("M" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("D" & i) 'Purchaser/Tenant
'Skip P/T Client, not on received report
Range("O" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("H" & i) 'Associate Gross
Range("P" & d).Value = Workbooks("received.xls").Sheets("Sheet1").Range("J" & i) 'Associate Bonus
'Skip Office Gross, not on received report
'Skip Area, not on received report
'Skip Land Size, not on received report
'Skip Transaction Value, not on received report
d = d + 1
End If
Next i
'Copy Data from Pipeline workbook
Workbooks.Open Filename:="P:\Admin\DTS\Consolidate Historial DTS\pipeline.xls"
Workbooks("HistoricalData.xls").Activate
For i = 8 To MaxRows
If Not IsEmpty(Workbooks("pipeline.xls").Sheets("Sheet1").Range("A" & i)) Then
Range("A" & d).Value = "Pipeline" 'Data Source
Range("B" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("G" & i) 'Department
Range("C" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("A" & i) 'Associate
Range("D" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("E" & i) 'Complete Deal#
'Skip Deal#
'Skip Deal Type
'Skip Property Type
Range("H" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("D" & i) 'Deal Status
Range("I" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("C" & i) 'Deal Date
Range("J" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("H" & i) 'Property Address
Range("K" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("I" & i) 'Vendor/Landlord
'Skip V/L Client, not on pipeline report
Range("M" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("J" & i) 'Purchaser/Tenant
'Skip P/T Client, not on pipeline report
Range("O" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("M" & i) 'Associate Gross
'Skip Associate Bonus, not on pipeline report
Range("Q" & d).Value = Workbooks("pipeline.xls").Sheets("Sheet1").Range("L" & i) 'Office Gross
'Skip Area, not on received report
'Skip Land Size, not on received report
'Skip Transaction Value, not on received report
d = d + 1
End If
Next i
'Split complete deal# into Deal#, Deal Type and Property Type in Historial Data workbook
Workbooks("HistoricalData.xls").Activate
For i = 2 To MaxRows
If Not IsEmpty(Workbooks("HistoricalData.xls").Sheets("Sheet1").Range("D" & i)) Then
For h = 2 To MaxRows
Range("E" & h).Formula = "=IF(LEN(D" & h & ")=8,LEFT(D" & h & ",4),IF(LEN(D" & h & ")=9,LEFT(D" & h & ",5),IF(LEN(D" & h & ")=10,LEFT(D" & h & ",6),IF(LEN(D" & h & ")=12,LEFT(D" & h & ",5),LEFT(D" & h & ",6)))))" 'Insert formula into Deal #
'Copy of Formula =IF(LEN(E" & h & ")=8,LEFT(E" & h & ",4),IF(LEN(E" & h & ")=9,LEFT(E" & h & ",5),IF(LEN(E" & h & ")=10,LEFT(E" & h & ",6),IF(LEN(E" & h & ")=12,LEFT(E" & h & ",5),LEFT(E" & h & ",6)))))
Range("F" & h).Formula = "=IF(LEN(D" & h & ")=8,MID(D" & h & ",5,1),IF(LEN(D" & h & ")=9,MID(D" & h & ",6,1),IF(LEN(D" & h & ")=10,MID(D" & h & ",7,1),IF(LEN(D" & h & ")=12,MID(D" & h & ",6,1),MID(D" & h & ",7,1)))))" 'Insert formula into Deal Type
'Copy of Formula from Deal Type field =IF(LEN(E" & h & ")=8,MID(E" & h & ",5,1),IF(LEN(E" & h & ")=9,MID(E" & h & ",6,1),IF(LEN(E" & h & ")=10,MID(E" & h & ",7,1),IF(LEN(E" & h & ")=12,MID(E" & h & ",6,1),MID(E" & h & ",7,1)))))
Range("G" & h).Formula = "=IF(LEN(D" & h & ")<11,RIGHT(D" & h & ",3),IF(LEN(D" & h & ")=12,MID(D" & h & ",7,3),MID(D" & h & ",8,3)))" 'Insert formula into Property Type
'=IF(LEN(D" & h & ")<11,RIGHT(D" & h & ",3),IF(LEN(D" & h & ")=12,MID(D" & h & ",7,3),MID(D" & h & ",8,3)))
Next h
End If
Next i
'Copy Data from Transaction Report workbook
Workbooks.Open Filename:="P:\Admin\DTS\Consolidate Historial DTS\transaction.xls"
Windows("HistoricalData.xls").Activate
'Match deal numbers in HistoricalData and for every match copy deal status(?),v/l client, p/t client, area, land size and transaction value
For i = 8 To MaxRows
If Not IsEmpty(Workbooks("transaction.xls").Sheets("Sheet1").Range("B" & i)) Then
DealId = Workbooks("transaction.xls").Sheets("Sheet1").Range("A" & i).Value 'look at Deal# in Transaction Report
For h = 2 To MaxRows
If Trim(Workbooks("HistoricalData.xls").Sheets("Sheet1").Range("E" & h).Value) = Trim(DealId) Then
Range("A" & h).Value = "Transaction" 'Change data source to Transaction
Range("J" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("E" & i) 'Copy Property Address
Range("H" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("B" & i) 'Copy Deal Status
Range("L" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("G" & i) 'Copy V/L client
Range("N" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("J" & i) 'Copy P/T client
Range("R" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("M" & i) 'Copy Area
Range("S" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("N" & i) 'Land Size
Range("T" & h).Value = Workbooks("transaction.xls").Sheets("Sheet1").Range("O" & i) 'Transaction Value
End If
Next h
End If
Next i
'Close pipeline workbook without changes
Workbooks("pipeline.xls").Activate
ActiveWorkbook.Close savechanges:=False
'Close transaction workbook without changes
Workbooks("transaction.xls").Activate
ActiveWorkbook.Close savechanges:=False
'Close received workbook without changes
Workbooks("received.xls").Activate
ActiveWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: