英文:
Excel formula or vba? distribution of given amounts of fuel?
问题
I understand your request for code translation. Here is the translated code:
子 过程 RasponKomora()
双 kolicinaCisterne
双 kolicineKomora()
双 rezervoar1
双 rezervoar2
双 razlika
双 postotak
kolicinaCisterne = 范围("G6").值
kolicineKomora = 范围("G7:G11").值
rezervoar1 = 范围("E2").值
rezervoar2 = 范围("E3").值
postotak = 范围("A1").值
razlika = (rezervoar1 - rezervoar2) * (1 + postotak)
对于 i = 1 到 5
如果 kolicinaCisterne > 0 Then
如果 kolicineKomora(i, 1) <= razlika Then
rezervoar1 = rezervoar1 + kolicineKomora(i, 1)
razlika = razlika - kolicineKomora(i, 1)
范围("e7").偏移(i, 0).值 = "R-1"
ElseIf kolicineKomora(i, 1) <= kolicinaCisterne Then
rezervoar1 = rezervoar1 + kolicinaCisterne
razlika = razlika - kolicinaCisterne
范围("e7").偏移(i, 0).值 = "R-1"
Else
rezervoar1 = rezervoar1 + razlika
razlika = 0
范围("e7").偏移(i, 0).值 = "R-1"
End If
kolicinaCisterne = kolicinaCisterne - kolicineKomora(i, 1)
Else
范围("e7").偏移(i, 0).值 = ""
End If
下一个 i
范围("F2").值 = rezervoar1 - 范围("E2").值
范围("F3").值 = rezervoar2 - 范围("E3").值
结束 子
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!!
Sub RasponKomora()
Dim kolicinaCisterne As Double
Dim kolicineKomora() As Double
Dim rezervoar1 As Double
Dim rezervoar2 As Double
Dim razlika As Double
Dim postotak As Double
kolicinaCisterne = Range("G6").Value
kolicineKomora = Range("G7:G11").Value <<<<<<<<<run-time 13 type mismatch errorrr
rezervoar1 = Range("E2").Value
rezervoar2 = Range("E3").Value
postotak = Range("A1").Value
razlika = (rezervoar1 - rezervoar2) * (1 + postotak)
For i = 1 To 5
If kolicinaCisterne > 0 Then
If kolicineKomora(i, 1) <= razlika Then
rezervoar1 = rezervoar1 + kolicineKomora(i, 1)
razlika = razlika - kolicineKomora(i, 1)
Range("e7").Offset(i, 0).Value = "R-1"
ElseIf kolicineKomora(i, 1) <= kolicinaCisterne Then
rezervoar1 = rezervoar1 + kolicinaCisterne
razlika = razlika - kolicinaCisterne
Range("e7").Offset(i, 0).Value = "R-1"
Else
rezervoar1 = rezervoar1 + razlika
razlika = 0
Range("e7").Offset(i, 0).Value = "R-1"
End If
kolicinaCisterne = kolicinaCisterne - kolicineKomora(i, 1)
Else
Range("e7").Offset(i, 0).Value = ""
End If
Next i
Range("F2").Value = rezervoar1 - Range("E2").Value
Range("F3").Value = rezervoar2 - Range("E3").Value
End Sub
Tried ChatGPT tried numerous excel forums...
I just need to have more fuel in R-1
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
以下是代码部分的翻译:
Option Explicit
Sub CalculateCompartmentsPerReservoir()
' s - 源
Const SRC_COMPARTMENTS As String = "G7:G11"
Const SRC_RESERVOIRS As String = "E2:E3"
Const SRC_PERCENTAGE As String = "A1"
' d - 目标 (结果)
Const DST_COMPARTMENTS_COLUMN As String = "F"
Const DST_RESERVOIRS_COLUMN As String = "F"
Const DST_ABBREVIATION_1 As String = "R-1"
Const DST_ABBREVIATION_2 As String = "R-2"
Const DST_EMPTY_RESERVOIR As String = "空"
Dim ws As Worksheet: Set ws = ActiveSheet ' 改进!
' c - 隔间 (Komore)
Dim dcrg As Range, scData(), CompartmentsCount As Long
With ws.Range(SRC_COMPARTMENTS)
CompartmentsCount = .Rows.Count
scData = .Value
Set dcrg = .EntireRow.Columns(DST_COMPARTMENTS_COLUMN)
End With
' r - 水库 (Rezervoari)
Dim drrg As Range, srData(), ReservoirsCount As Long
With ws.Range(SRC_RESERVOIRS)
ReservoirsCount = .Rows.Count
srData = .Value
Set drrg = .EntireRow.Columns(DST_RESERVOIRS_COLUMN)
End With
' 比例 (Omjer)
Dim Ratio As Double: Ratio = 1
With ws.Range(SRC_PERCENTAGE)
If VarType(.Value) = vbDouble Then
Ratio = Ratio + .Value
End If
End With
' 目标数组
Dim drData() As Long: ReDim drData(1 To ReservoirsCount, 1 To 1)
Dim dcData() As String: ReDim dcData(1 To CompartmentsCount, 1 To 1)
' 循环 1
Dim CompartmentFuel, n As Long, WouldBeReservoir2Fuel As Long
For n = 1 To CompartmentsCount
CompartmentFuel = scData(n, 1)
If VarType(CompartmentFuel) = vbDouble Then
WouldBeReservoir2Fuel = srData(2, 1) + CompartmentFuel
If srData(1, 1) < WouldBeReservoir2Fuel * Ratio Then
srData(1, 1) = srData(1, 1) + CompartmentFuel
drData(1, 1) = drData(1, 1) + 1
Else
srData(2, 1) = WouldBeReservoir2Fuel
drData(2, 1) = drData(2, 1) + 1
End If
Else
scData(n, 1) = 0
dcData(n, 1) = DST_EMPTY_RESERVOIR
End If
Debug.Print n, srData(1, 1), srData(2, 1), WouldBeReservoir2Fuel
Next n
' 循环 2
' 第一个隔间中的燃料将用于填充第一个水库。
' 在内部 If 语句中切换表达式
' 以使用第一个隔间的燃料填充第二个水库。
For n = 1 To CompartmentsCount
If Len(dcData(n, 1)) = 0 Then
If drData(1, 1) > 0 Then
dcData(n, 1) = DST_ABBREVIATION_1
drData(1, 1) = drData(1, 1) - 1
ElseIf drData(2, 1) > 0 Then
dcData(n, 1) = DST_ABBREVIATION_2
drData(2, 1) = drData(2, 1) - 1
End If
End If
Debug.Print n, drData(1, 1), drData(2, 1), dcData(n, 1)
Next n
' 写入工作表。
drrg.Value = srData
dcrg.Value = dcData
' 通知。
MsgBox "每个水库的隔间已计算。", vbInformation
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.
<!-- language: lang-vb -->
Option Explicit
Sub CalculateCompartmentsPerReservoir()
' s - Source
Const SRC_COMPARTMENTS As String = "G7:G11"
Const SRC_RESERVOIRS As String = "E2:E3"
Const SRC_PERCENTAGE As String = "A1"
' d - Destination (Result)
Const DST_COMPARTMENTS_COLUMN As String = "F"
Const DST_RESERVOIRS_COLUMN As String = "F"
Const DST_ABBREVIATION_1 As String = "R-1"
Const DST_ABBREVIATION_2 As String = "R-2"
Const DST_EMPTY_RESERVOIR As String = "Empty"
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
' c - Compartments (Komore)
Dim dcrg As Range, scData(), CompartmentsCount As Long
With ws.Range(SRC_COMPARTMENTS)
CompartmentsCount = .Rows.Count
scData = .Value
Set dcrg = .EntireRow.Columns(DST_COMPARTMENTS_COLUMN)
End With
' r - Reservoirs (Rezervoari)
Dim drrg As Range, srData(), ReservoirsCount As Long
With ws.Range(SRC_RESERVOIRS)
ReservoirsCount = .Rows.Count
srData = .Value
Set drrg = .EntireRow.Columns(DST_RESERVOIRS_COLUMN)
End With
' Ratio (Omjer)
Dim Ratio As Double: Ratio = 1
With ws.Range(SRC_PERCENTAGE)
If VarType(.Value) = vbDouble Then
Ratio = Ratio + .Value
End If
End With
' Destination Arrays
Dim drData() As Long: ReDim drData(1 To ReservoirsCount, 1 To 1)
Dim dcData() As String: ReDim dcData(1 To CompartmentsCount, 1 To 1)
' Loop 1
Dim CompartmentFuel, n As Long, WouldBeReservoir2Fuel As Long
For n = 1 To CompartmentsCount
CompartmentFuel = scData(n, 1)
If VarType(CompartmentFuel) = vbDouble Then
WouldBeReservoir2Fuel = srData(2, 1) + CompartmentFuel
If srData(1, 1) < WouldBeReservoir2Fuel * Ratio Then
srData(1, 1) = srData(1, 1) + CompartmentFuel
drData(1, 1) = drData(1, 1) + 1
Else
srData(2, 1) = WouldBeReservoir2Fuel
drData(2, 1) = drData(2, 1) + 1
End If
Else
scData(n, 1) = 0
dcData(n, 1) = DST_EMPTY_RESERVOIR
End If
Debug.Print n, srData(1, 1), srData(2, 1), WouldBeReservoir2Fuel
Next n
' Loop 2
' The fuel in the 1st compartments will be used to fill the 1st reservoir.
' Switch the expressions in the inner If statement
' to fill the 2nd reservoir with the fuel of the 1st compartments.
For n = 1 To CompartmentsCount
If Len(dcData(n, 1)) = 0 Then
If drData(1, 1) > 0 Then
dcData(n, 1) = DST_ABBREVIATION_1
drData(1, 1) = drData(1, 1) - 1
ElseIf drData(2, 1) > 0 Then
dcData(n, 1) = DST_ABBREVIATION_2
drData(2, 1) = drData(2, 1) - 1
End If
End If
Debug.Print n, drData(1, 1), drData(2, 1), dcData(n, 1)
Next n
' Write to the worksheet.
drrg.Value = srData
dcrg.Value = dcData
' Inform.
MsgBox "Compartments per reservoir calculated.", vbInformation
End Sub
答案2
得分: 0
Dim kolicineKomora As Variant
kolicineKomora = Range("G7:G11").Value
英文:
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:
Dim kolicineKomora() As Double
Dim i As Long
Dim array_index As Long 'for calculating items in array
array_index = 0
For i = 7 To 11
ReDim Preserve kolicineKomora(array_index)
kolicineKomora(array_index) = Cells(i, "G")
array_index = array_index + 1
Next i
'print the items in kolicineKomora()
For i = LBound(kolicineKomora) To UBound(kolicineKomora)
Debug.Print i; kolicineKomora(i)
Next i
Erase kolicineKomora
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论