Results 1 to 2 of 2

Thread: Recording Macros in Excel through VB

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Portland, OR
    Posts
    47

    Recording Macros in Excel through VB

    Hey,
    I have a VB Application that creates an instance of an excel.application. The application then enters data in the excel sheet, just like normal. The thing that is tricky is i need to add code to a worksheet event, only i want this code to still be attached to the event after my VB application is closed. In other words, i need the code to be a macro in the workbook, not in my application
    There is an application.RecordMacro methodb where you can pass in a string of VB code, but this requires the 'Record Macro' tool bar option to be recording to work. So the basis of my question is: is there anyway to start recording a macro through code, if not, how else could this be accomplished?

    Any guidance on this issue woudl be amazingly helpful, as I've been stuck for a few days on it
    Thank you!!

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Recording Macros in Excel through VB

    This should put code in ThisWorkbook:

    VB Code:
    1. Sub addcode()
    2. Dim Code As String
    3. Dim nextline As Byte
    4.  
    5. Code = "Private Sub Workbook_Activate()" & vbCrLf
    6. Code = Code & "   On error resume next" & vbCrLf
    7. Code = Code & "   Sheets(""Sheet1"").Activate" & vbCrLf
    8. Code = Code & "End sub"
    9.  
    10. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    11.     nextline = .CountOfLines + 1
    12.     .InsertLines nextline, Code
    13. End With
    14.    
    15.    
    16.  
    17. End Sub

    Hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width