0

So i have a form that creates a chart based on a different file. I'm trying to write code that does this: User clicks "Get Data"

Form opens

User clicks the directory from which to get files

Code opens directory and populates a combobox in form with file names beginning with gmn

User clicks the files they want to get data from

macro is called and all data is added

I'm not any good with vba or excel but hey I'm learning. So the problem is that I have idea what to use to get the files? I tried looking at other post but they are just getting specific files. Any help is greatly appreciated! And if more info is needed let me know!

Code and Pics:

Private Sub ComboBoxDir_Change()

'populate our directories
Dim DirNow As String
DirNow = Dir("\\na.luk.com\wooster\DATA\NL-LUS-E\EAD\Tom_Freshly\SAE2\TestsDone\GMN10055\Ford-A oil\Inspection\" + Me.ComboBoxDir + "\*", vbNormal)

'loop to fill up pull down
Do While DirNow <> ""

    'add items to combo
    UserFormDataa.ComboBoxFiles.AddItem (DirNow)

    'get next dir
    DirNow = Dir()

Loop

'Now add files
Call GetFilesFromDirect("")



End Sub
Private Sub GetFilesFromDirect(DirectNow As String)

Dim file As Variant
   file = Dir("DirectNow")
   While (file <> "")
      If InStr(file, "Gmn*") > 0 Then
         ComboBoxFiles.AddItem (file)
      End If
     file = Dir
  Wend

End Sub

Private Sub ComboBoxFiles_Change()

End Sub

Private Sub GetSheets_Click()

   Call GetData

End Sub

enter image description here

5
  • see the GetSaveAsFileName method. Commented Jun 27, 2016 at 20:02
  • If I'm guessing right, you need to retrieve the file items in the Do While loop right? Commented Jun 27, 2016 at 20:08
  • Yes @Sgdva I sort of have something going. I can get the directories into my directories combo box. Now i'm trying to code so that when user clicks on a directory, all the files in that directory are added to (Files) Commented Jun 27, 2016 at 20:12
  • If this is a coding exercise, fine. Otherwise, you're reinventing the wheel. Commented Jun 27, 2016 at 20:13
  • I'm just trying to get the file names that start with gmn from a directory that the user clicks. Then add those files to a combobox. I see how this would work but I need it to only display the files in that directory Commented Jun 27, 2016 at 20:27

1 Answer 1

1

This should populate with all the elements that begin with "gmn" within DirNow Address

...
Do While DirNow <> ""
Dim oFSO as Object
Dim FileItem  as Variant
Dim oFolder as Object
Set oFSO = CreateObject("Scripting.FileSystemObject") 'library needed to do stuff related to file management
Set oFolder = oFSO.GetFolder(DirNow)
UserFormDataa.ComboBoxFiles.Clear 'no previous data needed (if any)
     For each FileItem in oFolder.Files
    'add items to combo
    If Instr(FileItem.Name,"gmn") >0 Then UserFormDataa.ComboBoxFiles.AddItem (FileItem.Name)
    'I'd ignore case "GMN" won't be added) If Instr(lCase(FileItem.Name)...
    Next FileItem 
Loop
...

OT: While I'm partially agree that File Dialog may be a better approach, I guess this kind of works too to make it easier to the users. I don't understand why comments suggested GetSaveAsFileName since you state there may be multiple user selections I'd go for GetOpenFilename or, it could be better to go with GetFolder

Sign up to request clarification or add additional context in comments.

3 Comments

So it gave me an error :(. But what i did was i wrote the code for the user to click on an item in the Directory ComboBox. That item is stored and passed to a sub called GetFilesFromDirect(DirectNow as String) I took your code, put it in this sub, and changed every DirNow to DirectNow. Should I not do this?
That's the problem "DirNow" should be the path to the files
Got it.Just had to add the file path to DirNow. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.