Question : Excel VBA: How to run dynamically created code/string

Hey Experts,

I am trying to execute a string that I have dynamically created.  I build this code through a loop, then want to execute it afterwords.  Is there a function or way that I can do this?  Or if there is not, is there an alternative way to solve my issue?  See Code for what I am trying to do.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Function RunCode(TotalRows)
    Dim SelectionString As String
    Dim counter As Integer
        SelectionString = "Selection.TextToColumns Destination:=ActiveCell, datatype:=xlFixedWidth, FieldInfo:=Array("
        Do
        If (counter = 0) Then
            SelectionString = SelectionString & "Array(" & myArray(counter) & ", 2)"
        Else
            SelectionString = SelectionString & ", Array(" & myArray(counter) & ", 2)"
        End If
        counter = counter + 1
    Loop Until (counter > TotalRows)
    SelectionString = SelectionString & "), TrailingMinusNumbers:=True"
    MsgBox (SelectionString)
    'This string successfully produces the function to convert text in my selection to columns.
    'However, SelectionString is a string obviously, and looks like the function I need, but I need a way to execute it.
    'Or I need a different way to do this. The TotalRows changes each time, so I need a way to build this large 'FieldInfo' array dynamically
    'NOTE: myArray is a global array, don't worry about what is in it.
End Function

Answer : Excel VBA: How to run dynamically created code/string


I wrote the following code for another problem, i have taken out the unnecessary stuff, I believe this is a much better way to approach opening the said workbook, then closing it, then quitting excel

notice the private function at the bottom, also this assumes you will add a reference to excel in the project and also use Imports Excel = microsoft.blah.blah.blha

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
Dim excelApp As Excel.Application
        Dim WB As Excel.Workbook
        Dim workbookName As String = "Test Workbook Name"

        'open Excel to minimized state
        excelApp = New Excel.Application()
        excelApp.Visible = CBool(Microsoft.Office.Core.MsoTriState.msoTrue)
        excelApp.WindowState = Excel.XlWindowState.xlMinimized

        'open workbook
        WB = excelApp.Workbooks.Open(workbookName, Microsoft.Office.Core.MsoTriState.msoFalse, _
                                     Microsoft.Office.Core.MsoTriState.msoFalse, _
                                     Microsoft.Office.Core.MsoTriState.msoTrue)
	
	WB.close
	excelApp.quit

        'need to release these com objects or they may remain open in the running services
        ReleaseObject(excelApp)
        ReleaseObject(WB)
        ReleaseObject(WS)
        ReleaseObject(target)

        excelApp = Nothing
        WB = Nothing
        WS = Nothing
        target = Nothing
        'Erase targetArray

        'run garbage collector
        GC.Collect()

    Private Sub ReleaseObject(ByVal o As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        Catch ex As Exception
        Finally
            o = Nothing
        End Try

    End Sub
Random Solutions  
 
programming4us programming4us