Code:
Sub Breaches()
Dim shd, lrow, nrow As Long
Dim sh As Worksheet
shd = 1
lrow = Sheets("breaches").Range("A" & Rows.Count).End(xlUp).Row
nrow = Range("A" & Rows.Count).End(xlUp).Row
For Each sh In Sheets
If sh.Name = "DifferenceData" Or sh.Name = "Breaches" Then
'do nothing
Else
Sheets("DifferenceData").Cells(1, shd).Value = sh.Name
For i = 2 To lrow
If sh.Cells(i, 6).Value = "Y" Then
sh.Cells(i, 9).Copy
ActiveCell = Sheets("breaches").Cells(lrow, shd)
ActiveCell.PasteSpecial
nrow = nrow + 1
End If
Next i
shd = shd + 1
End If
Next sh
End Sub
Basically I have a number of worksheets in my workbook. All worksheets are formatted the same. Column 6 is a Y/N field identifying if a breach has occured. Column 9 is the value fo the breach.
I am attempting to get a macro that will roll through the sheets, determine if the is a breach ("Y) and if so copy the relevent data item, in cell (i,9) and copy the value to a new sheet (breaches) in the correct column and cell.
(there is also a line first to name the cells in the first row after the sheet name)
The destination cell is set by the lrow variable, the first empty cell in the column, witht he column number chosen dependant on the iteration of the sheet with shd. i.e. for the first sheet in the loop it will be 1, then 2, then 3 etc. etc.
I think it is close to working, but I am starting to pull my hair out. Any input will be greatly appreciated.
Cheers