VBA选择不应该使用IF Range.Text =" True"?

时间:2016-08-10 19:05:54

标签: excel vba excel-vba

我有以下代码,这是一项正在进行中的工作,但是VBA一直表示If Range("G"&CRow).text = "True" then在突出显示的行中是正确的,当它显然不是时。任何人都可以帮我解决这个问题吗?

enter image description here

Range("G1").FormulaR1C1 = _
"=IF(OR(ISNUMBER(SEARCH(""GS "",RC[-6])),ISNUMBER(SEARCH(""@"",RC[-6]))),""TRUE"",""FALSE"")"
Range("G1").AutoFill Destination:=Range("G1:G" & lastrow)

With Range("G1:G" & lastrow)
    .Value = .Value
End With

Dim T As Integer
Dim CRow As Integer
CRow = 1

For Each cell In Range("G1:G" & lastrow)
    If Range("G" & CRow).Text = "TRUE" Then
        cell.Select
        ActiveCell.Offset(0, -5).Select

        If Selection.Value = "" Then
            Selection.Resize(, 4).Select
            Selection.Delete Shift:=xlUp
            ActiveCell.Offset(2, 0).Select
            Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            CRow = CRow - 1
        End If
    Else
        CRow = CRow + 1
    End If
Next

2 个答案:

答案 0 :(得分:1)

由于这个

CRow = 1
For Each cell In Range("G1:G" & lastrow)
If Range("G" & CRow).Text = "TRUE" Then

您正在为CRow分配1并在每次迭代中使用它。所以实际上你总是测试Just Row 1。 将Range("G" & CRow).Text更改为cell.Text

答案 1 :(得分:0)

请参阅下面的示例,使用反向循环删除同一组单元格而不选择。我相信我正确地解释并改变了这条线ActiveCell.Offset(2, 0).Select,但是如果我误认为它并没有按预期发挥作用,请告诉我。

Range("G1").FormulaR1C1 = _
"=IF(OR(ISNUMBER(SEARCH(""GS "",RC[-6])),ISNUMBER(SEARCH(""@"",RC[-6]))),""TRUE"",""FALSE"")"
Range("G1").AutoFill Destination:=Range("G1:G" & lastrow)

With Range("G1:G" & lastrow)
    .Value = .Value
End With

Dim T As Integer

For T = 1 to lastrow Step -1
    Set cell = Range("G" & T)
    If cell.Text = "TRUE" Then
        If cell.offset(0,-5) = "" Then
            cell.Offset(0,-5).Resize(,4).Delete Shift=xlUp
            Range("G" & T + 2).Insert Shift:=xlDown CopyOrigin:=xlFormatFromLeftOrAbove
        End If
    End If
Next