• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

ComboBox dropdown select various window folders|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / ComboBox dropdown select various window folders|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosComboBox dropdown select various wi…
sp_PrintTopic sp_TopicIcon
ComboBox dropdown select various window folders
Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
1
June 24, 2021 - 4:16 pm
sp_Permalink sp_Print

Hi,

 

I was working on a code where I created a combobox so that I can select various windows folders but comes up as Run-time error 438 object doesnt syupport this property or method.

Private Sub UserForm_Initialize()
Dim myfso As FileSystemObject, myfolder As Object, myfile As Object

Me.ComboBox1.Clear

Set myfso = New Scripting.FileSystemObject
Set myfolder = myfso.GetFolder("C:\Andy")

For Each myfile In myfolder.File
Me.ComboBox1.AddItem myfile.Name
Next myfile

End Sub

Can you check if I made some mistake here???

Is there another way in a combo box dropdown to select various folders on a windows drive

I got this code from

Thanks

 

Steve

sp_AnswersTopicSeeAnswer See Answer
Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
2
June 25, 2021 - 2:34 am
sp_Permalink sp_Print

Hi everyone, it's been a while, sorry, a lot of work

Hi Steven 😉

you want to add the name of files that are found in a folder, to a combobox, right?

 

example:

 

code in userform: (userform with command button & combobox)

 

Option Explicit

Private Sub CommandButton4_Click()

Call allFilesInFolder ' (MACROS IN MODULE)

End Sub

 

code in module

 

Option Explicit

Public Sub allFilesInFolder()

Dim checkForFolder$

checkForFolder = checkDir
If checkForFolder = "" Then MsgBox "Critical error, unable to complete!", vbCritical, "Critical error": Exit Sub

listAllFilesInFolder checkForFolder, True

End Sub

Private Sub listAllFilesInFolder(folderName As String, subfolderName As Boolean)

Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder
Dim SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim str$

Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(folderName)

UserForm1.ComboBox1.Clear

For Each FileItem In SourceFolder.Files
     With UserForm1.ComboBox1
         ''--- file name and extension
         ' str = FileItem.Name
         ' .AddItem FileItem.Name
         ''--- only file name & remove extension
         str = removeLastOne(FileItem.Name)
         .AddItem str
     End With
Next FileItem

If subfolderName Then
     For Each SubFolder In SourceFolder.SubFolders
         listAllFilesInFolder SubFolder.Path, True
     Next SubFolder
End If

If Not SourceFolder Is Nothing Then Set SourceFolder = Nothing
If Not FSO Is Nothing Then Set FSO = Nothing

End Sub

Private Function checkDir()

Dim objShell, xFolder, xPath, xCheck

Set objShell = CreateObject("Shell.Application")
Set xFolder = objShell.BrowseForFolder(&H0&, "Please confirm: Select the folder with the files!", &H1&)

On Error Resume Next
xPath = xFolder.ParentFolder.ParseName(xFolder.Title).Path & ""

If xFolder.Title = "Bureau" Then
     xPath = "C:WindowsBureau"
End If

If xFolder.Title = "" Then
     xPath = ""
End If

xCheck = InStr(xFolder.Title, ":")

If xCheck > 0 Then
     xPath = VBA.Mid(xFolder.Title, xCheck - 1, 2) & ""
End If

checkDir = xPath

If Not objShell Is Nothing Then Set objShell = Nothing
If Not xFolder Is Nothing Then Set xFolder = Nothing

End Function

Private Function removeLastOne(str As String) As String

Const character As String = "."
Dim Position As Long

If VBA.InStr(str, character) = 0 Then
     removeLastOne = str
     Exit Function
End If

Position = VBA.InStrRev(str, character)
removeLastOne = VBA.Mid(str, 1, Position - 1)

End Function

 

Regards Steven

 

Miguel

Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
3
June 25, 2021 - 9:13 pm
sp_Permalink sp_Print

Hi Steven, sorry I made a macro error!!!

In Private Sub listAllFilesInFolder(folderName As String, subfolderName As Boolean), removes this part: UserForm1.ComboBox1.Clear and puts it in:

Private Sub CommandButton4_Click() before:Call allFilesInFolder ' (MACROS IN MODULE)

 

with my error it is not possible to add the files from the subfolders to the combobox and erases everything!!!

sry

 

Regards

 

Miguel

sp_AnswersTopicAnswer
Answers Post
Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
4
June 26, 2021 - 5:26 pm
sp_Permalink sp_Print

Thanks for that Miguel,

 

I have to do that after out lockdown in Sydney.

 

Steve

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Andy Kirby, Ben Hughes, Alison West, Jill Niemeier, Laxmi Praveen, Francis Drouillard
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
yashal minahil
Oluwadamilola Ogun
Yannik H
dectator mang
Francis Drouillard
Orlando Inocente
Jovitha Clemence
Maloxat Axmatovna
Ricardo Freitas
Marko Meglic
Forum Stats:
Groups: 3
Forums: 24
Topics: 6201
Posts: 27185

 

Member Stats:
Guest Posters: 49
Members: 31861
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.