Hi,
As AD is column 30, and AF is column 32 you shall in Sub Sel_Second_Right_Y
make your loop:
For i = 32 To 137 Step 9 ' not 30 To 135 Step 9
then corrected code in Sub copy_selected_from_column_2 would be:
table(j) = CInt(Sheets("R-2").Cells(2 + i, input_col - 4))
note: inputcol - 4 because inputcol is 32 (AF) while numbers are from AB (AF-4) and header shall be from AE (so inputcol-1):
Sheets("T-2").Range("I" & output_row) = "R-2 " & Sheets("R-2").Cells(2, input_col - 1)
of course for third similar (in Sub Sel_Third_Right_Y):
then in copy_selected_from_column_3:
table(j) = CInt(Sheets("R-2").Cells(2 + i, input_col - 6))
and exactly the same as above:
Sheets("T-2").Range("I" & output_row) = "R-2 " & Sheets("R-2").Cells(2, input_col - 1)
Probably I do not have to write it, that for fourth
and
table(j) = CInt(Sheets("R-2").Cells(2 + i, input_col - 8))
would do the job.
This approach is implemented in first attached file. But as we do all the time the same (side comment - would be even easier if columns AB and AC, AK and AL etc were in oposite sequence, we could skip all that play with offset_to_header and where_header variables), it is wise to do all actions within another loop. For instance:
Sub Sel_All_Four_Right_Y()
Dim i As Long, j As Integer, where_header As Integer
For j = 1 To 4
If j = 1 Then
where_header = 2
Else
where_header = 1
End If
For i = 28 To 133 Step 9
Call copy_selected_from_column_X(i + 2 * j, 2 * j, where_header)
Next i
Next j
End Sub
Sub copy_selected_from_column_X(input_col As Long, offset_to_numbers As Integer, offset_to_header As Integer)
Dim i As Long, j As Long, output_row As Long, output_string As String, table(1 To 5) As Integer
If Application.WorksheetFunction.CountIf(Sheets("R-2").Cells(3, input_col).Resize(36, 1), "Y") <> 5 Then
MsgBox "Please select EXACTLY five Y's in " & Cells(3, input_col).Resize(36, 1).Address
Else
j = 1
For i = 1 To 36 'Start Column 1 to 108 Correct for my new release Tab R-2
If Sheets("R-2").Cells(2 + i, input_col) = "Y" Then
table(j) = CInt(Sheets("R-2").Cells(2 + i, input_col - offset_to_numbers))
j = j + 1
End If
Next i
output_string = "" 'added only for clarity - this string is empty anyway
For j = 1 To 5
output_string = output_string & Format(Application.WorksheetFunction.Small(table, j), "00") & ","
Next j
output_string = Left(output_string, Len(output_string) - 1)
output_row = Sheets("T-2").Range("I3").End(xlDown).Row + 1
Sheets("T-2").Range("I" & output_row) = "R-2 " & Sheets("R-2").Cells(2, input_col - offset_to_header)
Sheets("T-2").Range("J" & output_row) = output_string
End If
End Sub
This approach is used in second file. Results are the same, but this second one is much more compact and easier to correct if for some reason input data layout changes somewhat.
Bookmarks