搜索空单元格;将相邻单元格复制到消息框;删除行或将文本添加到空单元格

时间:2019-07-02 04:55:08

标签: vba

在D列的范围内循环查找空单元格。找到空单元格后,将文本从C列中的相邻单元格复制到“消息框”。消息框提供了删除行或将文本添加到空单元格的选项。重复直到D列中没有空单元格。

期望删除包含无用信息的行,并保留包含无用信息的行。包含有用信息的行将被重新分类。

2 个答案:

答案 0 :(得分:0)

Dim i As Integer
Dim lastRowCat As Integer
Dim cat As String
Dim ws As Worksheet
Set ws = ActiveSheet

lastRowCat = ws.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).row   'This gives the last Row with a nonempty cell in column A
For i = 2 To lastRowCat
    If IsEmpty(ws.Cells(i, 4)) Then
    abc = MsgBox(ws.Cells(i, 3), vbYesNo + vbQuestion, "Save Transaction")

    If abc = vbYes Then
    cat = Application.InputBox("Add New Category" + " " + ws.Cells(i, 3))
    ws.Cells(i, 4).Value = cat

    Else
    ws.Cells(i, 3).EntireRow.Delete

    End If

    End If
Next i

答案 1 :(得分:0)

Set ws = ActiveSheet
    lastRowCat = 300
    blnks = 300
    For i = 2 To lastRowCat
        If IsEmpty(ws.Cells(i, 4)) Then
            blnks = Range("D2" & ":" & "D" & lastRowCat).SpecialCells(xlCellTypeBlanks).Count
            abc = MsgBox(ws.Cells(i, 3) & " " & "$" & ws.Cells(i, 5), vbYesNo + vbQuestion + vbDefaultButton2, "Save Transaction ?" & " " & blnks & " " & "left")

            If abc = vbYes Then
                cat = Application.InputBox("Add New Category" & " " & ws.Cells(i, 3))
                ws.Cells(i, 4).Value = "(new)" & cat
                lastRowCat = ws.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).row
            Else
                ws.Cells(i, 3).EntireRow.Delete
                lastRowCat = ws.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).row
            End If

        End If

        If i >= lastRowCat Then
            Exit For
        End If

    Next i