I'm having a problem of iterating through a range of items in a for loop:
How the procedure is supposed to work-
I start off on worksheet 1("Tracking Spreadsheet"), and depending on a change in worksheet 1 ( selecting "yes" in a cell) it would transfer you to worksheet 2("Deferred Submittals") and then iterate through a range of cells ( A1:A20 for example) from worksheet 2. The for loop would keep going until it reached a cell that was empty, and stop and then proceed to write into that cell.
How its working now-
I start off on worksheet 1, and depending on a change in worksheet 1 ( selecting "yes" in a cell) it would transfer you to worksheet 2 and then iterate through a range of cells ( A1:A20 for example) from worksheet 2. However, instead of iterating through worksheet 2's A1:A20, it would iterate through worksheet 1's A1:A20. After it found an empty cell in worksheet 1's A1:A20 range, it would then fill in the corresponding cell in Worksheet 2 ( If A5 is empty on worksheet 1, it would then fill in A5 on worksheet 2).
I was hoping to get help on having the range iterate through the active sheet at the time ( worksheet 2) instead of iterating through worksheet 1.
My code so far:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
' Deferred Submittal, Column C = 3
Dim cellTextDS As String
Dim deferredArray As Range, deferredCell As Range, deferredRowEmpty As Long
deferredRowEmpty = 1
' Deferred Submittal, Column C
If Target.Column = 3 Then
Row = Target.Row
cellTextDS = ActiveSheet.Range("C" & Row).Text
If cellTextDS = "Yes" Then
Sheets(3).Activate
Set deferredRange = Workbooks("BPS Tracking Sheet v6.xlsm").Worksheets("Deferred Submittals").Range("A1:A20")
For Each deferredCell In deferredRange
Sheets(3).Activate
MsgBox "inside deferredCell is " & deferredCell
MsgBox " active sheet currently is " & name
If IsEmpty(Range("A" & deferredRowEmpty).Value) = True Then Exit For
MsgBox " deferredRowEmpty is " & deferredRowEmpty
deferredRowEmpty = deferredRowEmpty + 1
Next deferredCell
MsgBox "Moving to 'Deferred Submittals' tab in order to input more information. row is " & deferredRowEmpty
ActiveSheet.Range("A" & deferredRowEmpty).Value = "empty"
End If
End If
End Sub
Any help would be really appreciated!
p.s. the code is in the worksheet_change section of worksheet 1 if that makes any difference.
p.s.s. I've tried using this stackoverflow method for setting workbooks and worksheets, to no avail
p.s.s.s. posted the rest of my code for this portion. There is more code in this section, but it is literally just copy and paste of this section but for different columns. I've also edited what worksheet 1 and 2 are, but they are the different worksheet tabs in this workbook, specifically worksheet 1 = "Tracking Spreadsheet" and worksheet 2 = "Deferred Submittals"