run excel vb script based on combobox

Guys,

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
Book1.xlsm
DarrenJacksonAsked:
Who is Participating?
 
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
        Next
    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()
    Loop

    Columns("A:A").Select
    ActiveWorkbook.Worksheets("Import").Sort.SortFields.Clear
    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
        .Apply
    End With
    Range("A1").Select


    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
kyodaiCommented:
Do it like this...

if Combo1.ListIndex = 0 then
'execute first script
RunScriptOne
elseif Combo1.ListIndex = 1 then
'execute second script
RunScriptTwo
...
0
 
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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kyodaiCommented:
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
    Next
    Dim File
    For Each File In Folder.Files
        ' Operate on each file
    Next
End Sub
0
 
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:\  ?

Regards
0
 
DarrenJacksonAuthor Commented:
Rory this is perfect.

Thankyou for this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.