I need help figuring out the right verbiage for the VBA Coding that I have in my excel workbook. Currently I have the entire row copying to another sheet when pressing a button if a specific cell contains the word "Fail". I am wanting to change that to the next cell over on the pages to move the entire row if containing any words no matter whether the previous cell says "Pass" or "Fail".
Here is what my coding is so far:
a = Worksheets("Extinguisher").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher").Cells(i, 10).Value = "Fail" Then
Worksheets("Extinguisher").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher").Activate
End If
So I need it to change to cell(i, 12) instead of cell(i, 10) and I would like that stated cell now to be able to copy the row like I have it doing below if it contains any value, but not copy other rows that contain no data in cell(i, 12).
Hopefully this makes sense as to what I'm looking to do.
So since posting I have found the solution in my VBA coding see below, but new problem has started. I want the VBA code to look at cells(i, 12) only after row 22 on the specified sheet. I've tried using "FirstRow22" but that makes it so the VBA code does nothing.
a = Worksheets("Extinguisher").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher").Cells(i, 12).Value > "" Then
Worksheets("Extinguisher").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher").Activate
End If
I feel like I may just be missing a line of code before this part:
If Worksheets("Extinguisher").Cells(i, 12).Value > "" Then
Please help. Thanks :)
THIS ADDITION IS TO THE COMMENTS BELOW BY FANEDURU:
Here is the entire code now with your changes. I receive a 'Run-Time Error' depending on how many rows are being copied.
Private Sub CommandButton1_Click()
'unprotect sheet Worksheets("Repairs Sheet").Unprotect Password:="JODA"
a = Worksheets("Extinguisher").Cells(Rows.Count, 1).End(xlUp).Row
For i = 21 To a
If Worksheets("Extinguisher").Cells(i, 12).Value <> "" Then
Worksheets("Extinguisher").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher").Activate
End If
Next a = Worksheets("Extinguisher pg2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher pg2").Cells(i, 12).Value <> "" Then
Worksheets("Extinguisher pg2").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher pg2").Activate
End If
Next a = Worksheets("Extinguisher pg3").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher pg3").Cells(i, 12).Value = "" Then
Worksheets("Extinguisher pg3").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher pg3").Activate
End If
Next a = Worksheets("Extinguisher pg4").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher pg4").Cells(i, 12).Value <> "" Then
Worksheets("Extinguisher pg4").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher pg4").Activate
End If
Next a = Worksheets("Extinguisher pg5").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher pg5").Cells(i, 12).Value <> "" Then
Worksheets("Extinguisher pg5").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher pg5").Activate
End If
Next a = Worksheets("Extinguisher pg 6").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Extinguisher pg 6").Cells(i, 12).Value <> "" Then
Worksheets("Extinguisher pg 6").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Extinguisher pg 6").Activate
End If
Next a = Worksheets("E-Lights").Cells(Rows.Count, 1).End(xlUp).Row
For i = 21 To a
If Worksheets("E-Lights").Cells(i, 12).Value <> "" Then
Worksheets("E-Lights").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E-Lights").Activate
End If
Next a = Worksheets("E Lights pg2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("E Lights pg2").Cells(i, 11).Value <> "" Then
Worksheets("E Lights pg2").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E Lights pg2").Activate
End If
Next a = Worksheets("E-Lights pg3").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("E-Lights pg3").Cells(i, 11).Value <> "" Then
Worksheets("E-Lights pg3").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E-Lights pg3").Activate
End If
Next a = Worksheets("E Lights pg4").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("E Lights pg4").Cells(i, 11).Value <> "" Then
Worksheets("E Lights pg4").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E Lights pg4").Activate
End If
Next a = Worksheets("E Lights pg5").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("E Lights pg5").Cells(i, 11).Value <> "" Then
Worksheets("E Lights pg5").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E Lights pg5").Activate
End If
Next a = Worksheets("E Lights pg6").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("E Lights pg6").Cells(i, 11).Value <> "" Then
Worksheets("E Lights pg6").Rows(i).Copy
Worksheets("Repairs Sheet").Activate
b = Worksheets("Repairs Sheet").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Repairs Sheet").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("E Lights pg6").Activate
End If
Worksheets("Repairs Sheet").Range("A1:N300").Locked = True
'protect the sheet back Worksheets("Repairs Sheet").Protect Password:="JODA"
Next Application.CutCopyMode = False
End Sub
Again the run time error seems to change where it comes out at based on how many rows I am asking the coding to copy to the "Repair Sheet"
If Worksheets("Extinguisher").Cells(i, 12).Value > "" Then
should be transformed inIf Worksheets("Extinguisher").Cells(i, 12).Value <> "" Then
(not equal with nothing) and starting iteration from row 22 is solved writingFor i = 22 To a
. But this reflects my initial supposition which has been considered as wrong... – FaneDuru