简体   繁体   English

Excel 2013 VBA - 以编程方式从工作表2上的一个单元格复制文本,粘贴到Sheet1模块

[英]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..... 不知道为什么你想要,但.....

  • Add a reference in the VBE to Microsoft Visual Basic For Applications Extensibility 5.3. 在VBE中添加一个引用到Microsoft Visual Basic For Applications Extensibility 5.3.
  • Enable programmatic access to the VBA Project. 启用对VBA项目的编程访问。
    In Excel 2010 select the Developer tab and click the Macro Security button. 在Excel 2010中,选择“ Developer选项卡并单击“ Macro Security按钮。
    Under macro settings tick Trust access to the VBA project object model . 在宏设置下,勾选对VBA项目对象模型的信任访问

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.

相关问题 如果单元格的颜色为绿色,则从 Sheet1 复制一行并将其粘贴到 Sheet 2 - Copy a row from Sheet1 and paste it into Sheet 2 if color of a cell is green 从单元格J中包含“是”的Sheet1复制行并将其粘贴到Excel 2007中的Sheet4 - Copy and Paste a Row from Sheet1 Containing “YES” in Cell J to Sheet4 in Excel 2007 Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet Excel VBA从sheet1单元格复制到sheet2单元格,格式为 - Excel VBA copy from sheet1 cell to sheet2 cell with format Excel VBA,如何使用条件将单元格/单元格从工作表 1 复制到工作表 2 - Excel VBA, How to Copy the cell/cells from sheet1 to the sheet2 with condition 从Sheet1复制范围并将其粘贴到Sheet 2中 - Copy Range from Sheet1 And paste it in Sheet 2 请提供Excel VBA帮助; 将值从Sheet1复制到Sheet2中的条件匹配单元格 - Excel VBA assistance please; copy a value from Sheet1 to a criteria matching cell in Sheet2 Excel VBA编程可将特定行从sheet1复制到sheet2特定单元格 - Excel VBA programming to copy specific rows from sheet1 to sheet2 specific cell 在 excel VBA 中复制行范围从 Sheet1 到 Sheet2 - Copy rows range from Sheet1 to Sheet2 in excel VBA VBA-将多个单元中的一个单元格复制/粘贴到主表 - VBA - copy / paste one cell from multiple workseets to master sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM