如果语句为true,则将列中的多个项替换为0

时间:2016-11-11 16:44:51

标签: excel vba excel-vba

filename                      ID            Ended On          Sync   Total   ToDo
2016_05_03_10_28_32_xxxx     xxxx    Tue May 3 10:29:57 2016    8     8        0

这是我正在处理的项目中excel中的电子表格的一行。如果xxxx,如何在电子表格中用ID替换0列中ToDo=0列的所有 A B C D E F 2016_05_13_23_53_28_xxxx xxxx Fri May 13 5 8 3 2016_05_14_05_33_42_xxxx xxxx Sat May 14 8 8 0 2016_05_14_07_23_22_yyyy yyyy Sat May 14 8 8 0 2016_05_14_07_23_22_yyyy yyyy Sat May 14 2 8 6 个实例。我需要为每一行做这件事。

编辑:

此文件大约有30k行。如何编写公式或VBA:如果F2 = 0,则将B列中文本值(B2)的所有外观替换为0.然后对每行重复。

字母代表列(第1行)

require

3 个答案:

答案 0 :(得分:0)

在单元格B2中填写并填写:

=IF(F2<>0,RIGHT(A2,4),"0000")

答案 1 :(得分:0)

Jim,除了上面引用的公式之外,VBA中的这段代码还会用零替换你的数据:

Sub Replace()
Dim i as long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow
    If Cells(i, 2) = "xxxx" And Cells(i, 6) = 0 Then
       Cells(i, 2) = 0
    End If
Next
End Sub

答案 2 :(得分:0)

您可以使用Autofilter()

Sub main()
    With Worksheets("Zeros") '<-- change "Zeros" to your actual worksheet name
        With .Range("F1", .Cells(.Rows.Count, 1).End(xlUp)) '<--| get range in columns "A:F" down to last non empty row in column "A"
            .AutoFilter Field:=6, Criteria1:=0 '<--| filter rows by column "F" (the 6th one) on value 0
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(, 1).Resize(, 1).SpecialCells(xlCellTypeVisible) = 0 '<-- if any rows filtered other than headers one then change their column "B" value to zero
        End With
        .AutoFilterMode = False '<-- remove dropdowns
    End With
End Sub

这应该比循环遍历单元格快得多