I am trying to do a loop through the columns of two rows to add data from one sheet to another. This is additional data that was not dealt with in the previous code (since the previous code had specific logic to populate it).
I tried creating a sub to loop through the current rows on each page, but I am getting a byreference error. One of the ranges is the whole destination row, which is offset (incremented) every time it is populated. The other range is the source material, and covers a column in the source sheet. Other columns are accessed via offset. It am doing much of my work in a For/Each of the source range.
When I create the sub and try to pass the second range (bar), I get the error. I am trying to access the 'bar' object in the For/Each, so that both pages are dealing with the same row. This doesn't appear to be working.
Do I need to reDim, or find some other way to pass the appropriate range to the looping function?
Relevant code:
looping sub (very simple) -
Private Sub LoopThru38(thisRow As Range, sourceRow As Range)
Dim counter As Integer
For counter = 1 To 35
thisRow.Cells(1, 8 + counter).Value = sourceRow.Cells(1, 19 + counter)
Next counter
End Sub
Where I pass it -
ElseIf bar.Cells(1, 19) = prevComp And bar.Cells(1, 19).Value = foo.Cells(1, 2).Value Then ' compare if prev and current comp match
' add other DTCs of this component
destRange.Cells(1, 1).Value = idNumber
destRange.Cells(1, 2).NumberFormat = "@"
destRange.Cells(1, 2).Value = CStr(objectNumber + dotNumber - 0.01) & "-" & CStr(dashNumber)
destRange.Cells(1, 3).Value = "3"
destRange.Cells(1, 5).Value = bar.Cells(1, 6).Value '
destRange.Cells(1, 6).Value = bar.Cells(1, 10) ' foo.Cells(1, 3).Value & " - " & foo.Cells(1, 4)
destRange.Cells(1, 7).Value = bar.Cells(1, 11)
destRange.Cells(1, 8).Value = "FMI " & bar.Cells(1, 11) & ": " & bar.Cells(1, 13)
LoopThru38 destRange, bar ' loops through rest of 38 col to populate export sheet
Set destRange = destRange.Offset(1, 0)
idNumber = idNumber + 1
dashNumber = dashNumber + 1
End If
Original declarations of the ranges -
With ThisWorkbook
Set WS = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
Set Columns_38 = .Worksheets("Joe")
Set dtcList = .Worksheets("Fred")
Set spnList = .Worksheets("Martha")
End With
'...cont
Set srcRange = dtcList.Range(dtcList.Cells(2, "A"), dtcList.Cells(lastRowSrc, "A"))
Set destRange = WS.Range(WS.Cells(2, 1), WS.Cells(2, 42))
Set spnRange = spnList.Range(spnList.Cells(6, 1), spnList.Cells(lastRowSPN, 1))
spnRange is where I seem to be having issues. It is in the second For loop (bar), and that is where I get the source data for the output. It doesn't want to pass 'bar' into the sub though. Do I need to pass the whole range in there, and figure out where I am at?
Thanks
For Each
in your code anywhere?Set spnRange..
)?