我希望将单元格A1:B100复制到D1:E100,但要删除空格。
这是一个例子:
有一个简单的方法可以做到这一点:
初始数据:
步骤1:复制中的所有数据A:B
。选择D1
,右键单击并选择PasteSpecial-> PasteValues:
步骤2:在选定范围内将数据列入列D:E
(在我的情况下D1:E8
),请按CTRL+G
步骤3:在GoTo
对话视窗中,按一下Special...并选取[空白]
步骤4:现在D1:E8
选中所有空单元格。
步骤5:右键单击amy选定的单元,然后按Delete
第6步:选择“向上移动单元格”,然后按OK。完毕!
结果:
UPD:作为评论的后续内容,OP=IF(Sheet1!D11="YES",Sheet1!A11, "")
在中A:B
。而且由于""
实际上不是空白单元格,所以我们不能使用Special-> Blanks。
有VBA解决方案:
Sub test()
Dim lastrow As Long
Dim cell As Range
Dim rng As Range
'change sheet name to suit
With ThisWorkbook.Worksheets("Sheet1")
'find lastrow in columns A:B
lastrow = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, _
.Cells(.Rows.Count, "B").End(xlUp).Row)
'Copy values from A:B to D:E
.Range("D1:E" & lastrow).Value = .Range("A1:B" & lastrow).Value
'Iterates throught each cell in D:E and if it equals to "" - remember it
For Each cell In .Range("D1:E" & lastrow)
If cell.Value = "" Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next
'delete all empty cells (with "")
If Not rng Is Nothing Then rng.Delete Shift:=xlUp
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句