JasonEnsor
Programmer
Hi Guys,
I have a single workbook that has 2 sheets of data. The All_Ok sheet has 11670 rows and the Calced sheet has 17631 rows. What I am doing is for each row in All Ok (based on unique student number), finding all occurrences of that student in the calced row, the student can show up many times. Once the student is found I pass values from 4 of the cells in the calced sheet to the All Ok sheet.
What I am using is the following
My Enclose Macro sets up the environment (screen updating etc..)
I use the following function to determine the last row in the worksheets
Then my main module looks like this
I know I should change the scope of my variables for the main method, but I am assuming that won't be making much of a difference speed wise. There is a lot of data being compared here and I still need to calculate other values from various sheets on the workbook, so I am hoping I can speed this section up, then just build the extra functionality in.
Many thanks in advance for any tips and ideas
Regards
J.
I have a single workbook that has 2 sheets of data. The All_Ok sheet has 11670 rows and the Calced sheet has 17631 rows. What I am doing is for each row in All Ok (based on unique student number), finding all occurrences of that student in the calced row, the student can show up many times. Once the student is found I pass values from 4 of the cells in the calced sheet to the All Ok sheet.
What I am using is the following
My Enclose Macro sets up the environment (screen updating etc..)
Code:
Option Explicit
Sub Enclose_Macro()
Dim StartCalc As Integer
Dim StartScreen As Boolean
Dim StartAlerts As Boolean
' Store Application Settings
StartCalc = Application.Calculation
StartScreen = Application.ScreenUpdating
StartAlerts = Application.DisplayAlerts
'Set Application Settings
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.StatusBar = True
' Call Main Macro
Call Main
Clean_Up:
On Error Resume Next
Application.Calculation = StartCalc
Application.ScreenUpdating = StartScreen
Application.DisplayAlerts = StartAlerts
Exit Sub
' Error Handling Code Here
End Sub
I use the following function to determine the last row in the worksheets
Code:
Option Explicit
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", After:=sh.Range("A1"), _
LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Then my main module looks like this
Code:
Option Explicit
Public ws_All_OK As Worksheet
Public ws_calced As Worksheet
Public calcedLastRow As Long
Public calcedCurrentRow As Long
Public currentLastRow As Long
Public currentRow As Long
Public student As Variant
Sub Main()
' Set Worksheet
Set ws_All_OK = ThisWorkbook.Sheets("Add Data")
Set ws_calced = ThisWorkbook.Sheets("Calced")
ws_All_OK.Activate
currentLastRow = LastRow(ws_All_OK)
For Each student In ws_All_OK.Range("A2:A" & currentLastRow)
student.Rows.EntireRow.Select
currentRow = ActiveCell.Row
Application.StatusBar = "Current Cell: tHEN " & currentRow
' Setup Student Id
studentNumber = ws_All_OK.Range("A" & currentRow).Value
ws_calced.Activate
calcedLastRow = LastRow(ws_calced)
For Each calcedStudent In ws_calced.Range("A2:A" & calcedLastRow)
calcedStudent.Rows.EntireRow.Select
calcedCurrentRow = ActiveCell.Row
' Set Test ID
testNumber = ws_calced.Range("A" & calcedCurrentRow).Value
If (StrComp(studentNumber, testNumber, vbTextCompare) = 0) Then
ws_All_OK.Range("C" & currentRow).Value = ws_All_OK.Range("C" & currentRow).Value + ws_calced.Range("B" & calcedCurrentRow).Value
ws_All_OK.Range("D" & currentRow).Value = ws_calced.Range("C" & calcedCurrentRow).Value
ws_All_OK.Range("E" & currentRow).Value = ws_calced.Range("D" & calcedCurrentRow).Value
ws_All_OK.Range("F" & currentRow).Value = ws_calced.Range("E" & calcedCurrentRow).Value
End If
Next calcedStudent
ws_All_OK.Activate
Next student
End Sub
I know I should change the scope of my variables for the main method, but I am assuming that won't be making much of a difference speed wise. There is a lot of data being compared here and I still need to calculate other values from various sheets on the workbook, so I am hoping I can speed this section up, then just build the extra functionality in.
Many thanks in advance for any tips and ideas
Regards
J.