在最后一个工作表之后将值复制到新工作表,如果为空则删除列b

时间:2013-04-19 20:56:46

标签: excel excel-vba vba

我只想复制我运行宏的任何工作簿的最后一张表的值(不是公式)(我不知道页面的名称或页数)并删除B列,如果它是空白的并命名这个新的wirksheet“游戏”。这就是我所拥有的并且不起作用=(。有人可以给我一个帮助吗?

Sub ArrumarTabela()
    ActiveSheet.Copy
    Cells.Copy

    Application.CutCopyMode = False
    ActiveSheet.Name = "Games"
    Range("A1").PasteSpecial Paste:=xlPasteValues
    Application.ScreenUpdating = False

    ActiveSheet.Name = "Games"

    Dim cl As Range
    For Each cl In Range("$B$2:$B" & Range("$B$65536").End(xlUp).Row)
        If cl = "" Then cl.EntireColumn.Delete
    Next cl

    Range("C1").Select
    Selection.EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove

    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

这种修改能否满足您的需求?

Sub ArrumarTabela()
    ActiveSheet.Copy 'this will create a new workbook and a new sheet with current

    With ActiveSheet
        .Name = "Games" 'change new sheets name

        .Cells.Copy 'copy all cells
        .Range("A1").PasteSpecial Paste:=xlPasteValues 'paste only values
        .ScreenUpdating = False

        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row?

        Set cl = .Range("B2:B" & lastrow).Find("", lookat:=xlWhole) 'find a blank cell in a range

        If Not cl Is Nothing Then cl.EntireColumn.Delete 'delete column if blank exists

        .Range("C1").EntireColumn.Insert 'insert new column belfore column C

        .ScreenUpdating = True
    End With
End Sub