Dir() function not working in Mac Excel 2011 VBA Dir() function not working in Mac Excel 2011 VBA vba vba

Dir() function not working in Mac Excel 2011 VBA


Gianna, you cannot use DIR like that in VBA-EXCEL 2011. I mean the wildcards are not supported. You have to use MACID for this purpose.

See this code sample (TRIED AND TESTED)

Sub Sample()    MyDir = ActiveWorkbook.Path    strPath = MyDir & ":"    strFile = Dir(strPath, MacID("TEXT"))    'Loop through each file in the folder    Do While Len(strFile) > 0        If Right(strFile, 3) = "csv" Then            Debug.Print strFile        End If        strFile = Dir        LoopEnd Sub

See this link for more details on MACID

Topic: MacID Function

Link: http://office.microsoft.com/en-us/access-help/macid-function-HA001228879.aspx

EDIT:

In case that link ever dies which I doubt, here is an extract.

MacID Function

Used on the Macintosh to convert a 4-character constant to a value that may be used by Dir, Kill, Shell, and AppActivate.

Syntax

MacID(constant)

The required constant argument consists of 4 characters used to specify a resource type, file type, application signature, or Apple Event, for example, TEXT, OBIN, "XLS5" for Excel files ("XLS8" for Excel 97), Microsoft Word uses "W6BN" ("W8BN" for Word 97), and so on.

Remarks

MacID is used with Dir and Kill to specify a Macintosh file type. Since the Macintosh does not support * and ? as wildcards, you can use a four-character constant instead to identify groups of files. For example, the following statement returns TEXT type files from the current folder:

Dir("SomePath", MacID("TEXT"))

MacID is used with Shell and AppActivate to specify an application using the application's unique signature.

HTH


If Dir(outputFileName) <> "" ThenDim ansans = MsgBox("File already exists.Do you wish to continue(the previous file will be    deleted)?", vbYesNo)If ans = vbNo ThenExit SubElseKill outputFileNameEnd IfEnd IfFor listitem = 0 To List6.ListCount() - 1


For the answer above, it worked for me when I took out the "TEXT" in MacID:

Sub LoopThruFiles()    Dim mydir As String    Dim foldercount As Integer    Dim Subjectnum As String    Dim strpath As String    Dim strfile As String    ChDir "HD:Main Folder:"    mydir = "HD:Main Folder:"    SecondaryFolder = "Folder 01:"    strpath = mydir & SecondaryFolder    strfile = Dir(strpath)    'Loop through each file in the folder    Do While Len(strfile) > 0     If Right(strfile, 3) = "cef" Then        MsgBox (strfile)        End If        strfile = Dir    LoopEnd Sub