OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 855
- Office Version
- 365
- Platform
- 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
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.
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.
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
ListTasks2.xlsm | |||
---|---|---|---|
A | |||
3 | 20 | ||
4 | 21 | ||
5 | 22 | ||
6 | 23 | ||
7 | 24 | ||
8 | 25 | ||
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 | |||
3 | Task 20 | ||
4 | A | ||
5 | B | ||
6 | C | ||
7 | Task 22 | ||
8 | E | ||
9 | Task 23 | ||
10 | F | ||
11 | G | ||
12 | Task 24 | ||
13 | B | ||
14 | C | ||
15 | Task 25 | ||
16 | D | ||
17 | E | ||
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