I have two sheets. Sheet 1 (Customer tracker - OC) has a column which contains either yes or no for every row. If the value is yes , the two adjacent cells to the right have two values in them.
I would like to create a do while / for each macro that cycles through each cell in the yes/no column in sheet1, and where the value is yes , creates a new row in the blank sheet 2 which the two adjacent values from sheet 1.
Ie
Sheet1
A B C
1 YES 4 6
2 NO
3 YES 91 42
desired result in sheet 2 ("contracts")
A B
1 4 6
2 91 42
I have the below code , but it's erroring with "overflow".
Sub ForEach_Loop()
Dim TrackerLastRow As Integer
Dim TrackerCurrentRow As Integer
Dim ContractLastRow As Integer
Dim ContractCurrentRow As Integer
TrackerLastRow = Worksheets("Customer Tracker - OC").Range("T1").End(xlDown).Row
TrackerCurrentRow = 1
ContractLastRow = Worksheets("Contracts").Range("A1").End(xlDown).Row
ContractCurrentRow = 1
Do While TrackerCurrentRow <= TrackerLastRow
If Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Value = "Yes" Then
Worksheets("Contracts").Range("A" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 1).Value
Worksheets("Contracts").Range("B" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 2).Value
TrackerLastRow = TrackerLastRow + 1
TrackerCurrentRow = TrackerCurrentRow + 1
ContractLastRow = ContractLastRow + 1
ContractCurrentRow = ContractCurrentRow + 1
End If
TrackerCurrentRow = TrackerCurrentRow + 1
ContractCurrentRow = ContractCurrentRow + 1
Loop
End Sub
Any help would be appreciated
It seems you are over-incrementing your vars and over-extending the functional limits of an integer type var.
Option Explicit
Sub ForEach_Loop()
Dim TrackerLastRow As Long, TrackerCurrentRow As Long
Dim ContractLastRow As Long, ContractCurrentRow As Long
With Worksheets("Contracts")
ContractLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
ContractCurrentRow = 1
End With
With Worksheets("Customer Tracker - OC")
TrackerLastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
TrackerCurrentRow = 1
Do While TrackerCurrentRow <= TrackerLastRow
If .Cells(TrackerCurrentRow, "T").Value = "Yes" Then
Worksheets("Contracts").Range("A" & ContractCurrentRow) = .Range("T" & TrackerCurrentRow).Offset(0, 1).Value
Worksheets("Contracts").Range("B" & ContractCurrentRow) = .Range("T" & TrackerCurrentRow).Offset(0, 2).Value
ContractCurrentRow = ContractCurrentRow + 1
End If
TrackerCurrentRow = TrackerCurrentRow + 1
Loop
End With
End Sub
I have managed to solve it, i did change to integer and removed some unnecessary incrementing. Think i was a bit confused at first. Thanks for the help!
Sub ForEach_Loop()
Dim TrackerLastRow As Long
Dim TrackerCurrentRow As Long
Dim ContractLastRow As Long
Dim ContractCurrentRow As Long
TrackerLastRow = 2486
TrackerCurrentRow = 3
ContractCurrentRow = 1
Do While TrackerCurrentRow <= TrackerLastRow
If Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Value = "Yes" Then
Worksheets("Contracts").Range("A" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 1).Value
Worksheets("Contracts").Range("B" & ContractCurrentRow) = Worksheets("Customer Tracker - OC").Range("T" & TrackerCurrentRow).Offset(, 2).Value
TrackerCurrentRow = TrackerCurrentRow + 1
ContractCurrentRow = ContractCurrentRow + 1
Else
TrackerCurrentRow = TrackerCurrentRow + 1
End If
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.