我一直致力于复制最后一列并插入新列的代码,复制其公式和格式。但是,我需要在创建的新列中删除行6
到24
,然后从56
到78
的单元格值。我无法找到一种方法来引用这些单元格以删除它们的值,有人可以帮我这个吗?我的代码如下:
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 :(得分: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
答案 1 :(得分: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