Excel公式还是VBA?分配给定量的燃料?

huangapple go评论92阅读模式
英文:

Excel formula or vba? distribution of given amounts of fuel?

问题

I understand your request for code translation. Here is the translated code:

  1. 过程 RasponKomora()
  2. kolicinaCisterne
  3. kolicineKomora()
  4. rezervoar1
  5. rezervoar2
  6. razlika
  7. postotak
  8. kolicinaCisterne = 范围("G6").值
  9. kolicineKomora = 范围("G7:G11").值
  10. rezervoar1 = 范围("E2").值
  11. rezervoar2 = 范围("E3").值
  12. postotak = 范围("A1").值
  13. razlika = (rezervoar1 - rezervoar2) * (1 + postotak)
  14. 对于 i = 1 5
  15. 如果 kolicinaCisterne > 0 Then
  16. 如果 kolicineKomora(i, 1) <= razlika Then
  17. rezervoar1 = rezervoar1 + kolicineKomora(i, 1)
  18. razlika = razlika - kolicineKomora(i, 1)
  19. 范围("e7").偏移(i, 0).值 = "R-1"
  20. ElseIf kolicineKomora(i, 1) <= kolicinaCisterne Then
  21. rezervoar1 = rezervoar1 + kolicinaCisterne
  22. razlika = razlika - kolicinaCisterne
  23. 范围("e7").偏移(i, 0).值 = "R-1"
  24. Else
  25. rezervoar1 = rezervoar1 + razlika
  26. razlika = 0
  27. 范围("e7").偏移(i, 0).值 = "R-1"
  28. End If
  29. kolicinaCisterne = kolicinaCisterne - kolicineKomora(i, 1)
  30. Else
  31. 范围("e7").偏移(i, 0).值 = ""
  32. End If
  33. 下一个 i
  34. 范围("F2").值 = rezervoar1 - 范围("E2").值
  35. 范围("F3").值 = rezervoar2 - 范围("E3").值
  36. 结束

If you have any further requests or questions, please let me know.

英文:

my English isn't good enough.

I have TWO reservoires R-1&R-2 of fuel. I need to have AROUND 10% more in R-1 after delivery. In cell A1 is 0,1 as 10%. Truck/Lorry/tanker have 30,000 of fuel with 5 section in which is 6,000 litres each. Only ONE section can be dropped in ONE reservoire not in both! In cells G7:G11 are amount of each section of tanker. In cells E2:E3 are current amount of fuel in reservoires before delivery. In cells E7:E11 I need formula to give me result as "R-1" or "R-2" for each section of tanker. I repeat, only ONE section can be delivered into one not in both reservoires. Cannot split 6,000. In F2:F3 need total amount of delivered plus amount before delivery, it must be R-1 > R-2 for AROUND cell A1 value. It is EASY when there E2:E3 are 0, reservoires was empty before delivery, then we would drop section 1,2 and 3(18,000 total) to R-1 and sections 4 and 5(12,000 total) to R-2 reservoire and total amount of reservoires would be R-1 AROUND 10% > than R-2. There can be sections of tanker with empty sections, eg. 24,000 litres, sections 1-4 are 6,000 litres and s5 is empty, Or other way around. Thanky you very much for your time and interest!!

  1. Sub RasponKomora()
  2. Dim kolicinaCisterne As Double
  3. Dim kolicineKomora() As Double
  4. Dim rezervoar1 As Double
  5. Dim rezervoar2 As Double
  6. Dim razlika As Double
  7. Dim postotak As Double
  8. kolicinaCisterne = Range(&quot;G6&quot;).Value
  9. kolicineKomora = Range(&quot;G7:G11&quot;).Value &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;run-time 13 type mismatch errorrr
  10. rezervoar1 = Range(&quot;E2&quot;).Value
  11. rezervoar2 = Range(&quot;E3&quot;).Value
  12. postotak = Range(&quot;A1&quot;).Value
  13. razlika = (rezervoar1 - rezervoar2) * (1 + postotak)
  14. For i = 1 To 5
  15. If kolicinaCisterne &gt; 0 Then
  16. If kolicineKomora(i, 1) &lt;= razlika Then
  17. rezervoar1 = rezervoar1 + kolicineKomora(i, 1)
  18. razlika = razlika - kolicineKomora(i, 1)
  19. Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
  20. ElseIf kolicineKomora(i, 1) &lt;= kolicinaCisterne Then
  21. rezervoar1 = rezervoar1 + kolicinaCisterne
  22. razlika = razlika - kolicinaCisterne
  23. Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
  24. Else
  25. rezervoar1 = rezervoar1 + razlika
  26. razlika = 0
  27. Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
  28. End If
  29. kolicinaCisterne = kolicinaCisterne - kolicineKomora(i, 1)
  30. Else
  31. Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;&quot;
  32. End If
  33. Next i
  34. Range(&quot;F2&quot;).Value = rezervoar1 - Range(&quot;E2&quot;).Value
  35. Range(&quot;F3&quot;).Value = rezervoar2 - Range(&quot;E3&quot;).Value
  36. End Sub

