简体   繁体   中英

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.

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). Copying the result from Sheet2 into the Module for Sheet2 is desirable in this scenario.

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.

Thank you.

So you can achieve this by 2 methods. 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.
  • Enable programmatic access to the VBA Project.
    In Excel 2010 select the Developer tab and click the Macro Security button.
    Under macro settings tick Trust access to the VBA project object model .

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.
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:

 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. To use that use ThisWorkbook.Worksheets("Sheet1"). instead of just Sheet1 .

Edit 2 (as I'm waiting for 5:30pm to go home):
Add this code into Sheet1 module and it will auto-update the comments whenever you type into cell U6:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$U$6" Then
        AddCommentModule
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM