+ 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

    Wink Use a string to create VBA coding

    Hi,

    In VFP one can store any line of code in a variable and then execute it as a line of code by putting the & sign in front of it if i remember well:

    Ex:

    test = "Hello"
    test2 = 1
    test3 = "myform.text" & test2 & ".value=" & test

    &test3

    I looked all around the web to find a similar solution for Excel VBA, as I am trying to create a user automation tool. Despite all the "Nay" sayers, i am convinced there must be a simple solution to this challenge.

    Creating an on-the-fly procedure is not an option as it will get in trouble with many antiviruses.

    I tried to use EbExecuteLine which would have been the perfect solution, but Microsoft Office 2010 does not recognize it.

    Any idea would be greatly appreciated?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Use a string to create VBA coding

    The closest thing I can think of would be CallByName.
    Dim test
    Dim test2
    
    test = "Hello"
    test2 = 1
    
    CallByName Me.Controls("TextBox" & test2), "Value", VbLet, test
    If posting code please use code tags, see here.

  3. #3
    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

    Norie, thank you for the very quick reply.

    But I am not sure it will do, as what I am trying to do is to automate any action, not just the example i gave. It could be about switching spreadsheets (Ex: sheets("Contacts").select), or running a macro behind a button (Ex: call add_new_client).

    So it is not about "controls" only.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Use a string to create VBA coding

    As far as I know CallByName isn't just for controls, I based what I posted in what you posted?

    Why can't you write code in the 'normal' way?

    What sort of automation are you aiming for?

  5. #5
    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

    Is it possible to post a print screen to show what I am designing?

  6. #6
    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

    Please see attachment.
    Attached Files Attached Files

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

    What I am trying to do is provide users with a set of icons they can use to automate any tasks in the spreadsheet. I expect most uses will be to go to specific part of the spreadsheet (Range("C2").select), call function behind buttons (call xyz_sub), type data, etc.

    can callbyname do these tasks?

  8. #8
    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

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Use a string to create VBA coding

    I thought you didn't want to create code on the fly?

    PS Can you add code tags?

  10. #10
    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

    Yes, But one has to compromise at times...

    But I would still prefer a one line solution

  11. #11
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Use a string to create VBA coding

    There is no one line solution for VBA. (I understand I will simply be another nay sayer to you )

    Your solution requires that the user has trusted access to the VBA Project in the Trust Center settings. I think you will also run into occasional errors due to timing problems that will be very difficult to debug because you are adding code to the same project in which you are running code.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  12. #12
    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

    Well, running one line of code at a time does the job for now. I will soon try for more and will let you know.

    My motto is: When there is a will, there is a way...

    VFP was really such a great tool. It is too bad Microsoft killed it.

+ 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