Tried ChatGPT tried numerous excel forums...
I just need to have more fuel in R-1 Excel公式还是VBA?分配给定量的燃料?

Hello,

Based on your description, you need a formula to distribute the sections of the tanker between two reservoirs (R-1 and R-2) in such a way that the total amount of fuel in R-1 is approximately 10% more than in R-2, considering the current amounts in cells E2:E3.

You also need a formula in cells E7:E11 to determine which reservoir each section should be delivered to, ensuring that each section is delivered to only one reservoir.

Additionally, you need the total amounts of fuel in R-1 and R-2, including the amounts before delivery, in cells F2:F3.

I understand that the sections cannot be split, and only one section can be delivered to each reservoir.

Please note that the formula for achieving an exact 10% difference may not be feasible in all cases due to the constraints of whole sections and the need to deliver them to only one reservoir. However, we can create a formula that will distribute the sections to achieve an approximate 10% difference.

I hope this clarifies the context of your question. If you have any specific queries or if there's anything else you'd like to add, please let me know.

Regenerate response

答案1

得分: 1

以下是代码部分的翻译:

  1. Option Explicit
  2. Sub CalculateCompartmentsPerReservoir()
  3. ' s - 源
  4. Const SRC_COMPARTMENTS As String = "G7:G11"
  5. Const SRC_RESERVOIRS As String = "E2:E3"
  6. Const SRC_PERCENTAGE As String = "A1"
  7. ' d - 目标 (结果)
  8. Const DST_COMPARTMENTS_COLUMN As String = "F"
  9. Const DST_RESERVOIRS_COLUMN As String = "F"
  10. Const DST_ABBREVIATION_1 As String = "R-1"
  11. Const DST_ABBREVIATION_2 As String = "R-2"
  12. Const DST_EMPTY_RESERVOIR As String = "空"
  13. Dim ws As Worksheet: Set ws = ActiveSheet ' 改进!
  14. ' c - 隔间 (Komore)
  15. Dim dcrg As Range, scData(), CompartmentsCount As Long
  16. With ws.Range(SRC_COMPARTMENTS)
  17. CompartmentsCount = .Rows.Count
  18. scData = .Value
  19. Set dcrg = .EntireRow.Columns(DST_COMPARTMENTS_COLUMN)
  20. End With
  21. ' r - 水库 (Rezervoari)
  22. Dim drrg As Range, srData(), ReservoirsCount As Long
  23. With ws.Range(SRC_RESERVOIRS)
  24. ReservoirsCount = .Rows.Count
  25. srData = .Value
  26. Set drrg = .EntireRow.Columns(DST_RESERVOIRS_COLUMN)
  27. End With
  28. ' 比例 (Omjer)
  29. Dim Ratio As Double: Ratio = 1
  30. With ws.Range(SRC_PERCENTAGE)
  31. If VarType(.Value) = vbDouble Then
  32. Ratio = Ratio + .Value
  33. End If
  34. End With
  35. ' 目标数组
  36. Dim drData() As Long: ReDim drData(1 To ReservoirsCount, 1 To 1)
  37. Dim dcData() As String: ReDim dcData(1 To CompartmentsCount, 1 To 1)
  38. ' 循环 1
  39. Dim CompartmentFuel, n As Long, WouldBeReservoir2Fuel As Long
  40. For n = 1 To CompartmentsCount
  41. CompartmentFuel = scData(n, 1)
  42. If VarType(CompartmentFuel) = vbDouble Then
  43. WouldBeReservoir2Fuel = srData(2, 1) + CompartmentFuel
  44. If srData(1, 1) < WouldBeReservoir2Fuel * Ratio Then
  45. srData(1, 1) = srData(1, 1) + CompartmentFuel
  46. drData(1, 1) = drData(1, 1) + 1
  47. Else
  48. srData(2, 1) = WouldBeReservoir2Fuel
  49. drData(2, 1) = drData(2, 1) + 1
  50. End If
  51. Else
  52. scData(n, 1) = 0
  53. dcData(n, 1) = DST_EMPTY_RESERVOIR
  54. End If
  55. Debug.Print n, srData(1, 1), srData(2, 1), WouldBeReservoir2Fuel
  56. Next n
  57. ' 循环 2
  58. ' 第一个隔间中的燃料将用于填充第一个水库。
  59. ' 在内部 If 语句中切换表达式
  60. ' 以使用第一个隔间的燃料填充第二个水库。
  61. For n = 1 To CompartmentsCount
  62. If Len(dcData(n, 1)) = 0 Then
  63. If drData(1, 1) > 0 Then
  64. dcData(n, 1) = DST_ABBREVIATION_1
  65. drData(1, 1) = drData(1, 1) - 1
  66. ElseIf drData(2, 1) > 0 Then
  67. dcData(n, 1) = DST_ABBREVIATION_2
  68. drData(2, 1) = drData(2, 1) - 1
  69. End If
  70. End If
  71. Debug.Print n, drData(1, 1), drData(2, 1), dcData(n, 1)
  72. Next n
  73. ' 写入工作表。
  74. drrg.Value = srData
  75. dcrg.Value = dcData
  76. ' 通知。
  77. MsgBox "每个水库的隔间已计算。", vbInformation
  78. End Sub

