0

It's my first time asking something in this forum, i have this code that adds some files to a combobox in a User Form, but the thing is i only need to add the .xlsm files, but the code i came up with adds me every file, how can I do something like that?

Here's my code:

Dim Pathh As String
Dim fila As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Path = "Z:\Primera Inspección\" & Alertas_Mes.Controls("Label" & i).Caption
Set carpeta = fso.getfolder(Path)
If carpeta <> Pathh Then GoTo Sig
Set ficheros = carpeta.Files  
For Each ficheros In ficheros

    b = ficheros.Name
    Alertas_Mes.Controls("Combobox" & i).AddItem b
Next ficheros

2 Answers2

0

Should be something as simple as putting in an If statement

Dim Pathh As String
Dim fila As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Path = "Z:\Primera Inspección\" & Alertas_Mes.Controls("Label" & i).Caption
Set carpeta = fso.getfolder(Path)
If carpeta <> Pathh Then GoTo Sig
Set ficheros = carpeta.Files  
For Each ficheros In ficheros

    b = ficheros.Name
    if(b like "*.xlsm") then
        Alertas_Mes.Controls("Combobox" & i).AddItem b
    end if
Next ficheros
ClintB
  • 509
  • 3
  • 6
  • I tried this code and it worked as a charm, i knew i had to add an if statement, but i couldn't figure out how to ask for a xlsm file – Jake.Casillas May 15 '17 at 14:13
0

Check the file extension:

For Each ficheros In ficheros
    b = ficheros.Name
    If Right(b, 5) = ".xlsm" Then Alertas_Mes.Controls("Combobox" & i).AddItem b
Next ficheros
Kostas K.
  • 8,293
  • 2
  • 22
  • 28