The Sub CheckServiceI() works and retrieves the values required. However, it takes about…
- User
The Sub CheckServiceI() works and retrieves the values required. However, it takes about 8 minutes to complete. I believe that the coding may be doing something that is not required. Looking at the comments in the codeing that start'xxxx, I think the sequence should be as below. 'Declare variables 'Set variables 'Find the column with the name "I" 'If the cell value in column I is not blank, get the worksheet name 'Check if the worksheet exists 'Get the last row in column B of the named worksheet 'Loop through the rows in column B of the named worksheet 'If the value "Service" is found in column B, get the value from column H 'If the value "Service" is not found, set the value in column J to "NA" 'Go to the next worksheet
I have included the full code below with its comments and how it is currently written. Will my sequence as described in the comments above still allow the code to achieve the desired result, and will it make it faster.
Sub CheckServiceI()
Application.ScreenUpdating = False Application.EnableEvents = False
'Declare variables Dim wsStartPage As Worksheet Dim wsNamedPage As Worksheet Dim iColumnI As Long Dim lastRow As Long Dim i As Long, j As Long Dim strWorksheetName As String Dim iLastRow As Long Dim bWorksheetExists As Boolean
'Set variables Set wsStartPage = ThisWorkbook.Sheets("Start Page")
'Find the column with the name "I" iColumnI = wsStartPage.Columns("I").Find(What:="I", LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
'Loop through the rows in column B of the named worksheet For i = 5 To 34 strWorksheetName = "" bWorksheetExists = False
'If the cell value in column I is not blank, get the worksheet name If wsStartPage.Cells(i, iColumnI).Value <> "" Then strWorksheetName = wsStartPage.Cells(i, iColumnI).Value bWorksheetExists = WorksheetExists(strWorksheetName) End If
'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 the value "Service" is found in column B, get the value from column H If LCase(Trim(wsNamedPage.Range("B" & j).Value)) = "service" Then wsStartPage.Range("J" & i).Value = wsNamedPage.Range("H" & j).Value2 GoTo NextSheet End If Next j
'If the value "Service" is not found, set the value in column J to "NA" wsStartPage.Range("J" & i).Value = "NA" End If
'Go to the next worksheet NextSheet: Next i
Application.ScreenUpdating = True Application.EnableEvents = True
End Sub
Function WorksheetExists(shtName As String) As Boolean On Error Resume Next WorksheetExists = Not ThisWorkbook.Sheets(shtName) Is Nothing On Error GoTo 0 End Function
- Assistant
Your suggested sequence appears correct and should achieve the desired result. It may also improve the speed of the code as it avoids unnecessary looping through rows.
Here is the updated code with the revised sequence:
Sub CheckServiceI()
Application.ScreenUpdating = False Application.EnableEvents = False
- User
code still took about 7 minutes to complete which is a very long time. Any suggestions
- Assistant
- User
Can you please change the code so that in the sheet 'Start Page' M5:M24 are where the sheet names are, and the results found are now displayed in Column N of the sheet 'Start Page'. Nothing changes in the searched sheets.
- Assistant