2015-09-08 72 views
1

我一直在研究複製最後一列並插入新列的代碼,並複製其公式和格式。但是,我需要刪除行6中的單元格的值到24,然後從5678刪除創建的新列中的單元格的值。我無法找到一種方法來引用這些單元格以刪除它們的值,任何人都可以幫助我解決這個問題嗎?我的代碼如下:刪除創建的最後一列中的單元格的值

Sub Copy_Column() 
    Dim LastCol As Integer 
    With Worksheets("BO_Corretora") 
     LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column 
     Columns(LastCol).Copy 
     Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats 
     Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas 
    End With 
End Sub 

回答

0

這是你正在嘗試?

Sub Copy_Column() 
    Dim LastCol As Integer 
    Dim NewCol As String 

    With Worksheets("BO_Corretora") 
     LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column 
     .Columns(LastCol).Copy 
     .Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats 
     .Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas 

     '~~> Get the Column Letter of the new column 
     NewCol = Split(.Cells(, LastCol + 1).Address, "$")(1) 

     '~~> Range would be like Range("A6:A24,A56:A78") 
     .Range(NewCol & "6:" & NewCol & "24," & _ 
       NewCol & "56:" & NewCol & "78").ClearContents 
    End With 
End Sub 
1

您可以使用intersect。這使得要刪除的行易於閱讀,並且可以將它們存儲在常量中,以便在需要時進行簡單編輯。

Sub Copy_Column() 
    Dim LastCol As Integer 
    Dim NewCol As String 

    With Worksheets("BO_Corretora") 
     LastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column 
     .Columns(LastCol).Copy 
     .Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormats 
     .Columns(LastCol + 1).PasteSpecial Paste:=xlPasteFormulas 

     Intersect(.Columns(LastCol + 1), .Range("6:24,56:78")).ClearContents 
    End With 
End Sub 
相關問題