删除最后一列创build的单元格的值
我一直在做一个代码,复制最后一列,并插入一个新的,复制其公式和格式。 但是,我需要删除从第6
到24
行,然后从56
到78
在新创build的列的单元格的值。 我找不到一种方法来引用这些单元格来删除它们的值,有谁能帮我解决这个问题吗? 我的代码如下:
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
你可以使用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
这是你正在尝试?
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