Forum Discussion

excelnoob298's avatar
excelnoob298
Copper Contributor
Mar 09, 2020

Can you guys review this code?

Hi guys 

I have a code which i needed some help with. I have searched the internett but with no luck. so i hope this community can help me with this.

The code which written down is only working on windows Pc and not Mac. so my question is; what can i change here for it to work on both systems? im new to VBA coding so its would be helpfull if you guys can point out what i need to change.

I wrote this code on windows laptop and i want it to work on both mac and windows, as my colleagues have mac/windows laptops and they also use this file.

 

ActiveSheet.Next.Select
Cells.Select
Range("F3").Activate
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Dim Path1 As String
Dim Path2 As String
Dim sheet1 As String
Dim fpathname1 As String

Path1 = "C:\Users\"username"\Dropbox\"workers"\"
Path2 = Range("F3")
sheet1 = Range("B3")
fpathname1 = Path1 & Path2 & "\" & sheet1 & ".xlsx"
ActiveWorkbook.SaveAs filename:=fpathname1, FileFormat:=51
ActiveWorkbook.Close

 

 

And the second code after this;

Windows("Filename.xlsm").Activate
Sheets("Sheet1").Select

Dim filename As String
Dim path As String
Application.DisplayAlerts = False
path = "C:\Users\"username"\Dropbox\"workers"\2020\"
filename = Range("A1").Value & ".xlsx"
ActiveWorkbook.SaveAs path & filename, xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Close

 

 

so this code save the file to its right folder name. but like over i want this code to run on both mac and windows. 

 

And my second question is that if anyone can help me with creating a which will work like this; when you run the code over. before or after it has saved the file, the same file it will send to the workers. the next thing is that there are 3 company emails i have to send from, so the code need to know when to use which email. if the value have to be in cell for it to know which email it has to send from then even better. 

 

Thanks 🙂

 

 

 

8 Replies

    • excelnoob298's avatar
      excelnoob298
      Copper Contributor

      JKPieterse so i found this on website; 

              #If Mac Then

               [Mac specific code here

               #Else

               [Windows specific code here]

               #End If

      End sub

       

      i wrote my windows code between the windows and #endif line. Now what i didnt understand is when i run this code workbook close but not excel. before writing this code thw whole excel file closed. what can i add here to close the whole excel file and not just the workbook? i have searched on the internett to find out what #endif is, and found nothing. could you explain it and if i need to write anything under the #endif or not?

       

      Thanks

      • JKPieterse's avatar
        JKPieterse
        Silver Contributor
        The # signs are compiler directives. VBA Only compiles and runs the relevant portion(s).
        For the code snippet you posted:
        If run on a Mac, ONLY the bit between "#If Mac Then" and "#Else" is compiled and run.
        If run on a Windows, ONLY the bit between "#Else" and "#End If" is compiled and run.