首页 > 解决方案 > 宏没有按应有的方式划分 - 第 2 部分

问题描述

我在 Stack 网站上发布了一个问题来帮助编写一些代码,因为它的工作方式并不完全符合我的要求。有人友好地回复了下面的建议,但我不知道要对我的代码进行哪些更改,因为我不理解建议的更改。我想知道是否有人可以帮助我更改代码,以便我可以使用该更改。如果需要,我可以发布整个代码,如果有帮助的话。PS。在有人回答之前,您为什么不直接问提出建议的人:我做到了。

建议:修复后右括号向左移动 4 位。

Val(Left(pair, InStr(pair, "-"))) - 1 

我有一个可以工作的宏,但只有在除以 19.00 时才有效。当 20.00 或更高是要划分的数字时,它只划分为 10.00,仅此而已。它应该做的是将高达 12.00 的任何数字分成 10 个单元格,并且任何超过 12.00 的数字都应该将多余的数量写入找到的对中的第一个数字。我已经复制了我的 Excel 工作表。要划分的数字在单元格 D30 (20.00) 中。

Sub DIVIDE()
Application.ScreenUpdating = False

Dim pair As Variant, accumulator As Variant
Dim findFifteen As Double
Dim remainder As Long, found As Long

found = 1

For Each pair In Range("B30, F30, J30")
If Right(pair, 2) = 15 Then
    If pair.Offset(0, 2) <= 12 Then
        findFifteen = pair.Offset(0, 2) / 10
        remainder = 0
    Else
        findFifteen = 1
        remainder = pair.Offset(0, 2) Mod 10
    End If

    For Each accumulator In Range("A36, D36, G36, J36, M36, A40, D40, G40, J40, M40")
        If accumulator.Offset(-1, 0) = Val(Left(pair, InStr(pair, "-")   - 1)) Then
            accumulator.Value = accumulator.Value + remainder
        End If
        accumulator.Value = accumulator.Value + findFifteen
    Next accumulator
    End If
Next pair

Application.ScreenUpdating = True
End Sub

电子表格:

优秀表

标签: excelvba

解决方案


从中间出来告诉我:

pair = "400-bar"
Debug.Print InStr(pair, "-")
Debug.Print Left(pair, InStr(pair, "-"))
Debug.Print Val(Left(pair, InStr(pair, "-"))) - 1

导致

4
400-
399

所以我认为你的助手所说的是在左函数括号内移动最右边的参数(-1)。

Debug.Print Val(Left(pair, InStr(pair, "-") - 1))

导致

400


推荐阅读