只要找到某个文本字符串,就插入FormulaArray

时间:2013-09-26 12:06:32

标签: excel excel-vba vba

我在H列中列出了这个列表。

enter image description here

这是我的宏excel代码

Sub TheSpaceBetween1()
    Dim ws As Worksheet
    Dim LRow As Long, i As Long
    Dim insertRange As Range

    '~~> Chnage this to the relevant sheet
    Set ws = ActiveSheet

    '~~> Work with the relevant sheet
    With ws
        '~~> Get the last row of the desired column
        LRow = .Range("H" & .Rows.Count).End(xlUp).Row

        '~~> Loop from last row up
        For i = LRow To 1 Step -1
            '~~> Check for the condition
            '~~> UCASE changes to Upper case
            '~~> TRIM removes unwanted space from before and after
            If UCase(Trim(.Range("H" & i).Value)) = "NOT THE SAME" Then
                '~~> Insert the rows
                Selection.FormulaArray = "=MIN(IF(C[-7]=RC[-7],C[-6]))" <----this is the problem
            End If
        Next i
    End With
End Sub

不幸的是,它没有用。它假设用这个公式替换“不相同”这个词

Selection.FormulaArray = "=MIN(IF(C[-7]=RC[-7],C[-6]))"

宏的作用是什么

使用公式Selection.FormulaArray = "=MIN(IF(C[-7]=RC[-7],C[-6]))"

替换 ALL 单元格内容与文本NOT THE SAME

有些人可以帮我制作代码吗?

1 个答案:

答案 0 :(得分:1)

Santhosh已经提供了正确的答案,但我认为他错过了在他的答案中添加(.FormulaArray)。下面的一个对我来说很好(测试)

Sub TheSpaceBetween1()
    Dim ws As Worksheet
    Dim LRow As Long, i As Long
    Dim insertRange As Range

    '~~> Chnage this to the relevant sheet
    Set ws = ActiveSheet

    '~~> Work with the relevant sheet
    With ws
        '~~> Get the last row of the desired column
        LRow = .Range("H" & .Rows.Count).End(xlUp).Row

        '~~> Loop from last row up
        For i = LRow To 1 Step -1
            '~~> Check for the condition
            '~~> UCASE changes to Upper case
            '~~> TRIM removes unwanted space from before and after
            If UCase(Trim(.Range("H" & i).Value)) = "NOT THE SAME" Then
                '~~> Insert the rows
                .Range("H" & i).FormulaArray = "=MIN(IF(C[-7]=RC[-7],C[-6]))"  ' <----this is the problem
            End If
        Next i
    End With
End Sub