This code takes ages to complete. Can it be made faster and more efficient: Sub…
- User
This code takes ages to complete. Can it be made faster and more efficient: Sub CheckServiceI()
Application.ScreenUpdating = False Application.EnableEvents = False
'Declare variables Dim wsStartPage As Worksheet Dim wsNamedPage As Worksheet Dim lastRow As Long Dim i As Long Dim j As Long Dim iColumnI As Long Dim strWorksheetName As String
'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
'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
'Check if the worksheet exists If WorksheetExists(strWorksheetName) Then Set wsNamedPage = Sheets(strWorksheetName)
'Get the last row in column B of the named worksheet Dim iLastRow As Long 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"
'Go to the next worksheet NextSheet:
End If End If Next i
Application.ScreenUpdating = True Application.EnableEvents = True
End Sub Function WorksheetExists(shtName As String) As Boolean Dim sht As Worksheet
On Error Resume Next Set sht = ThisWorkbook.Worksheets(shtName) On Error GoTo 0
WorksheetExists = Not sht Is Nothing End Function
- Assistant
- User
I am getting a subscript out of range error
- Assistant
If you are getting a "subscript out of range" error, it means that the code is trying to access an array element that does not exist.
One possible cause of this error is if the size of the array does not match the size of the range that was loaded into the array. For example, if you load a range of size (10,5) into an array of size (9,4), you may get a subscript out of range error.
To fix this error, make sure that the size of the array matches the size of the range that was loaded into the array.
Another possible cause of this error is if you are trying to access a worksheet or workbook that does not exist. Make sure that the worksheet or workbook name is spelled correctly and that it exists in the workbook.
You can also try to use Debug.Print statements to isolate the line of code that is causing the error and determine the exact nature of the error. For example, you can add a debug statement like this:
Debug.Print "i=" & i & ", j=" & j & ", strWorksheetName=" & strWorksheetNameThis will print the value of i, j, and strWorksheetName to the Immediate Window, which can be helpful in diagnosing the problem.
- User
The subscript out of range error is on this line : i=0, j=0, strWorksheetName=
- Assistant
If the subscript out of range error is occurring on the line where you declared the variables
i,j, andstrWorksheetName, it means that the error is occurring before the loop and may be related to how the variables are being initialized.Try initializing the variables before the loop like this:
Dim wsStartPage As Worksheet Dim strWorksheetName As String Dim i As Long, j As Long Application.ScreenUpdating = False Application.EnableEvents = False '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 = "" '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).ValueThis will ensure that the variables are initialized correctly before the loop starts.
If the problem persists, you may need to add additional debug statements to isolate the exact line of code causing the error. You can also use error handling to handle the error and provide more information about the error to the user.
- User
Can you please re write the entire code with your suggestion.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.