run excel vb script based on combobox


I need some help. I have attached an excel sheet which has a combobox in sheet "MAIN" what I want it to do is look at a specific folder and list all the subfolders so that I can choose. Then when you press the IMPORT button I want the vb code behind the IMPORT button to use what I have chosen in the combobox to execute the script.

Can anyone help
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do it like this...

if Combo1.ListIndex = 0 then
'execute first script
elseif Combo1.ListIndex = 1 then
'execute second script
DarrenJacksonAuthor Commented:
HI kyodai thanks for helping.

The example you have given wont give me the ability for my combobox to look up folders within a subfolder.

I think you have misread my question
If you just need to get all subfolders then use something like this:

Dim FileSystem As Object
Dim HostFolder As String

HostFolder = "C:\"

Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)

Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Dim File
    For Each File In Folder.Files
        ' Operate on each file
End Sub
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

DarrenJacksonAuthor Commented:
Where would I place this within my attached excel file so that when I press on the drop down box within my combobox I get the folders within C:\  ?

Rory ArchibaldCommented:
I can't use your file due to the whole MS activex update issue, but basically you want code like this in the sheet:
Option Explicit

Const csROOT_FOLDER As String = "C:\Temp\Payroll\Week1\Exported\"

Private Sub ComboBox1_DropButtonClick()
    Dim n As Long
    Dim fdr As Object
    Dim aFiles()
    n = 1
    With CreateObject("Scripting.FileSystemObject").getfolder(csROOT_FOLDER)
        ReDim aFiles(1 To .subfolders.Count)
        For Each fdr In .subfolders
            aFiles(n) = fdr.Name
            n = n + 1
    End With
    ComboBox1.List = aFiles
End Sub

Private Sub CommandButton21_Click()
    If ComboBox1.ListIndex > -1 Then Call Import(csROOT_FOLDER & ComboBox1.Text)
End Sub

Open in new window

then change the import code to:
Public Sub Import(cPath As String)
    Dim strFile               As String
    Dim strData               As String
    Dim intFN                 As Integer
    Dim rng                   As Range
    Dim wks                   As Worksheet
    Dim vData                 As Variant
    Set wks = ActiveWorkbook.Worksheets("Import")
    Set rng = wks.Range("A1")
    strFile = Dir(cPath & "*.prn")
    Application.ScreenUpdating = False
    Do Until Len(strFile) = 0
        intFN = FreeFile
        Open cPath & strFile For Input As #intFN
        strData = Input(LOF(intFN), #intFN)
        Close intFN
        vData = Split(strData, vbCrLf)
        wks.Range(rng, rng.Offset(UBound(vData))).Value = WorksheetFunction.Transpose(vData)
        'rng.Value = Left(strFile, Len(strFile) - 4)
        Set rng = rng.Offset(1, 0)
        strFile = Dir()

    ActiveWorkbook.Worksheets("Import").Sort.SortFields.Add Key:=Range("A1:A18000" _
                                                                       ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Import").Sort
        .SetRange Range("A1:A18000")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
    End With

    Application.ScreenUpdating = True
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarrenJacksonAuthor Commented:
Rory this is perfect.

Thankyou for this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.