【问题标题】:excel vba adding and subtracting values in different cellsexcel vba在不同的单元格中添加和减去值
【发布时间】:2012-01-18 14:30:01
【问题描述】:

我正在使用 Excel 制作一种日程安排表。在这张表中,输入了某些专家和活动的工作日。通常情况下,必须在专家和活动之间转换工时。我坚持的部分是单元格中值的实际更新。这个想法是我的第一个数组中的所有行都代表行号。我逐步遍历范围内的每个单元格,寻找一个值并减去变化的天数。如果移动天数大于单元格值,我将移动到下一个,依此类推,直到所有天数都用完。第二个例程使用相同的系统,但增加了人工天数。我的问题是源活动的工日增加然后减少,但目标活动应该增加而源活动减少。

获得想法的工作表结构 - 括号中的部分应更新:

     M1 M2 M3 ... EXP1 EXP2 EXP3
A1[  1  1  1  ]    3 
A2[  1     1  ]         2
A3[        1  ]              1

减少工时的代码:

ReduceDaysCounter = ShiftDays

For row = UBound(FirstExpRowNumbers) To 0 Step -1  
    If FirstExpRowNumbers(row) > 0 And FirstExpRowNumbers(row) <= LastRow() Then
        For col = ExpertColumns(0) - 1 To 5 Step -1
            CurrCellValue = cells(FirstExpRowNumbers(row), col).Value
            If CurrCellValue > 0 And ReduceDaysCounter > 0 Then
                If ReduceDaysCounter >= CurrCellValue Then
                    cells(FirstExpRowNumbers(row), col).Value = 0
                    ReduceDaysCounter = ReduceDaysCounter - CurrCellValue
                End If
            End If
        Next
    End If
Next

增加工作日的代码:

IncreaseDaysCounter = ShiftDays

For row = 0 To UBound(SecondExpRowNumbers)  
    If SecondExpRowNumbers(row) > 0 And SecondExpRowNumbers(row) <= LastRow() Then
        For col = 5 To ExpertColumns(0) - 1
            CurrCellValue = cells(SecondExpRowNumbers(row), col).Value
            If CurrCellValue > 0 And IncreaseDaysCounter > 0 Then
                'If CurrCellValue < 2 Then
                    cells(SecondExpRowNumbers(row), col).Value = CurrCellValue + 1
                    IncreaseDaysCounter = IncreaseDaysCounter - 1
                'End If
            End If
        Next
    End If
Next

【问题讨论】:

  • 你能附上预期结果的文件吗?
  • 这是一个线性规划问题吗?
  • @Kannan S:我上传了一张截图以供澄清。抱歉,我无法提供文件。 flic.kr/p/aUK4Fg
  • 我想我现在知道问题出在哪里了。我使用一个数组来保存专家名称的所有行号。我使用这个数组来更新单元格,但我忘记了我不能使用整个数组,因为目标和源活动限制了可用的行号或应该限制它们。如果我有几位专家分配给一项活动,这将变得一团糟。如何找到正确的行号进行更新?

标签: excel vba cell


【解决方案1】:

好的,我发现了问题。这是查找正确行号的函数:

Function FindingSDExpRow(actrow, expname)

Dim SDExpRow As Integer
SDExpRow = 0

Do While SDExpRow = 0
    actrow = actrow + 1
    If Left((cells(actrow, 2).Value), Len(expname)) = expname Then
        SDExpRow = cells(actrow, 2).row
    End If
Loop

FindingSDExpRow = SDExpRow

End Function

然后就很简单了——修改单元格值的代码:

ReduceDaysCounter = ShiftDays

For col = ExpertColumns(0) - 1 To 5 Step -1
    CurrCellValue = cells(FirstExpRow, col).Value
    If CurrCellValue > 0 And ReduceDaysCounter > 0 Then
        If ReduceDaysCounter >= CurrCellValue Then
            cells(FirstExpRow, col).Value = 0
            ReduceDaysCounter = ReduceDaysCounter - CurrCellValue
        End If
    End If
Next

IncreaseDaysCounter = ShiftDays

For col = 5 To ExpertColumns(0) - 1
    CurrCellValue = cells(SeconExpRow, col).Value
    If CurrCellValue > 0 And IncreaseDaysCounter > 0 Then
        cells(SeconExpRow, col).Value = CurrCellValue + 1
        IncreaseDaysCounter = IncreaseDaysCounter - 1
    End If
Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2014-08-23
    • 2021-12-06
    相关资源
    最近更新 更多