如何在真正的空白单元格上进行查找和替换

时间:2018-11-09 19:55:50

标签: excel vba excel-vba replace find

我有一个范围,该范围可以大小变化,并且可以包含数万个单元格。 对于此范围中每个具有字符串的单元格,我都需要替换为1。对于完全没有值的每个单元格,我都需要替换为零。

我尝试了以下操作,但是尽管确实用一个替换了填充的单元格,但空白单元格仍然空白。

    button.addMouseListener(new MouseAdapter() {
        boolean mouseDown;
        @Override
        public void mouseDown(MouseEvent e) {
            System.out.println("mouseDown");
            mouseDown = true;
            Display.getDefault().asyncExec(new Runnable() {
                @Override
                public void run() {
                    while (mouseDown) {
                        System.out.println("Doing next in mouseDown");
                        next(composite, label_1);
                        synchronized(this){
                            try {
                                wait(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });

        }
        @Override
        public void mouseUp(MouseEvent e) {
            System.out.println("mouseUp");
            mouseDown = false;
        }
    }); 

我也尝试了相同的结果。

Selection.Replace What:="*", Replacement:="1", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

编辑:包括完整的代码

Selection.Replace What:=null, Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

编辑:我试图在下面的一些代码之后进行尝试,但是尽管它似乎可以正常工作,但我无法选择一个自定义范围。

Sub MassFindReplace()

        ' This will select an area within the given parameters and replace all blank cells with zeros and all populated cells with Ones

    Dim VRange1 As String
    Dim VRange2 As String
    Dim Doublecheck As Integer

    VRange1 = InputBox("Enter First Cell Address Here" & vbNewLine & vbNewLine & "Make sure you ONLY input a single cell address")

    VRange2 = InputBox("Enter Second Cell Address Here" & vbNewLine & vbNewLine & "Make sure you ONLY input a single cell address")

    Range(VRange1, VRange2).Select

    Doublecheck = MsgBox("The range you have selected is between " & VRange1 & " and " & VRange2 & vbNewLine & vbNewLine & "Does this sound right to you?" & vbNewLine & vbNewLine & "If not press No to cancel", vbYesNo)

    If Doublecheck = vbYes Then

    ' This turns off a number of background functions and greatly speeds up this process
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    ' choose what to search for and what to replace with here
    Selection.Replace What:="*", Replacement:="1", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Selection.Cells.SpecialCells(xlCellTypeBlanks).Value = 1


    'Resets the background functions. THIS MUST HAPPEN or it will screw up your excel.
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.CalculateFull

    MsgBox "Complete"

    Else
        MsgBox "Canceled"

    End If

End Sub

结束子

3 个答案:

答案 0 :(得分:2)

使用此:

On Error Resume Next
    Selection.Cells.SpecialCells(xlCellTypeBlanks).Value = 1
On Error GoTo 0

请注意,它只会填充UsedRange和Selected Cells的交集。

答案 1 :(得分:2)

如果您必须检查并评估每个单元格,则只需检查每个单元格以查看是否为空。当然,如果工作表的UsedRange不在您需要的范围内,则可以手动指定它。

Sub MassFindReplace()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim cel As Range

    For Each cel In ws.UsedRange
        If cel.Value <> "" Then
            cel.Value = 1
        Else
            cel.Value = 0
        End If
    Next

End Sub

根据urdearboy的建议,您也可以将其加载到数组中,然后在此处进行检查。

Sub MassFindReplace()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim data As Variant, v As Variant

    data = ws.UsedRange.Value

    For i = LBound(data, 1) To UBound(data, 1)
        For j = LBound(data, 2) To UBound(data, 2)
            If data(i, j) <> "" Then
                 data(i, j) = 1
            Else
                data(i, j) = 0
            End If
        Next
    Next

    ws.UsedRange.Resize(UBound(data, 1), UBound(data, 2)).Value = data

End Sub

答案 2 :(得分:0)

使用“〜*”。

Selection.Replace What:="~*", Replacement:="1", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False