title: excel vba code to check for a folder if it exists, if not create a folder
body: <p>I am a newbie in excel vba coding and trying to create pdf of a excel sheet range. My code works well in windows OS but somehow it doesn't work in Mac OS. The Code is as below:
  `</p>
  <pre><code>Sub GeneratePDF()
  Dim wsA As Worksheet
  Dim wbA As Workbook
  Dim strTime As String
  Dim strName As String
  Dim strPath As String
  Dim strFile As String
  Dim strPathFile As String
  Dim myFile As Variant
  Dim VelleName As String
  Dim SelectedRange As Range
  With ThisWorkbook.Worksheets(&quot;modulo&quot;)
  .Activate
  .Range(.Cells(1, 1), .Cells(33, 10)).Select
  Selection.Name = &quot;SelectedRange&quot;
  End With
  
  
  On Error GoTo errHandler
  
  Set wbA = ActiveWorkbook
  Set wsA = ActiveWorkbook.Worksheets(&quot;modulo&quot;)
  strTime = Format(Now(), &quot;ddmmyyyy\_hhmm&quot;)
  
  'get active workbook folder, if saved
  strPath = wbA.Path
  If strPath = &quot;&quot; Then
    strPath = Application.DefaultFilePath
  End If
  strPath = strPath &amp; &quot;\&quot;
  
  VelleName = ThisWorkbook.Worksheets(&quot;database&quot;).Range(&quot;B&quot; &amp; Desiredrow) &amp; &quot;_&quot; &amp; ThisWorkbook.Worksheets(&quot;database&quot;).Range(&quot;C&quot; &amp; Desiredrow)
  
  'replace spaces and periods in sheet name
  strName = Replace(VelleName, &quot; &quot;, &quot;_&quot;)
  strName = Replace(strName, &quot;.&quot;, &quot;_&quot;)
  strName = Replace(strName, &quot;-&quot;, &quot;_&quot;)
  strName = Replace(strName, &quot;/&quot;, &quot;_&quot;)
  
  'create default name for savng file
  strFile = strName &amp; &quot;_&quot; &amp; strTime &amp; &quot;.pdf&quot;
  
  strPathFile = strName &amp; &quot;_&quot; &amp; strTime
  
  
  ' select folder for file
  If Dir(strPath &amp; Application.PathSeparator &amp; &quot;forme&quot;, vbDirectory) = &quot;&quot; Then '&lt;== check if folder exists but its not detecting even though i had created a folder there.
      MkDir (ThisWorkbook.Path &amp; Application.PathSeparator &amp; &quot;forme&quot;) '&lt;== Create Folder and its not working for Mac OS.
  End If
  myFile = ThisWorkbook.Path &amp; Application.PathSeparator &amp; &quot;forme&quot; &amp; Application.PathSeparator &amp; strPathFile
  
  
  'export to PDF if a folder was selected
  If myFile &lt;&gt; &quot;False&quot; Then
  With wsA.PageSetup
      .Orientation = xlPortrait
      .PrintArea = &quot;SelectedRange&quot;
      .Zoom = False
      .FitToPagesTall = 1
      .FitToPagesWide = 1
  End With
      wsA.ExportAsFixedFormat _
          Type:=xlTypePDF, _
          Filename:=myFile, _
          Quality:=xlQualityStandard, _
          IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, _
          OpenAfterPublish:=False
          
      
      MsgBox &quot;Il file pdf è stato creato: &quot; _
        &amp; vbCrLf _
        &amp; myFile
  End If
  
  exitHandler:
      Exit Sub
  errHandler:
      MsgBox &quot;Impossibile creare il file pdf&quot;
      Resume exitHandler
  End Sub
  </code></pre>
  <p>`
  I have tried searching a lot on internet but havent found any source which specifically teaches vba coding in Mac OS. Moreover, i got only one link <a href="https://macexcel.com/examples/filesandfolders/makefolder/" rel="nofollow noreferrer">https://macexcel.com/examples/filesandfolders/makefolder/</a> but i dont think it would work as it should be only one line of command and the biggest issue i dont have Mac OS available now. So can somebody test my code change my command to make it compatible it with Mac OS</p>