【发布时间】: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