excel vba在不同单元格中添加和减去值
我正在用Excel制作一种日程安排表。在该表中,输入了某些专家和活动的工时。经常发生的情况是,必须在专家和活动之间转换工作日。我一直坚持的部分是单元格中值的实际更新。我的想法是,我的第一个数组中的所有行都表示行号。我一步一步地在范围内的每个单元格中查找一个值并减去移动天数。如果移动天数大于单元格值,我将移动到下一个,依此类推,直到所有天数都用完。第二个例行程序使用相同的系统,但增加了工时。我的问题是源活动的工时先增加后减少,但目标活动应该增加,源活动应该减少 获得想法的表格结构-括号中的部分应更新:excel vba在不同单元格中添加和减去值,excel,vba,cell,Excel,Vba,Cell,我正在用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
ReduceDaysCounter=ShiftDays
对于行=UBound(FirstExprowNumber)到0,步骤-1
如果FirstExpRowNumbers(行)>0,FirstExpRowNumbers(行)为0且ReduceDaysCounter>0,则
如果ReduceDaysCounter>=currcell值,则
单元格(FirstExprowNumber(行),列)。值=0
ReduceDaysCounter=ReduceDaysCounter-CurrCellValue
如果结束
如果结束
下一个
如果结束
下一个
增加工时的代码:
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
increaseDaysCenter=ShiftDays
对于行=0到UBound(SecondExprowNumber)
如果SecondExpRowNumbers(行)>0,SecondExpRowNumbers(行)为0且IncreaseDaysCenter>0,则
'如果Currcell值小于2,则
单元格(第二行,列)。值=当前单元格值+1
递增日中心=递增日中心-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
你能附上预期结果的文件吗?这是一个线性规划问题吗?@Kannan S:我上传了一个截图来澄清。对不起,我不能提供文件。我想我现在知道问题出在哪里了。我使用数组来保存专家名的所有行号。我使用这个数组来更新单元格,但我忘记了我不能使用整个数组,因为目标和源活动约束可用的行号,或者应该约束它们。如果我有几个专家被分配到一项活动,这将变得混乱。如何找到要更新的正确行号?