The VBA incluced below is accurate in providing all the information I require. However,…
- User
The VBA incluced below is accurate in providing all the information I require. However, it does take a long time to complete. Is there any way it can be speeded up....... Sub CheckServiceI() ' Unlock Sheet Me.Unprotect Password:="edit" Application.ScreenUpdating = False Application.EnableEvents = False Dim wsStartPage As Worksheet Dim wsNamedPage As Worksheet Dim iColumnI As Long Dim strWorksheetName As String Dim iLastRow As Long Dim bWorksheetExists As Boolean Dim services() As Variant Dim results() As Variant Dim i As Long, j As Long 'Set variables Set wsStartPage = ThisWorkbook.Sheets("Start Page") 'Find the column with the name "I" iColumnI = wsStartPage.Columns("I").Column 'Get the services and worksheet names to search services = wsStartPage.Range(wsStartPage.Cells(5, iColumnI), wsStartPage.Cells(38, iColumnI)).Value2 ReDim results(1 To UBound(services, 1), 1 To 1) 'Loop through the worksheet names and services For i = 1 To UBound(services, 1) strWorksheetName = Trim(services(i, 1)) If Len(strWorksheetName) > 0 Then bWorksheetExists = WorksheetExists(strWorksheetName) 'Check if the worksheet exists If bWorksheetExists Then Set wsNamedPage = ThisWorkbook.Sheets(strWorksheetName) 'Get the last row in column B of the named worksheet iLastRow = wsNamedPage.Columns("B").Rows.count 'Loop through the rows in column B of the named worksheet For j = iLastRow To 5 Step -1 If LCase(Trim(wsNamedPage.Cells(j, "B").Value2)) = "service" Then results(i, 1) = wsNamedPage.Cells(j, "H").Value2 Exit For End If Next j 'If the value "Service" is not found, set the value in column N to "NA" If j = 4 Then results(i, 1) = "NA" End If Else results(i, 1) = "Worksheet not found" End If End If Next i 'Write the results back to the worksheet wsStartPage.Range("J5:J38").Value2 = results Application.ScreenUpdating = True Application.EnableEvents = True Me.Protect Password:="edit" ' Lock Sheet End Sub
- Assistant
There are a few potential ways to speed up the execution of this VBA code:
-
Optimize the WorksheetExists Function: If the WorksheetExists function is taking a significant amount of time to determine if a worksheet exists, you can try optimizing this function to make it more efficient.
-
Limit the Range Selections: Instead of selecting a range for all services (from row 5 to 38), you can dynamically determine the range based on the number of services. This can help reduce the processing time.
-
Update cells in bulk: Instead of updating cells one by one in the loop, consider updating the entire range of cells at once. You can store the results in an array and then update the range all at once.
-
Turn off Calculation: If your worksheet has complex calculations, turning off calculation during the execution of the code can help speed up the process. You can use the Application.Calculation property to turn off calculations and then turn it back on once the code has finished executing.
By implementing these optimizations, you should be able to speed up the execution of the VBA code.
-