0

I want to run an excel vba which will go down column E and upon finding the value = "capa" will go two cell below, calculate the hex2dec value of that cell, present it by the cell with the value "capa" in column F and continue to search down column E. So far I've came with the below but it doesn't work:

 For Each cell In Range("E:E")
     If cell.Value = "Capa" Then
           ActiveCell.Offset.FormulaR1C1 = "=HEX2DEC(R[2]C[-1])"
     End If
Next cell

Thanks!

4

2 回答 2

0

这样的事情怎么样?

这将在 E 卷中搜索“Capa”,如果找到,将使用 E 列中“Capa”正下方的值将公式放在 F 列中

Sub CapaSearch()
    Dim cl As Range

    For Each cl In Range("E:E")
        If cl.Value = "Capa" Then
            cl.Offset(0, 1).Formula = "=HEX2DEC(" & cl.Offset(1, 0) & ")"
        End If
    Next cl
End Sub
于 2012-09-08T08:00:00.393 回答
0

你真的想限制循环,这样你就不会循环整个工作表(Excel 2007+ 中的 1,000,000+ 行)
此外,将源数据复制到变量数组也会加快速度。

尝试这个

Sub Demo()
    Dim dat As Variant
    Dim i As Long
    With ActiveSheet.UsedRange
        dat = .Value
        For i = 1 To UBound(dat, 1)
            If dat(i, 6 - .Column) = "Capa" Then
                .Cells(i, 7 - .Column).FormulaR1C1 = "=HEX2DEC(R[2]C[-1])"
            End If
        Next
    End With
End Sub
于 2012-09-08T09:33:51.217 回答