Cannot get get While or Do loop to do the simple task required. End up with endless looping. I'm missing something basic.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
I cannot get myself out of an endless While or Do loop while attempting to help another MrExcel lister. Raw data is as follows. I loop data in this worksheet

ListTasks2.xlsm
A
320
421
522
623
724
825
List


For each number in the data above I then look for the word "Task" to determine when to end the while or do loop when looping through the data below.

ListTasks2.xlsm
A
3Task 20
4A
5B
6C
7Task 22
8E
9Task 23
10F
11G
12Task 24
13B
14C
15Task 25
16D
17E
Tasks


Code is supposed to iterate through the data for each task number (shown in mini sheet List) then look in data in mini sheet Tasks and get the letters under the respective task. For example, if I send a cell object to the sub with the loop -- such as cell A3 for Task 20 -- code should report A, B, and C then exit the loop. I tried While and Do to no avail. Looping is endless rather than merely reporting the three values for Task 20 in A3.

I've tried different approaches and all result in an endless loop as the condition sought to end the loop is not found.

VBA Code:
Sub ListToTasks()

    Dim wsList As Worksheet
    Dim wsTasks As Worksheet
    
    Dim rAnchorCellList As Range
    Dim rAnchorCellTasks As Range
    
    Dim rListCells As Range
    Dim rListCell As Range
    
    Dim rTasksCell As Range
    
    Dim iColumns As Long
    Dim iRows As Long
    
    Set wsList = ThisWorkbook.Worksheets("List")
    Set wsTasks = ThisWorkbook.Worksheets("Tasks")
    
    Set rAnchorCellList = wsList.Range("A3")
    Set rAnchorCellTasks = wsTasks.Range("A3")
    
'   Need to clear existing results.
    With rAnchorCellList.CurrentRegion
        iColumns = .Columns.Count
        iRows = .Rows.Count
    End With
    
    rAnchorCellList.Offset(, 1).Resize(iRows, iColumns - 1).Clear
    
'   Set range object to point to the one column range of task numbers in worksheet List.
    Set rListCells = rAnchorCellList.Resize(iRows, 1)
    
'   Iterate each number in the List worksheet.
    For Each rListCell In rListCells
        
'       Locates the string "Task " & rListCell.Value in the Task worksheet.
'       rTasksCell is a ByRef parameter that is set to the cell where the string is found.
        Call FindStringInSheet(wsTasks, "Task " & rListCell.Value, rTasksCell)
        
        If Not rTasksCell Is Nothing _
         Then
         
            Call GetTaskList(rTasksCell)

        End If
    Next
    
End Sub


Sub GetTaskList(ByVal prTasksCell As Range)

    Dim sTaskAlpha As String
    
    Dim iOffset As Long
    
    Dim bDone As Boolean
    
    sTaskAlpha = ""
    
    bDone = False
    
Debug.Print "prTasksCell  = " & prTasksCell.Address
    
'    Do
'        iOffset = iOffset + 1
'        sTaskAlpha = prTasksCell.Offset(iOffset)

'Debug.Print "sTaskAlpha = " & sTaskAlpha

'        If sTaskAlpha Like "Task*" Then bDone = True
'
'    Loop While bDone = False
'
    
'    While Not sTaskAlpha Like "Task*"
'
'        iOffset = iOffset + 1
'        sTaskAlpha = rTasksCell.Offset(iOffset)
'
'Debug.Print "sTaskAlpha = " & sTaskAlpha
'
'    Wend

End Sub

The debug.print in the second (looping) sub reports the correct cells:

prTasksCell = $A$3
prTasksCell = $A$7
prTasksCell = $A$9
prTasksCell = $A$12
prTasksCell = $A$15
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If you need the result into the worksheet cells then set in List!B3, near the firs task number, the following formula
Excel Formula:
=LET(TaskN,A3,tArr,Tasks!$A$3:$A$20,fStr,TEXTJOIN(";",TRUE,IF(LEFT(tArr,7)="Task "&TaskN,"#",IF(LEFT(tArr,4)="Task","*",IF(tArr<>"",tArr,""))))&"*",farr,TEXTSPLIT(TEXTBEFORE(TEXTAFTER(fStr,"#"),"*"),";",,TRUE),farr)
Copy then down

Requires Office 365
 
Upvote 0
Thank you for the response but I need to do this with code as there is additional processing needed after I get those letters for a specific task. So I still need assistance with a while or do loop.
 
Upvote 0
I can't run your code because you have not provided your subroutine FindStringInSheet
One issue might be that it is not looping through to the next find or not ending when it loops back to the find the first value again.

For the loop issue you are only exiting when you find the next occurence of Task. Assuming all rows are populated add a test for hitting the end being a blank cell. eg
VBA Code:
If sTaskAlpha Like "Task*" Or sTaskAlpha = "" Then bDone = True

PS: I don't use While / Wend since that is superceded syntax and is there for backward compatibility.
 
Last edited:
Upvote 0
Solution
In case that Alex's message doesn't clarify the situation, you might use the following function:
VBA Code:
Function GetTasks(ByVal TaskN As Long, TTable As Range) As String
Dim StaR As Long, EndR As Long
'
For I = 1 To TTable.Rows.Count
    If Left(TTable.Cells(I, 1), 5) = "Task " And StaR <> 0 Then
        EndR = I
        Exit For
    End If
    If TTable.Cells(I, 1) = "Task " & TaskN Then
        StaR = I
    End If

Next I
If EndR = 0 Then EndR = I - 1
If StaR > 0 Then
    For I = StaR + 1 To EndR - 1
        If TTable.Cells(I, 1) <> "" Then
            GetTasks = GetTasks & TTable.Cells(I, 1) & ";"
        End If
    Next I
Else
    GetTasks = ";"
End If
End Function
Put the code into a standard module of your vba project then get the tasks in your variable by using
VBA Code:
myVariable = GetTasks(Sheets("List").Range("A3"),Sheets("Tasks").Range("A1:A100"))
 
Upvote 0
I found the issue. My code was looking for the word Task as a condition for exiting the loop. BUT, for the last Task entry there is no occurrence of the word Task at the end of the range. So needed to add two conditions:

VBA Code:
Do Until Left(sTaskAlpha, 4) = "Task" Or sTaskAlpha = ""
 
Upvote 0
Although necessary code snipped has not been provided, the issue has been already identified and solved in post #4.

Therefore I switched the marked solution post accordingly by also considering it will help future readers in a more generic way who would be using any kind of loop method.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top