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

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

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(&quot;G6&quot;).Value
    kolicineKomora = Range(&quot;G7:G11&quot;).Value  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;run-time 13 type mismatch errorrr
    rezervoar1 = Range(&quot;E2&quot;).Value
    rezervoar2 = Range(&quot;E3&quot;).Value
    postotak = Range(&quot;A1&quot;).Value
    
    razlika = (rezervoar1 - rezervoar2) * (1 + postotak)
    
    For i = 1 To 5
        If kolicinaCisterne &gt; 0 Then
            If kolicineKomora(i, 1) &lt;= razlika Then
                rezervoar1 = rezervoar1 + kolicineKomora(i, 1)
                razlika = razlika - kolicineKomora(i, 1)
                Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
            ElseIf kolicineKomora(i, 1) &lt;= kolicinaCisterne Then
                rezervoar1 = rezervoar1 + kolicinaCisterne
                razlika = razlika - kolicinaCisterne
                Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
            Else
                rezervoar1 = rezervoar1 + razlika
                razlika = 0
                Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;R-1&quot;
            End If
            kolicinaCisterne = kolicinaCisterne - kolicineKomora(i, 1)
        Else
            Range(&quot;e7&quot;).Offset(i, 0).Value = &quot;&quot;
        End If
    Next i
    
    Range(&quot;F2&quot;).Value = rezervoar1 - Range(&quot;E2&quot;).Value
    Range(&quot;F3&quot;).Value = rezervoar2 - Range(&quot;E3&quot;).Value
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

以下是代码部分的翻译:

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.

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

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

Option Explicit
Sub CalculateCompartmentsPerReservoir()
&#39; s - Source
Const SRC_COMPARTMENTS As String = &quot;G7:G11&quot;
Const SRC_RESERVOIRS As String = &quot;E2:E3&quot;
Const SRC_PERCENTAGE As String = &quot;A1&quot;
&#39; d - Destination (Result)
Const DST_COMPARTMENTS_COLUMN As String = &quot;F&quot;
Const DST_RESERVOIRS_COLUMN As String = &quot;F&quot;
Const DST_ABBREVIATION_1 As String = &quot;R-1&quot;
Const DST_ABBREVIATION_2 As String = &quot;R-2&quot;
Const DST_EMPTY_RESERVOIR As String = &quot;Empty&quot;
Dim ws As Worksheet: Set ws = ActiveSheet &#39; improve!
&#39; 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
&#39; 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
&#39; 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
&#39; 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)
&#39; 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) &lt; 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
&#39; Loop 2
&#39; The fuel in the 1st compartments will be used to fill the 1st reservoir.
&#39; Switch the expressions in the inner If statement
&#39; 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) &gt; 0 Then
dcData(n, 1) = DST_ABBREVIATION_1
drData(1, 1) = drData(1, 1) - 1
ElseIf drData(2, 1) &gt; 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
&#39; Write to the worksheet.
drrg.Value = srData
dcrg.Value = dcData
&#39; Inform.
MsgBox &quot;Compartments per reservoir calculated.&quot;, 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  &#39;for calculating items in array
array_index = 0
For i = 7 To 11
ReDim Preserve kolicineKomora(array_index)
kolicineKomora(array_index) = Cells(i, &quot;G&quot;)
array_index = array_index + 1
Next i
&#39;print the items in kolicineKomora()
For i = LBound(kolicineKomora) To UBound(kolicineKomora)
Debug.Print i; kolicineKomora(i)
Next i
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:

确定