在包含我的数据的附件图像中,我正在尝试执行以下操作:
1)如果C列中的值=“ DOSH”,则要选择从I列到末尾(列BQ)的所有行和单个单元格
2)一旦我为每个单元格选择了这些单元格(I至BQ),我想使用公式
“ = R(-2)C / R(-3)C”
我从下面的代码开始,但是它选择了整个行,而不仅仅是BQ到I列。我也不确定应该在哪里添加公式。
Sub SelRows()
Dim ocell As Range
Dim rng As Range
For Each ocell In Range("C:BQ")
If ocell.Value = "DOSH" Then
If rng Is Nothing Then
Set rng = ocell.Select
Else
Set rng = Union(rng, ocell.EntireRow)
End If
End If
Next
If Not rng Is Nothing Then rng.Select
Set rng = Nothing
Set ocell = Nothing
End Sub
答案 0 :(得分:0)
您可以试试吗?我不确定您的公式,因此可能需要调整。假设您的数据从第2行开始。
Sub SelRows()
Dim ocell As Range
Dim rng As Range
For Each ocell In Range("C2", Range("C" & Rows.Count).End(xlUp))
If ocell.Value = "DOSH" Then
If rng Is Nothing Then
Set rng = Range(Cells(ocell.Row, "I"), Cells(ocell.Row, "BQ"))
Else
Set rng = Union(rng, Range(Cells(ocell.Row, "I"), Cells(ocell.Row, "BQ")))
End If
End If
Next
If Not rng Is Nothing Then
rng.FormulaR1C1 = ""=iferror(R[-2]C/R[-3]C,"""")""
End If
Set rng = Nothing
Set ocell = Nothing
End Sub