+ Reply to Thread
Results 1 to 12 of 12

Use a string to create VBA coding

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Use a string to create VBA coding

    I actually found my solution. It is as follows:

    quote = chr(34)
    text = "Range(" & quote & "B5" & quote & ").select"

    call execute_code(text)

    Sub execute_code(code_string)
    'Create a new module in the current workbook, enter the code, run and remove the new module

    On Error Resume Next

    'Delete module in case it already exists
    Dim VBComp As VBComponent
    Set VBComp = ThisWorkbook.VBProject.VBComponents("NewModule")
    ThisWorkbook.VBProject.VBComponents.Remove VBComp
    On Error GoTo 0

    Dim LineNum As Long


    Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
    VBComp.Name = "NewModule"

    'Add the code lines
    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("NewModule").CodeModule
    With VBCodeMod
    LineNum = .CountOfLines + 1
    .InsertLines LineNum, _
    "Sub MyNewProcedure()" & Chr(13) & _
    code_string & Chr(13) & _
    "End Sub"
    End With

    'Run the new module
    Application.Run "MyNewProcedure"

    'Remove the new module
    ThisWorkbook.VBProject.VBComponents.Remove VBComp
    End Sub


    Nora, thank you so much for your quick answers.
    Last edited by Henri F; 01-02-2014 at 08:33 PM. Reason: missed a critical line

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. vba coding to create chart
    By amu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-01-2012, 01:35 AM
  2. More Efficient coding checkboxes into string var's
    By davegugg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-14-2009, 01:28 PM
  3. Coding links in a text string
    By herric in forum Excel General
    Replies: 3
    Last Post: 10-29-2008, 10:59 AM
  4. Need help coding string manipulation!
    By b744erf in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-12-2007, 08:34 PM
  5. how to create chart by VBA coding?
    By Fendic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-24-2005, 07:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1