HarveySpecter
New Member
- Joined
- Jan 4, 2019
- Messages
- 4
Hello All,
I've started playing with VBA some days ago, thus I'm new with Excel coding.
Below is a simplified version of the sheet I want to extrapolate the data from (sheet name "Data"):
[TABLE="width: 332"]
<tbody>[TR]
[TD="class: xl66, width: 83"]Customer[/TD]
[TD="class: xl66, width: 83"]Platform[/TD]
[TD="class: xl66, width: 83"]Program[/TD]
[TD="class: xl66, width: 83"]Motor Type[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr5[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr5[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]dd[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]dd[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
</tbody>[/TABLE]
What I'd like to do is to filter the columns "Customer" and "Platform" based on the values of the columns A and B of the sheet "First 50 Programs" (I though of creating a loop with the autofilter function) and run an if-statement that checks all the cells among the visible cells of the column "Motor Type" of the sheet "Data" that match multiple criteria as shown below.
At the end of the macro, the result should appear on the sheet "First 50 Programs" as follows:
[TABLE="width: 337"]
<tbody>[TR]
[TD]Customer[/TD]
[TD]Platform[/TD]
[TD]Motor Category[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD]Multienergy[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]2[/TD]
[TD]Electric[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]Conventional[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]1[/TD]
[TD]Multienergy[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]Conventional[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]3[/TD]
[TD]Electric[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]4[/TD]
[TD]Conventional[/TD]
[/TR]
</tbody>[/TABLE]
This is the code I have tried to apply, but it doesn't work
Thanks a lot.
I've started playing with VBA some days ago, thus I'm new with Excel coding.
Below is a simplified version of the sheet I want to extrapolate the data from (sheet name "Data"):
[TABLE="width: 332"]
<tbody>[TR]
[TD="class: xl66, width: 83"]Customer[/TD]
[TD="class: xl66, width: 83"]Platform[/TD]
[TD="class: xl66, width: 83"]Program[/TD]
[TD="class: xl66, width: 83"]Motor Type[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr5[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr5[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr4[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr3[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]aa[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]dd[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]dd[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]ee[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr1[/TD]
[TD="class: xl65"]bb[/TD]
[/TR]
[TR]
[TD="class: xl65"]D[/TD]
[TD="class: xl65"]4[/TD]
[TD="class: xl65"]pr2[/TD]
[TD="class: xl65"]cc[/TD]
[/TR]
</tbody>[/TABLE]
What I'd like to do is to filter the columns "Customer" and "Platform" based on the values of the columns A and B of the sheet "First 50 Programs" (I though of creating a loop with the autofilter function) and run an if-statement that checks all the cells among the visible cells of the column "Motor Type" of the sheet "Data" that match multiple criteria as shown below.
- if within the visible cells of filtered range (column "Motor Type"), there is at least one cell with the text "aa" or "bb" or "cc" or "dd" AND no cells with "ee", then return in column "Motor Category" the text "Conventional"
- if within the visible cells of filtered range (column "Motor Type"), there is at least one cell with the text "aa" or "bb" or "cc" or "dd" AND also at least one cell with text "ee", then return in column "Motor Category" the text "Multienergy"
- if within the visible cells of filtered range (column "Motor Type"), there are no cells with the text "aa" or "bb" or "cc" or "dd" AND there is at least one cell with text "ee", then return on cell "Motor Category" the text "Electric"
At the end of the macro, the result should appear on the sheet "First 50 Programs" as follows:
[TABLE="width: 337"]
<tbody>[TR]
[TD]Customer[/TD]
[TD]Platform[/TD]
[TD]Motor Category[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD]Multienergy[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]2[/TD]
[TD]Electric[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]3[/TD]
[TD]Conventional[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]1[/TD]
[TD]Multienergy[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]Conventional[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]3[/TD]
[TD]Electric[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]4[/TD]
[TD]Conventional[/TD]
[/TR]
</tbody>[/TABLE]
This is the code I have tried to apply, but it doesn't work
Code:
Sub Types_Of_Platforms()
Dim i As Long
Dim lRow As Long
Dim VisCell, c As Range
lRow = Worksheets("First 50 Programs").Cells(Rows.Count, 2).End(xlUp).Row
For i = 5 To lRow
Sheets("Data").Select
With ActiveSheet.Range("$A$1:$D$34")
.AutoFilter Field:=1, Criteria1:=Sheets("First 50 Programs").Range("A" & i).Value
.AutoFilter Field:=2, Criteria1:=Sheets("First 50 Programs").Range("B" & i).Value
End With
Set VisCell = Worksheets("Data").Range("D1:D34").SpecialCells(xlCellTypeVisible)
For Each c In VisCell.Cells
If c.Value2 = "aa" Or c.Value2 = "bb" Or c.Value2 = "cc" Or c.Value2 = "dd" And c.Value2 <> "ee" Then
Sheets("First 50 Programs").Range("C" & i).Value = "Conventional"
ElseIf c.Value2 <> "aa" And c.Value2 <> "bb" And c.Value2 <> "cc" And c.Value2 <> "dd" And c.Value2 = "ee" Then
Sheets("First 50 Programs").Range("C" & i).Value = "Only BEV"
ElseIf c.Value2 = "aa" Or c.Value2 = "bb" Or c.Value2 = "cc" Or c.Value2 = "dd" And c.Value2 = "ee" Then
Sheets("First 50 Programs").Range("C" & i).Value = "Multi-Energy"
Else: Sheets("First 50 Programs").Range("C" & i).Value = "Error"
End If
Next c
Next i
End Sub
Thanks a lot.