Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub SummarizeRecipes()
- Dim recipeSheet As Worksheet
- Dim infoSheet As Worksheet
- Dim recipeRow As Long
- Dim infoRow As Long
- Dim dishName As String
- Dim dishCalories As Double
- Dim dishPrice As Double
- Dim ingredientCalories As Double
- Dim ingredientPrice As Double
- Dim ingredientQty As Double
- ' Установить ссылки на листы
- Set recipeSheet = ThisWorkbook.Sheets("О рецептуре блюда")
- Set infoSheet = ThisWorkbook.Sheets("Сведения")
- infoRow = 2
- recipeRow = 2
- ' Пройти по каждой строке с рецептом
- Do Until IsEmpty(recipeSheet.Cells(recipeRow, 2).Value)
- ' Получить название блюда из столбца 1
- dishName = recipeSheet.Cells(recipeRow, 1).Value
- ' Получить калории и цену блюда из столбцов 4 и 5
- dishCalories = 0
- dishPrice = 0
- If Not IsEmpty(recipeSheet.Cells(recipeRow, 4).Value) Then
- dishCalories = recipeSheet.Cells(recipeRow, 4).Value * recipeSheet.Cells(recipeRow, 3).Value
- End If
- If Not IsEmpty(recipeSheet.Cells(recipeRow, 5).Value) Then
- dishPrice = recipeSheet.Cells(recipeRow, 5).Value * recipeSheet.Cells(recipeRow, 3).Value
- End If
- recipeRow = recipeRow + 1
- ' Пройти по каждой строке с ингредиентом для этого блюда
- Do Until Len(Trim(recipeSheet.Cells(recipeRow, 1).Value)) = 0 <> True Or IsEmpty(recipeSheet.Cells(recipeRow, 2).Value)
- ' Получить калории, цену и количество ингредиента
- ingredientCalories = recipeSheet.Cells(recipeRow, 4).Value
- ingredientPrice = recipeSheet.Cells(recipeRow, 5).Value
- ingredientQty = recipeSheet.Cells(recipeRow, 3).Value
- ' Умножить калории и цену на количество ингредиента
- dishCalories = dishCalories + ingredientCalories * ingredientQty
- dishPrice = dishPrice + ingredientPrice * ingredientQty
- ' Перейти к следующей строке с ингредиентом
- recipeRow = recipeRow + 1
- Loop
- ' Записать сумму калорий и цены в первый лист
- infoSheet.Cells(infoRow, 1).Value = dishName
- infoSheet.Cells(infoRow, 2).Value = dishCalories
- infoSheet.Cells(infoRow, 3).Value = dishPrice
- infoRow = infoRow + 1
- Loop
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement