0

我在 H 列中有这个列表。

在此处输入图像描述

这是我的宏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

不幸的是,它不起作用。假设用这个公式替换单词 NOT THE SAME

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

宏的作用是

所有单元格内容替换为与公式不一样的文本Selection.FormulaArray = "=MIN(IF(C[-7]=RC[-7],C[-6]))"

有人可以帮助我使代码正常工作吗?

4

1 回答 1

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
于 2013-09-26T12:15:20.540 回答