I have the following code, but I'm not getting the correct results. In a list on Sheet1, I have one column that shows month/year (column P). In cell A1 of the same sheet, there is a month/year value. If any data in column P equals the same month/year, I'd like the entire row of data in the list copied to Sheet2 in a certain position, let's say starting at row 187. My problem is that with this code, when I run it I only get the first two rows in my list copied to the other sheet. Anyone know what I'm doing wrong?
Sub CopyData()
Dim rngRow As Range, rngCol As Range, rngX As Range, ws As Worksheet, cell As CellFormat
Set ws = Worksheets("Sheet2"
lRow = 187
With Worksheets("Sheet1"
Set rngX = Range(.Cells(11, 16), .Cells(11, 16).End(xlDown))
Set rngRow = Range(.Cells(10, 1), .Cells(10, 1).End(xlDown))
Set rngCol = Range(.Cells(10, 1), .Cells(10, 1).End(xlToRight))
For Each r In rngX
If r.Value = Range("a1"
.Value Then
For Each c In rngCol
ws.Cells(lRow, c.Column).Value = .Cells(r.Row, c.Column).Value
Next
lRow = lRow + 1
End If
Next
End With
End Sub
Sub CopyData()
Dim rngRow As Range, rngCol As Range, rngX As Range, ws As Worksheet, cell As CellFormat
Set ws = Worksheets("Sheet2"
lRow = 187
With Worksheets("Sheet1"
Set rngX = Range(.Cells(11, 16), .Cells(11, 16).End(xlDown))
Set rngRow = Range(.Cells(10, 1), .Cells(10, 1).End(xlDown))
Set rngCol = Range(.Cells(10, 1), .Cells(10, 1).End(xlToRight))
For Each r In rngX
If r.Value = Range("a1"
For Each c In rngCol
ws.Cells(lRow, c.Column).Value = .Cells(r.Row, c.Column).Value
Next
lRow = lRow + 1
End If
Next
End With
End Sub