lundi 9 février 2015

if then statement, VBA, loop

Here is an example of what I am trying to accomplish is Excel.


Food Group / Food / Apple / Banana / Carrot / Broccoli


I am writing an If Then Statement to try and match Foods with their Groups.


(I know Access would be easier, but I don't want to have to keep copying and pasting from Excel to Access and back. It will only be 6 different items)


The If Then coding I made worked for just doing the first cell. But my loop is not working. Please help





Sub InsertFoodGroup()

Dim food As String, group As String
Dim i As Integer

i = Cells(2, 2).End(xlDown).Row

scop = Range(Cells(2, 2), Cells(i, 2)).Value

Do While Cells(i, 2).Value <> ""
If food = "Apple" Then
group = "Fruit"
ElseIf food = "Banana" Then
group = "Fruit"
ElseIf food = "Carrot" Then
group = "Vegetable"
ElseIf food = "Broccoli" Then
group = "Vegetable"
Else
total = "false"
End If
i = i + 1
Loop

Range(Cells(2, 21), Cells(i, 21)).Value = total

End Sub



Aucun commentaire:

Enregistrer un commentaire