Reputation: 11
I am trying to write a data from one tab to another.
I get an error on ws2.target2.Offset(m + row_num, l).Value = temp
.
Sub data_collection()
Dim ws As Worksheet
Dim tabNames(7) As String
tabNames(0) = "a"
tabNames(1) = "b"
tabNames(2) = "c"
tabNames(3) = "d"
tabNames(4) = "e"
tabNames(5) = "f"
tabNames(6) = "g"
tabNames(7) = "h"
For i = 0 To 7
Set ws = Sheets(tabNames(i))
Set target = ws.Range("B1")
Do Until target.Offset(j, 0).Value = "Total:"
If target.Offset(j, 0).Value = "1Req Detail" Then
Set ws2 = Sheets("REQ_DETAILS")
Set target2 = ws2.Range("A1")
Do Until target.Offset(j, 0).Value = "Total Open Reqs:"
If target.Offset(j, 0).Value <> "" Then
For l = 0 To 9
temp = target.Offset(j, l).Value
ws2.Select
ws2.target2.Select
ws2.target2.Offset(m + row_num, l).Value = temp
Next
row_num = row_num + 1
End If
k = k + 1
Loop
End If
j = j + 1
Loop
j = j
Next
End sub
Upvotes: 0
Views: 77
Reputation: 16204
target2
is a range so remove ws2
from ws2.target2.Select
and ws2.target2.Offset(m + row_num, l).Value
. Also with the second Do loop k = k + 1
should probably be j = j + 1
otherwise the loop never exits. Move the target and you don't need j
. ws2
and target2
don't change so move outside the loop.
Option Explicit
Sub data_collection()
Dim ws As Worksheet, ws2 As Worksheet
Dim target As Range, target2 As Range
Dim i As Long, c As Integer, row_num As Long, iLastRow As Long
Dim tabNames
tabNames = Array("a", "b", "c", "d", "e", "f", "g", "h")
Set ws2 = Sheets("REQ_DETAILS")
Set target2 = ws2.Range("A1")
row_num = 0
For i = 0 To UBound(tabNames)
Set ws = Sheets(tabNames(i))
Set target = ws.Range("B1")
iLastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
Do Until target.Value = "Total:" Or target.Row > iLastRow
If target.Value = "1Req Detail" Then
Do Until target.Value = "Total Open Reqs:" Or target.Row > iLastRow
If Len(target.Value) > 0 Then
'copy row
For c = 0 To 9
target2.Offset(row_num, c).Value = target.Offset(0, c).Value
Next
row_num = row_num + 1
End If
Set target = target.Offset(1, 0)
Loop
End If
Set target = target.Offset(1, 0)
Loop
Next
MsgBox "Done"
End Sub
Upvotes: 1
Reputation: 416
Set variables m, row_num and j starting values. Before vars usage write m=0: j=0: row_num=0 or other values.
Upvotes: 0