[英]Excel 2013 VBA - Programmatically Copy text from one Cell on Sheet 2, Paste into Sheet1 Module
Title is fairly self-explanatory, the goal is to use VBA on Sheet1 to Copy the contents of a Cell in Sheet 2, in this example Cell "U6", and Paste the copied text into Sheet1's Module. 标题是相当不言自明的,目标是使用Sheet1上的VBA复制工作表2中单元格的内容,在本例中为单元格“U6”,并将复制的文本粘贴到Sheet1的模块中。
The reason for copying text from a worksheet into a module in this case ( and I'm sure this can be done in several perhaps more efficient ways, but for the sake of trying, I wish to stick to this method for this issue ) is that the Cell on Sheet2 contains a Formula that arranges multiple lines of VBA syntax with several variables determined by other features in the WorkBook together into a brief line of code (four lines). 在这种情况下将文本从工作表复制到模块的原因( 我确信这可以通过几种更有效的方式完成,但为了尝试,我希望坚持这个方法来解决这个问题 )是Sheet2上的Cell包含一个公式,该公式将多行VBA语法与由WorkBook中其他功能确定的多个变量一起排列成一行简短的代码行(四行)。 Copying the result from Sheet2 into the Module for Sheet2 is desirable in this scenario. 在此方案中,需要将Sheet2中的结果复制到Sheet2的Module中。
For methods attempted, as the code source is on a Worksheet and does not yet live within a Module, unless I'm mistaken, I do not believe VBIDE would be an applicable solution. 对于尝试的方法,因为代码源在工作表上并且还没有存在于模块中,除非我弄错了,我不相信VBIDE是适用的解决方案。
Thank you. 谢谢。
So you can achieve this by 2 methods. 所以你可以通过2种方法实现这一点。 I have written both below. 我在下面写了两个。 You can use either of the one 你可以使用任何一个
Sub Copy()
'Method 1
Sheets("Sheet2").Range("U6").Copy
Destination:=Sheets("Sheet1").Range("A1")
'Method 2
'Copy the data
Sheets("Sheet2").Range("U6").Copy
'Activate the destination worksheet
Sheets("Sheet1").Activate
'Select the target range
Range("A1").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
No idea why you'd want to, but..... 不知道为什么你想要,但.....
Microsoft Visual Basic For Applications Extensibility 5.3.
在VBE中添加一个引用到Microsoft Visual Basic For Applications Extensibility 5.3.
Developer
tab and click the Macro Security
button. 在Excel 2010中,选择“ Developer
选项卡并单击“ Macro Security
按钮。 Use code similar to this: 使用类似这样的代码:
Sub AddProcedureToModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Dim x As Long
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Sheet1")
Set CodeMod = VBComp.CodeModule
x = 1
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Public Sub MyProcedureName()"
LineNum = LineNum + 1
Do While Sheet1.Cells(x, 1) <> ""
.InsertLines LineNum, " " & Sheet1.Cells(x, 1)
x = x + 1
LineNum = LineNum + 1
Loop
.InsertLines LineNum, "End Sub"
End With
End Sub
This will copy whatever is in Sheet1 column A into the VBE. 这会将Sheet1列A中的任何内容复制到VBE中。
http://www.cpearson.com/excel/vbe.aspx http://www.cpearson.com/excel/vbe.aspx
Edit: After re-reading your question, this code will add the value in U6 as a comment to the bottom of any code in Sheet1 module: 编辑:重新阅读您的问题后,此代码会将U6中的值作为注释添加到Sheet1模块中任何代码的底部:
Sub AddCommentModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Sheet1")
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "'" & Sheet1.Range("U6")
End With
End Sub
Note - in these instances, Sheet1 is the sheets codename and not necessarily the name that appears on the sheet tab. 注意 - 在这些情况下, Sheet1是工作表名称,而不一定是工作表选项卡上显示的名称。 To use that use ThisWorkbook.Worksheets("Sheet1").
要使用它,请使用ThisWorkbook.Worksheets("Sheet1").
instead of just Sheet1
. 而不仅仅是Sheet1
。
Edit 2 (as I'm waiting for 5:30pm to go home): 编辑2 (因为我等待下午5:30回家):
Add this code into Sheet1 module and it will auto-update the comments whenever you type into cell U6: 将此代码添加到Sheet1模块中,只要您键入单元格U6,它就会自动更新注释:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$U$6" Then
AddCommentModule
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.