excuse my english, it is not good, but I hope you can understand me.
I have a problem with my VBA code that I use in Excel 2010 on windows without problems, but his not working on mac excel 2011.
I have changed from pc to mac and I don't know the mac system and I not can find the solution.
this is my code:
Private Sub Workbook_Open()
Dim strJahrgang As String
Dim strKlasse As String
Dim strNr As String
Dim mPath As String
Dim mFoto As String
mPath = ThisWorkbook.Path & "\Foto"
Application.ScreenUpdating = False
strJahrgang = Worksheets("Register").Range("D18")
strKlasse = Worksheets("Register").Range("E36")
' Schleife über alle Tabellenblätter
For i = 1 To Sheets.Count
' Tabellenname enthält "schüler_"
If InStr(Sheets(i).Name, "schüler_") > 0 Then
With Sheets(i)
' Nr. aus dem Tabellennamen
strNr = Format(Application.Substitute(.Name, "schüler_", ""), "00")
' Bildnamen zusammensetzen
mFoto = strJahrgang & " Klasse " & strKlasse & " - " & strNr & " " & _
.Cells(1, 5) & " " & .Cells(1, 8)
' in laufender Tabelle ist kein Bild vorhanden
If .Shapes.Count = 0 Then
' benötigtes Bild ist im Ornder vorhanden
If Dir(mPath & "\" & mFoto & ".jpg") <> "" Then
.Pictures.Insert (mPath & "\" & mFoto & ".jpg")
With .Pictures(.Pictures.Count)
.Top = Range("G5").Top
.Left = Range("G5").Left
.Height = Range("G5:G12").Height
.Width = Range("G5:J5").Width
End With
DoEvents
' Bild ist im Ordner nicht mehr vorhanden
Else
' Bild löschen
If .Shapes.Count > 0 Then .Shapes(1).Delete
End If
' in laufender Tabelle ist Bild vorhanden
Else
' wenn Bild im Ordner nicht merh vorhanden dann Bild löschen
If Dir(mPath & "\" & mFoto & ".jpg") = "" Then .Shapes(1).Delete
End If
mFoto = ""
End With
End If
Next i
Application.ScreenUpdating = True
End Sub
I have understand the mac use ":" where win use "/", so I think to must change "/" with ":".
my problem is a runtime error 68 on this line
If Dir(mPath & "\" & mFoto & ".jpg") <> "" Then
.Pictures.Insert (mPath & "\" & mFoto & ".jpg")
I think mac don't like the command "Dir" and can not find the directory "Foto". probably I must use a MacID but I don't know how I must use it and my english to understand it are to bad.
I use the code to insert automatically the photo from my students in the schoolregister. the code search the schoolyear on the page "register" cell e18 and the class in cell e36. in page "schüler_1", "schüler_2" etc. in the cells e1 he found the last name and h1 first name from evrey stundent. With this informations the code search in the order "Foto" that stay in the same place how the excelfile the right foto of the stundents and insert it on cells g5:j12. example "2014-15 klasse 1a - 01 Ciccio Bello"
I hope someone can please help me, thanks.
dan
This link should give you the information you require for file and folder operations on the Mac.
EDIT
#T J Noted and thank you
#muma I don't use the Mac for VBA much so may not be able to respond in great detail. However, from the link, it seems you require a function to test for files/folders. Apparently Mac has difficulty with long file names?
For your specific Q, the below test code and function should enable you to test whether a file or folder exists. The first argument in the function determines whether the check is for a file or folder: 1 = file, 2 = folder. You will need to provide your own path/filename in the function call. My example file name just happens to be 'Workbook2.csv' in the code below.
Sub macTest()
Dim isfl As Boolean
If FileOrFolderExistsOnMac(1, "<your disk name>:Users:<your user name>:Documents:Workbook2.csv") Then
MsgBox "Yes it exists"
Else
MsgBox "Sorry file doesn't exist"
End If
End Sub
Function FileOrFolderExistsOnMac(FileOrFolder As Long, FileOrFolderstr As String) As Boolean
'By Ron de Bruin
'30-July-2012
'Function to test whether a file or folder exist on a Mac.
'Uses AppleScript to avoid the problem with long file names.
Dim ScriptToCheckFileFolder As String
ScriptToCheckFileFolder = "tell application " & Chr(34) & "Finder" & Chr(34) & Chr(13)
If FileOrFolder = 1 Then
ScriptToCheckFileFolder = ScriptToCheckFileFolder & "exists file " & _
Chr(34) & FileOrFolderstr & Chr(34) & Chr(13)
Else
ScriptToCheckFileFolder = ScriptToCheckFileFolder & "exists folder " & _
Chr(34) & FileOrFolderstr & Chr(34) & Chr(13)
End If
ScriptToCheckFileFolder = ScriptToCheckFileFolder & "end tell" & Chr(13)
FileOrFolderExistsOnMac = MacScript(ScriptToCheckFileFolder)
End Function
Related
I have some code that copys and pastes the current worksheet to a blank new workbook, and then saves it depending on the values of some cells (stored in variables).
Specifically, these are Site, Client, and Date visited.
It all works fine with site and client, however when I include the date variable in the filename to save, it throws an error: Runtime error 76 - Path not found.
I'd appreciate any help/advise.
Sub Pastefile()
Dim client As String
Dim site As String
Dim visitdate As String
client = Range("B3").Value
site = Range("B23").Value
screeningdate = Range("B7").Value
Dim SrceFile
Dim DestFile
SrceFile = "C:\2013 Recieved Schedules\schedule template.xlsx"
DestFile = "C:\2013 Recieved Schedules" & "\" & client & " " & site & " " & visitdate & ".xlsx"
FileCopy SrceFile, DestFile
ActiveWindow.SmallScroll Down:=-12
Range("A1:I37").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-30
Workbooks.Open Filename:= _
"C:\Schedules\2013 Recieved Schedules" & "\" & client & " " & site & " " & visitdate & ".xlsx", UpdateLinks:= _
0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
When using dates in file names, you never want to rely on the default textual representation of the date, because that depends on the current locale.
You should store the date as date in the first place, and explicitly format it in a safe way for the file name:
Dim visitdate As Date
visitdate = Range("b7").Value
dim visitdate_text as string
visitdate_text = Format$(visitdate, "yyyy\-mm\-dd")
You might also consider removing any special characters like \ from your other values, such as client and site. Otherwise the problem might arise again.
Here is my suggestion of code rewrite:
Sub Pastefile()
Dim client As String
Dim site As String
Dim visitdate As String
client = Range("B3").Value
site = Range("B23").Value
visitdate = Range("B7").Value
Dim SrceFile
Dim DestFile
If IsDate(visitdate) Then
SrceFile = "C:\2013 Received Schedules\schedule template.xlsx"
DestFile = "C:\2013 Received Schedules" & "\" & Trim(client) & " " & Trim(site) & " " & Str(Format(Now(), "yyyymmdd")) & ".xlsx"
If Trim(Dir("C:\2013 Received Schedules\schedule template.xlsx")) <> "" Then
FileCopy SrceFile, DestFile
Else
MsgBox (SrceFile & " is not available in the folder")
GoTo EndCode
End If
Range("A1:I37").Select
Selection.Copy
Workbooks.Open Filename:= _
"C:\Schedules\2013 Received Schedules" & "\" & client & " " & site & " " & visitdate & ".xlsx", UpdateLinks:= 0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close
Else
MsgBox ("Please input the correct date in cell B7")
ActiveSheet.Range("B7").Activate
End If
EndCode:
End Sub
The code works without error and opens the application 7zip with folder but does not extract the text file.
Sub B_UnZip_Zip_File_Fixed()
Dim PathZipProgram As String, NameUnZipFolder As String
Dim FileNameZip As Variant, ShellStr As String
PathZipProgram = "C:\Program Files (x86)\7-Zip"
If Right(PathZipProgram, 1) <> "\" Then
PathZipProgram = PathZipProgram & "\"
End If
If Dir(PathZipProgram & "7zFM.exe") = "" Then
MsgBox "Please find your copy of 7z.exe and try again"
Exit Sub
End If
NameUnZipFolder = "C:\vba\"
FileNameZip = "C:\vba\zout.Gz"
Shell (PathZipProgram & "7zFM.exe e -aoa" _
& " " & Chr(34) & FileNameZip & Chr(34) _
& " -o" & " " & Chr(34) & NameUnZipFolder & "*.txt*")
MsgBox "Look in " & NameUnZipFolder & " for extracted files"
End Sub
I used Shell Script, but it doesn't extract the file.
I'm trying to search for a file, in d:\ folder with the name Division_Application_Partner.xlsx where Division Application and Partner are variables holding string values.
This is the code I gave:
Set WorkbookPath = Dir(path & Division + "_" + Application + "_" + TradingPartner + ".xlsx")`enter code here`
It throws an error saying " Compile Error: Type Mismtach "
Is the name of the file im giving wrong
Here's the code:
Dim WorkbookPath As WorkBook
Dim path as String
Division = Range("C11").Value
Application = Range("C15").Value
TradingPartner = Range("C19").Value
path = "d:\"
'MsgBox (path)
'MsgBox (Division)
'MsgBox (Application)
MsgBox (TradingPartner)
If Len(Dir(path & Division & "_" & Application & "_" & TradingPartner & ".xlsx")) = 0 Then
Set WorkbookPath = Division & "_" & Application & "_" & TradingPartner & ".xlsx"
End If
I tried concatenating using & like you suggested. Still it shows the same error.
You try assign string to object, this why you getting an error
Dim WorkbookPath As WorkBook
Better try
Dim myWkb as Workbook
Set myWkb = Workbooks.Open(your_concat_string)
and dont use reserved words
Application
Finally
Sub test()
Dim wkbExternWorkbook As Workbook
Dim strPath As String
Dim strDivision As String, strApplication As String, strTradingPartner As String
strDivision = Range("C11").Value
strApplication = Range("C15").Value
strTradingPartner = Range("C19").Value
strPath = "D:\"
If Len(Dir(strPath & strDivision & "_" & strApplication & "_" & strTradingPartner & ".xlsx")) <> 0 Then
Set wkbExternWorkbook = Workbooks.Open(strPath & strDivision & "_" & strApplication & "_" & strTradingPartner & ".xlsx")
End If
End Sub
I would start with using & exclusively for string concatenation. The use of + is primarily for adding numbers though it can concatenate strings. However, there are all sorts of caveats to that when using option strict and so forth, so you're better off using what was intended.
The other thing you should do is actually output all those variables before attempting to concatenate or pass them to Dir. Something like:
MsgBox "[" & path & "]"
repeated for all the others as well. The output of that may well point to the problem.
Try this:
Sub test()
Dim application As Variant
Dim division As Variant
Dim WorkbookPath As String
Dim tradingpartner As Variant
Dim path As String
division = Range("C11").Value
application = Range("C15").Value
tradingpartner = Range("C19").Value
path = "d:\"
'MsgBox (path)
'MsgBox (Division)
'MsgBox (Application)
MsgBox (tradingpartner)
If Len(Dir(path & division & "_" & application & "_" & tradingpartner & ".xlsx")) = 0 Then
Workbooks.Add
ActiveWorkbook.SaveAs division & "_" & application & "_" & tradingpartner & ".xlsx"
End If
End Sub
You would first add the workbook and then save it using the created name.
I have the following question. I use a file to log assets (laptops, desktops etc) into certain folders, like deployed, stock, repair and hotswap.
I made some buttons in it which work all fine. One button called deployed, when I save the sheet with this button it saves it with EU IMAC, serial number and date as XLMS file.
I like to change the code from this button, so that when I save a sheet as deployed it automatically delete the XLMS file with serial number and name in the folder stock.
Below the codes for all the save buttons and it's button 61 that needs to be fixed, the others I will change afterwards. The code is form other forum, but with no success.
Sub Button60_Click()
Range("A1:G68").PrintOut
End Sub
Sub Button51_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Hotswap\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Hotswap -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub Button53_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Returned to stock\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Return to stock - " & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub awaitwuhan_Click()
ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\To repair\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & "- Repair -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub
Sub Button61_Click()
p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"
'opslaan
s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
ActiveWorkbook.SaveAs p & s_name
'verwijderen
d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then Kill p & d_name
End Sub
First, give your buttons meaningful names, that is such a garbled mess to try and determine what button60 is or does.
Second You need to use the file system object from Microsoft Scripting Library (add a reference in excel to this dll scrrun.dll) then you can check to see if the file exists and delete it
Sub Button61_Click()
p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"
'opslaan
s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
ActiveWorkbook.SaveAs p & s_name
'verwijderen
d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
'create the file system object
Dim fso As FileSystemObject
Set fso = New FileSystemObject
'make sure the file exists first
If fso.FileExists(p & d_name) = True Then
If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then
fso.DeleteFile p & d_name, True
End If
End If
'free the memory
Set fso = Nothing
End Sub
Code is below which was working fine, now does not seem to like the ".xlsm" section. problem is on all PCs. I tried using the Filename= and a few variants and have pinned it down to "." (period) that it does not accept in ".xlsm", delete the period and it is fine but then lands in SharePoint as an unknown file (with no file extension). Any advice appreciated!
Sub SUBMIT()
Dim FName As String
FName = Range("E3").Text
FDate = Range("I3").Text
If Range("E3") = "" Then
MsgBox "Please Enter Your Name"
Range("E3").Select
ElseIf Range("I3") = "" Then
MsgBox "Please Enter Fortnight Ending Date"
Range("I3").Select
ElseIf Range("I3") <> "" Then
If MsgBox("Are you sure? (Have you entered your supervisor(s) and Fortnight End Date in the top panel ?", vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.SaveAs ("https://*****.sharepoint.com/corp/payroll/Timesheets" & FName & " " & FDate & " " & "Timesheet" & "xls")
MsgBox "Timesheet Submitted"
End If
End Sub
You should supply the file path without the extension and use the FileFormat parameter of the ActiveWorkbook.SaveAs() function.
In your case, you should change the row to this
ActiveWorkbook.SaveAs ("https://*****.sharepoint.com/corp/payroll/Timesheets" & FName & " " & FDate & " " & "Timesheet"), 52
The 52 I added at the end is the value for xlOpenXMLWorkbookMacroEnabled as described here
The code below works well for me on Win7 Excel2013
Sub StackOverflow()
ActiveWorkbook.SaveAs "C:\Temp\myfile", 52
End Sub