现在我正在尝试在excel电子表格中搜索特定的彩色单元格。当excel找到一个与颜色匹配的单元格时,我想要excel从该单元格中取出数字并将其乘以其偏移量,即它正下方的单元格。完成后,我可以将它存储在另一个变量中。一旦在纸张上找到所有有色单元格,它就会将所有产品加起来并将答案返回到另一个单元格中。以下是我现在的代码:
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim sResult
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then 'If true then sum the colored Cells
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult) 'Sums the rCell and adds to vResult 'then stores in vResult
End If
Next rCell 'Goes to next cell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
oCell = Range("rCell").Offset(1, 0).Select
pResult = oCell * rCell
vResult = WorksheetFunction.SUM(pResult, vResult)
End If
Next rCell
End If
ColorFunction = vResult
End Function
现在我让我真正的一半程序工作得很好。我只是不能让程序的错误部分在VBA中工作。任何帮助都会很棒!
答案 0 :(得分:0)
这是不正确的:
oCell = Range("rCell").Offset(1, 0).Select
rCell不是范围的名称,而是范围。尝试:
oCell = rCell.Offset(1, 0).Select
此外,您不需要选择范围,但获取范围值。所以这更正确:
oCell = rCell.Offset(1, 0).Value