VBA - 范围 ("All cells but a few")

标签 vba excel

我正在尝试清除工作表上除特定范围之外的所有单元格的内容。我尝试将范围复制到剪贴板,然后将其再次粘贴到同一位置,但是 excel 是通常棘手的野兽 - 它不想玩球。

我希望保持不变的范围是 AB1:AC5。

任何建议均已采纳...

(这是我的代码)

Sub Button21_Click()

Application.ScreenUpdating = False

With Worksheets(2)
.Range("AB1:AC5").Copy
.Cells.ClearContents
.Paste(Destination:=Sheets("Data").Range("AB1"))
End With

Application.ScreenUpdating = True

End Sub

最佳答案

使用数组代替:

Sub Button21_Click()

Application.ScreenUpdating = False

Dim oldValues As Variant

With Worksheets(2)
    oldValues = .Range("AB1:AC5").Value
    .Cells.ClearContents
    .Range("AB1:AC5").Value = oldValues
End With

Application.ScreenUpdating = True

End Sub

关于VBA - 范围 ("All cells but a few"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38606800/

相关文章:

string - 字符串格式的疯狂问题

excel - 在 Excel 的基本宏中使用 ATL 库的功能

excel - 根据变量开始和结束列连接 Excel 行中的单元格

vba - Excel-VBA 多页 : move/reorder/index pages at runtime?

excel - 在 MS Excel 中创建 "search"函数

ms-access - Access 数据库表单 ActiveX 控件错误

python - 有没有更快的方法将 excel 文件读取到 pandas 数据框

excel - 拆分基于分隔符的单元格值并插入新列

javascript - 使用模块 xlsx 样式时无法找到命名空间 XLSX

vba - 首次运行时出现 OnAction 运行时错误 "1004"