Advertisement
ProgNeo

Untitled

Apr 4th, 2023 (edited)
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub SummarizeRecipes()
  2.     Dim recipeSheet As Worksheet
  3.     Dim infoSheet As Worksheet
  4.     Dim recipeRow As Long
  5.     Dim infoRow As Long
  6.     Dim dishName As String
  7.     Dim dishCalories As Double
  8.     Dim dishPrice As Double
  9.     Dim ingredientCalories As Double
  10.     Dim ingredientPrice As Double
  11.     Dim ingredientQty As Double
  12.    
  13.     ' Установить ссылки на листы
  14.    Set recipeSheet = ThisWorkbook.Sheets("О рецептуре блюда")
  15.     Set infoSheet = ThisWorkbook.Sheets("Сведения")
  16.     infoRow = 2
  17.     recipeRow = 2
  18.    
  19.     ' Пройти по каждой строке с рецептом
  20.    Do Until IsEmpty(recipeSheet.Cells(recipeRow, 2).Value)
  21.         ' Получить название блюда из столбца 1
  22.        dishName = recipeSheet.Cells(recipeRow, 1).Value
  23.        
  24.         ' Получить калории и цену блюда из столбцов 4 и 5
  25.        dishCalories = 0
  26.         dishPrice = 0
  27.         If Not IsEmpty(recipeSheet.Cells(recipeRow, 4).Value) Then
  28.             dishCalories = recipeSheet.Cells(recipeRow, 4).Value * recipeSheet.Cells(recipeRow, 3).Value
  29.         End If
  30.         If Not IsEmpty(recipeSheet.Cells(recipeRow, 5).Value) Then
  31.             dishPrice = recipeSheet.Cells(recipeRow, 5).Value * recipeSheet.Cells(recipeRow, 3).Value
  32.         End If
  33.        
  34.         recipeRow = recipeRow + 1
  35.         ' Пройти по каждой строке с ингредиентом для этого блюда
  36.        Do Until Len(Trim(recipeSheet.Cells(recipeRow, 1).Value)) = 0 <> True Or IsEmpty(recipeSheet.Cells(recipeRow, 2).Value)
  37.             ' Получить калории, цену и количество ингредиента
  38.            ingredientCalories = recipeSheet.Cells(recipeRow, 4).Value
  39.             ingredientPrice = recipeSheet.Cells(recipeRow, 5).Value
  40.             ingredientQty = recipeSheet.Cells(recipeRow, 3).Value
  41.            
  42.             ' Умножить калории и цену на количество ингредиента
  43.            dishCalories = dishCalories + ingredientCalories * ingredientQty
  44.             dishPrice = dishPrice + ingredientPrice * ingredientQty
  45.            
  46.             ' Перейти к следующей строке с ингредиентом
  47.            recipeRow = recipeRow + 1
  48.         Loop
  49.        
  50.         ' Записать сумму калорий и цены в первый лист
  51.        infoSheet.Cells(infoRow, 1).Value = dishName
  52.         infoSheet.Cells(infoRow, 2).Value = dishCalories
  53.         infoSheet.Cells(infoRow, 3).Value = dishPrice
  54.        
  55.         infoRow = infoRow + 1
  56.     Loop
  57. End Sub
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement