删除空白单元格

Posted

技术标签:

【中文标题】删除空白单元格【英文标题】:Delete Blank Cells 【发布时间】:2019-11-27 21:06:45 【问题描述】:

我正在尝试将数据编译为输入到 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

【问题讨论】:

旁注:你想在你的代码中avoid using select 【参考方案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

【讨论】:

效果很好。非常感谢您的帮助 dasche。

以上是关于删除空白单元格的主要内容,如果未能解决你的问题,请参考以下文章

Excel批量复制公式删除空白行

求助excel单元格不同自动添加3个空白行

如果单元格为空白,则清除 VBA 中另一个单元格代码的内容

UITableView 在删除一行或多行后留下空白单元格的模式

UICollectionView 在滚动时留下空白单元格

如果某列范围内的所有单元格均为空白,则删除 Excel 中的一行