请注意,我已经将一些代码中的注释翻译成中文。如果需要更多帮助,请随时提问。

英文:

Distribution of Fuel to Two Reservoirs

  • This doesn't account for more fuel in the tanker than the reservoirs can hold indicated by Tim Williams in the comments.
  • Also, this uses the following condition:
    In reservoir 1 there must be at least 10% more fuel than in reservoir 2.
    Keep in mind that this cannot be achieved if e.g. reservoir 1 is empty and reservoir 2 has 30,000 liters.

Excel公式还是VBA?分配给定量的燃料?

<!-- language: lang-vb -->

  1. Option Explicit
  2. Sub CalculateCompartmentsPerReservoir()
  3. &#39; s - Source
  4. Const SRC_COMPARTMENTS As String = &quot;G7:G11&quot;
  5. Const SRC_RESERVOIRS As String = &quot;E2:E3&quot;
  6. Const SRC_PERCENTAGE As String = &quot;A1&quot;
  7. &#39; d - Destination (Result)
  8. Const DST_COMPARTMENTS_COLUMN As String = &quot;F&quot;
  9. Const DST_RESERVOIRS_COLUMN As String = &quot;F&quot;
  10. Const DST_ABBREVIATION_1 As String = &quot;R-1&quot;
  11. Const DST_ABBREVIATION_2 As String = &quot;R-2&quot;
  12. Const DST_EMPTY_RESERVOIR As String = &quot;Empty&quot;
  13. Dim ws As Worksheet: Set ws = ActiveSheet &#39; improve!
  14. &#39; c - Compartments (Komore)
  15. Dim dcrg As Range, scData(), CompartmentsCount As Long
  16. With ws.Range(SRC_COMPARTMENTS)
  17. CompartmentsCount = .Rows.Count
  18. scData = .Value
  19. Set dcrg = .EntireRow.Columns(DST_COMPARTMENTS_COLUMN)
  20. End With
  21. &#39; r - Reservoirs (Rezervoari)
  22. Dim drrg As Range, srData(), ReservoirsCount As Long
  23. With ws.Range(SRC_RESERVOIRS)
  24. ReservoirsCount = .Rows.Count
  25. srData = .Value
  26. Set drrg = .EntireRow.Columns(DST_RESERVOIRS_COLUMN)
  27. End With
  28. &#39; Ratio (Omjer)
  29. Dim Ratio As Double: Ratio = 1
  30. With ws.Range(SRC_PERCENTAGE)
  31. If VarType(.Value) = vbDouble Then
  32. Ratio = Ratio + .Value
  33. End If
  34. End With
  35. &#39; Destination Arrays
  36. Dim drData() As Long: ReDim drData(1 To ReservoirsCount, 1 To 1)
  37. Dim dcData() As String: ReDim dcData(1 To CompartmentsCount, 1 To 1)
  38. &#39; Loop 1
  39. Dim CompartmentFuel, n As Long, WouldBeReservoir2Fuel As Long
  40. For n = 1 To CompartmentsCount
  41. CompartmentFuel = scData(n, 1)
  42. If VarType(CompartmentFuel) = vbDouble Then
  43. WouldBeReservoir2Fuel = srData(2, 1) + CompartmentFuel
  44. If srData(1, 1) &lt; WouldBeReservoir2Fuel * Ratio Then
  45. srData(1, 1) = srData(1, 1) + CompartmentFuel
  46. drData(1, 1) = drData(1, 1) + 1
  47. Else
  48. srData(2, 1) = WouldBeReservoir2Fuel
  49. drData(2, 1) = drData(2, 1) + 1
  50. End If
  51. Else
  52. scData(n, 1) = 0
  53. dcData(n, 1) = DST_EMPTY_RESERVOIR
  54. End If
  55. Debug.Print n, srData(1, 1), srData(2, 1), WouldBeReservoir2Fuel
  56. Next n
  57. &#39; Loop 2
  58. &#39; The fuel in the 1st compartments will be used to fill the 1st reservoir.
  59. &#39; Switch the expressions in the inner If statement
  60. &#39; to fill the 2nd reservoir with the fuel of the 1st compartments.
  61. For n = 1 To CompartmentsCount
  62. If Len(dcData(n, 1)) = 0 Then
  63. If drData(1, 1) &gt; 0 Then
  64. dcData(n, 1) = DST_ABBREVIATION_1
  65. drData(1, 1) = drData(1, 1) - 1
  66. ElseIf drData(2, 1) &gt; 0 Then
  67. dcData(n, 1) = DST_ABBREVIATION_2
  68. drData(2, 1) = drData(2, 1) - 1
  69. End If
  70. End If
  71. Debug.Print n, drData(1, 1), drData(2, 1), dcData(n, 1)
  72. Next n
  73. &#39; Write to the worksheet.
  74. drrg.Value = srData
  75. dcrg.Value = dcData
  76. &#39; Inform.
  77. MsgBox &quot;Compartments per reservoir calculated.&quot;, vbInformation
  78. End Sub

答案2

得分: 0

  1. Dim kolicineKomora As Variant
  2. kolicineKomora = Range("G7:G11").Value
英文:
  1. Dim kolicineKomora() As Double

change this to:<code>Dim kolicineKomora As variant</code>

then <code>kolicineKomora = Range("G7:G11").Value</code> will have no error.

Otherwise if you prefer to use array,try this:

  1. Dim kolicineKomora() As Double
  2. Dim i As Long
  3. Dim array_index As Long &#39;for calculating items in array
  4. array_index = 0
  5. For i = 7 To 11
  6. ReDim Preserve kolicineKomora(array_index)
  7. kolicineKomora(array_index) = Cells(i, &quot;G&quot;)
  8. array_index = array_index + 1
  9. Next i
  10. &#39;print the items in kolicineKomora()
  11. For i = LBound(kolicineKomora) To UBound(kolicineKomora)
  12. Debug.Print i; kolicineKomora(i)
  13. Next i
  14. Erase kolicineKomora

huangapple
  • 本文由 发表于 2023年6月29日 06:06:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定