表单控件复选框,用于从其他工作表复制和粘贴文本 [英] Form Control Checkbox to Copy and Paste Text from a Different Sheet
问题描述
我正在寻找一种方法,当我勾选表单控件复选框时,将文本从一张纸复制并粘贴到另一张纸上,而在取消选中它时将其删除.目前,我编写的宏没有执行任何操作,它没有出现任何错误,只是无法正常工作.到目前为止,我有:
I am looking for a way to copy and paste text from one sheet to another when I tick a form control checkbox and to delete it when I uncheck it. At the moment the macro I have written does nothing, it doesn't come up with any errors it just doesn't work. What I have so far is:
Sub CheckBox3_Click()
Application.ScreenUpdating = False
If CheckBox3 = True Then
Sheets("Data Sheet").Activate
Range("B1").Select
Selection.Copy
Sheets("Sheet1").Select
Range("C1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End If
If CheckBox3 = False Then
ActiveSheet.Range("C1").Select
Selection.Delete
End If
End Sub
感谢您的任何帮助.
推荐答案
要克服运行时错误,请将代码更改为
To overcome your Runtime error please change your code to
ActiveSheet.Range("B1").Select
并对目标范围C1进行相同操作
and do the same for the target range C1
但是,更优雅的方法是通过使用VBA而不是宏录制"一起摆脱 Select
和 Activate
一起使用...使用Range对象这样可以简化您的代码,避免出现混乱的屏幕跳转等问题……
However, the much more elegant way is to get rid of Select
and Activate
all together by using VBA rather than "macro recording" ... work with Range objects which will simplify your code, you avoid messy screen jumps etc ...
Private Sub CheckBox1_Click()
Dim SrcRange As Range, TrgRange As Range
Set SrcRange = Worksheets("Data Sheet").[B1]
Set TrgRange = Worksheets("Sheet1").[C1]
If CheckBox1 Then
TrgRange = SrcRange
Else
TrgRange = ""
End If
End Sub
这篇关于表单控件复选框,用于从其他工作表复制和粘贴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!