有没有办法将单元格的位置添加到SUM公式中?
我有以下For Each ... Next循环,但我不想仅计算包含值1的单元格数量,而是想将这样一个单元格的特定位置添加到SUM公式中。
Dim rng As Range, cell As Range
Set rng = Range("I3:DC70")
For Each cell In rng
If cell.Value = 1 Then Range("DF4").Value = Range("DF4").Value + 1
Next cell
这可能吗?
答案 0 :(得分:0)
您必须使用.Find
和.FindNext
。你可以阅读它Here。当您拥有大型数据集时,此方法比循环快得多。
这是我写的很快的内容。请根据您的需要进行修改。
Sub Find_Cells_Which_Has_My_Damn_String()
Dim WhatToFind As String
Dim aCell As Range, bCell As Range
Dim SearchRange As Range, rng As Range
Dim ws As Worksheet
'~~> Set this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
'~~> Set here what to find
WhatToFind = "1"
'~~> Set this to the relevant range
Set SearchRange = ws.Cells
Set aCell = SearchRange.Find(What:=WhatToFind, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
Set rng = aCell
Do
Set aCell = SearchRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
Set rng = Union(rng, aCell)
Else
Exit Do
End If
Loop
End If
If Not rng Is Nothing Then MsgBox rng.Address Else _
MsgBox "No cells were found containing " & WhatToFind
End Sub
<强>截图强>