删除空白单元格

时间:2019-07-18 21:25:29

标签: excel vba

我正在尝试将数据编译为用于输入SAP的格式。将数据放入一列(或多列)后,我想删除空白单元格。

是否可以(在Excel中作为宏,或在VBA中进行编码)选择范围,查找所有空白(空白/空/等)单元格并将其删除?

如果手动执行此操作,则可以选择范围,按Ctrl-F进入“查找/替换”窗口,然后将查找和替换为空白,然后选中“匹配整个单元格内容”框,然后单击“查找”。所有”。这将选择所有空白单元格。然后,我退出查找窗口,右键单击并删除单元格,然后向上移动内容。

如果我尝试在宏中捕获Ctrl-F函数,它将无法正常工作。 录制时确实打开了窗口。单元格选择(特殊复制和粘贴)起作用。当我尝试查找选择时,VBA中没有任何更新。

Sub WinShuttleFill()
'
' WinShuttleFill Macro
'

'
'   Select Range of values (Cell values are linked with individual cells, some with values, some without that are filled in by personnel manually
    Range("A89:B518").Select

'   Copy the range
    Selection.Copy

'   Select the destination cell for pasting/editing
    Range("D89").Select

'   Paste Special (values) to replace content with values and "blanks"
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

'   Select the new "value" range to be edited
    Range("D89:E518").Select

'   This is where all the Ctrl-F and find/replace/cell contents portion should be showing up in the code, but doesn't
    Application.CutCopyMode = False

'   Since the individual "blank" cell selection portion fails, the end result of the step below is to delete the entire range, vice the selected cells
    Selection.Delete Shift:=xlUp

'   Past this point, the macro works correctly.  It selects the edited (deleted) range, copies it, pastes it to the appropriate location, places my cursor to A1 and then ends.  But since the delete function clears the entire range, there isn't anything to paste so the macro doesn't accomplish what I need it to.
    Range("D89:E518").Select
    Selection.Copy
    Sheets("Substation (WinShuttle)").Select
    Range("B2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
End Sub

1 个答案:

答案 0 :(得分:1)

这是我的解决方案。

Sub WinShuttleFill()

'' How to avoid SELECT -> Create variables


'' Create a variable that stores your source range
Dim sourceCells As Range
Set sourceCells = Range("A89:B518")

'' Create another variable for the pasted data range
Dim targetCells As Range
Set targetCells = Range("D89:E518")

'' Copy the source range by bypassing the clipboard
targetCells.Value = sourceCells.Value

'' Remove blank cells from target range
targetCells.SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp



'   Past this point, the macro works correctly.  It selects the edited (deleted) range, copies it, pastes it to the appropriate location, places my cursor to A1 and then ends.  But since the delete function clears the entire range, there isn't anything to paste so the macro doesn't accomplish what I need it to.
    '' The copy and paste could be changed here as well to the .Value method but I'll let you do that if you want :)
    targetCells.Copy
    Sheets("Substation (WinShuttle)").Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub
相关问题