VBA删除多列选择的空单元格

时间:2013-11-11 14:28:19

标签: excel vba excel-vba

我找到一个可能解决我的潜在问题的方法遇到了一些麻烦。我正在使用VBA为我的主管编写一个宏,以便她只需单击分配给该宏的按钮,然后按照说明获取所需的数据。我遇到的问题是当宏粘贴数据时,如果用户选择多个列,则无法删除空单元格。

Sub DataPull()
' Written by Agony
' Data Pull macro
Dim rng1 As Range
Dim rng2 As Range
Dim chc1
Dim chc2
Dim wb1 As Workbook
Dim wb2 As Workbook

'Choose file to get data
chc1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select file to pull data from")
If chc1 = False Then Exit Sub

'Choose file to paste data
chc2 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select file to paste data to")
If chc2 = False Then Exit Sub

'Open first file and copy range
Set wb1 = Workbooks.Open(chc1)
Set rng1 = Application.InputBox("Select cells to transfer", "Selection", "Use your mouse/pointer to select the cells", Type:=8)
rng1.Copy
wb1.Close SaveChanges:=False

'Open second file and paste with specs
Set wb2 = Workbooks.Open(chc2)
Set rng2 = Range("A1")
rng2.PasteSpecial
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With Selection.Font
    .Name = "Cambria"
    .Size = 12
    .TintAndShade = 0
End With

Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

'Loop to delete empty cells
Dim i As Long
Dim rows As Long
Dim rng3 As Range
Set rng3 = ActiveSheet.Range("A1:Z50")
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
    If WorksheetFunction.CountA(rng3.rows(i)) = 0 Then rng3.rows(i).Delete
Next

wb2.Activate
MsgBox ("Macro Complete")
End Sub

如上所示,该范围目前是暂定的。如果用户选择具有多列的范围,我希望该函数删除空的单元格。我已经尝试将Len用于单元格,但这似乎也不起作用。任何帮助是极大的赞赏。谢谢!

1 个答案:

答案 0 :(得分:3)

我不认为您可以在源工作簿关闭时使用.Copy.Paste

我认为无论你复制什么,都会在工作簿关闭时丢失。

因此,解决问题的可能方法是关闭宏末尾的wb1,而不是在复制命令之后立即关闭。

所以将wb1.Close SaveChanges:=False移到此块之后

...
'Open second file and paste with specs
Set wb2 = Workbooks.Open(chc2)
Set rng2 = Range("A1")
rng2.PasteSpecial
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With Selection.Font
    .Name = "Cambria"
    .Size = 12
    .TintAndShade = 0
End With
wb1.Close SaveChanges:=False ' moved it here
...

Deletetion

试试这个子,看看这是不是你想要的。这是什么它找到电子表格中使用的最后一列和每列中的最后一行。从每列中的最后一行向后迭代,并删除所有空单元格,将填充的单元格向上移动。

Sub DeleteAllAtOnce()
Application.ScreenUpdating = False
    Dim lastColumn As Long
    Dim lastRow As Long

    lastColumn = ActiveSheet.UsedRange.Columns.Count

    Dim i As Long, j As Long
    Dim cell As Range
    For i = lastColumn To 1 Step -1
        lastRow = Cells(rows.Count, i).End(xlUp).Row
        For j = lastRow To 1 Step -1
            Set cell = Cells(j, i)
            If IsEmpty(cell) Then cell.Delete shift:=xlUp
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub