Unable to SaveCopyAs to proper path using vba:
ThisWorkbook.SaveCopyAs Filename:=ThisWorkbook.Path & "Rounds " & Month(Sheet4.Cells(1, 2)) & " - " & Year(Sheet4.Cells(1, 2))
The code is saving just short of the file path. Folder is located on a folder on the desktop but the new file is being saved to the desktop.
Path returns the folder the workbook is saved under. Sounds like you want:
ThisWorkbook.SaveCopyAs Filename:=ThisWorkbook.Path & "\Rounds " & Month(Sheet4.Cells(1, 2)) & " - " & Year(Sheet4.Cells(1, 2))
If you run this:
Sub TestMe()
Debug.Print ThisWorkbook.Path
End Sub
you would see something like: C:\Users\Username\Desktop in the immediate window.
Thus, if you run your code like this:
Debug.Print ThisWorkbook.Path & "Rounds " & Month(Sheet4.Cells(1, 2))
You would most probably see something like this:
C:\Users\Username\DesktopRounds 12 and this is not what you need. There is obviously a / missing after the Desktop. Thus, put it there and try further:
ThisWorkbook.SaveCopyAs Filename:=ThisWorkbook.Path & "\Rounds " & Month(Sheet4.Cells(1, 2)) & " - " & Year(Sheet4.Cells(1, 2))
Related
I have a series of files with names that follow a naming convention that includes a week ending date.
For example: 2020 Field - PP 28 - 07-05.xlsm
I have a cell that increments this value and acts as a hyperlink to the next file and one that hyperlinks to the previous file. This works flawlessly. The only drawback is that you end up with all the files still open draining resources.
Sometimes I do want several open at once but I would like to have the option to close the originating file when moving to the next file.
I was thinking to create a button that calls VBA code to follow the hyperlink and close the file, while the existing cell will act as it does and open the file without closing the current one. One of each for Next and one of each for Previous
I have tried to dabble a bit with some code but I don't know enough VB to blow my nose.
I came up with this, but since it doesn't work I know there will be more to it:
Sub CommandButton1()
Workbooks.Open Filename:=ActiveWorkbook.Sheets(1).Cells(1, 10).Value
ActiveWorkbook.Close savechanges:=True
End Sub
In case it is needed, the formula in the cell is this:
=HYPERLINK(CONCATENATE(YEAR(C1)," Field - PP ",IF((WEEKNUM(C1+14,2))<10,0,""),WEEKNUM(C1+14,2)," - ",IF(MONTH(C1+7)<10,"0"&MONTH(C1+7),MONTH(C1+7)),"-",IF(DAY(C1+7)<10,"0"&DAY(C1+7),DAY(C1+7)),".xlsm"),"Next PP")
C1 being the cell with the current files week ending date.
Some help would be greatly appreciated!
Try something like this:
Sub CommandButton1()
Dim dt, f
dt = ActiveSheet.Range("C1").Value
f = Format(dt, "yyyy") & " Field - PP " & Format(dt + 14, "ww") & _
" - " & Format(dt + 7, "mm") & " - " & Format(dt + 7, "dd") & ".xlsm"
Debug.Print "Opening: " & ThisWorkbook.Path & "\" & f
Workbooks.Open ThisWorkbook.Path & "\" & f
ThisWorkbook.Close savechanges:=True
End Sub
I have VBA code set up so that when the macro is run, a folder is created on a network drive, and then should open the created folder. However, it will only ever open the documents folder instead.
If I change the code to the below it correctly opens the folder in question (in this case the Applications folder) - how can I get it to open the newly created one instead?
If .Value = "Section 50" And Cells(i, 6) <> "" Then
dirName = Cells(4, i).Values
MkDir ("\\richmond\data\Environment\Traffic & Transport\T&T - NetworkManagement\Street Works 2019-20\Licences\Section 50\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")")
Call Shell("explorer.exe" & " " & "\\richmond\data\Environment\Traffic & Transport\T&T - NetworkManagement\Street Works 2019-20\Licences\Section 50\Applications 2019-20\", vbNormalFocus)
This code is part of a larger script so I've just shown the necessary section.
If .Value = "Section 50" And Cells(i, 6) <> "" Then
dirName = Cells(4, i).Values
MkDir ("\\richmond\data\Environment\Traffic & Transport\T&T - NetworkManagement\Street Works 2019-20\Licences\Section 50\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")")
Call Shell("explorer.exe" & " " & "\\richmond\data\Environment\Traffic & Transport\T&T - NetworkManagement\Street Works 2019-20\Licences\Section 50\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")", vbNormalFocus)
If you use Ampersands(&) in your folders path and don't escape them with^ or double-quotes it makes the command-line think that a new command is following the ampersand as it is used to run multiple commands on a single line.
Your documents folder is opened becauseexplorer \\is a explorer command-line switch.
For testing you can open a command-line (cmd.exe) and try:
explorer.exe \\richmond\data\Environment\Traffic & Transport\
what should showcommand Transport not foundas the ampersand concats multiple commands and the shell thinksTransportis the next one, but didn't find it.
Now try
explorer.exe "\\richmond\data\Environment\Traffic & Transport\"
or
explorer.exe \\richmond\data\Environment\Traffic ^& Transport\
Both commands are working now as the ampersand is escaped.
As alternative you can use Workbook.FollowHyperlink what escapes the ampersand automatic. Try
ThisWorkbook.FollowHyperlink "\\richmond\data\Environment\Traffic & Transport\"
on my excel sheet the user can choose some video clips and arrange in different order to play as a vlc playlist. Unfortunately it can’t be guaranteed that the video file names haven’t any blanks.
To build the vlc playlist I use successfully:
Dim PL
PL = Shell("C:\Program Files\VideoLAN\VLC\VLC.exe " & certainPath & "\Movie6.flv" & " " & certainPath & "\Movie7.flv" & " " & certainPath & "\Movie8.flv ", 1)
'using "\Movie 6.flv" 'doesn't work of course
'using "\'Movie 6.flv'" 'doesn't work aswell
Is there another way to encapsulate file name with blanks?
thankfull for a hint.
Assuming that certainPath folder end with \ ( es. "C:\" ), this should work:
Dim PL
PL = Shell(chr(34) & "c:\Program Files\VideoLAN\VLC\vlc.exe " & chr(34) & chr(34) & certainPath & "Movie 6.flv" & chr(34) & " " & chr(34) & certainPath & "Movie 7.flv" & chr(34))
CHR is a function to returns the character from the ASCII table (the quotes in this case).
ok so this is a short peace in a big workbook... All i am trying to do is tell it a certain place to save.
ActiveWorkbook.SaveCopyAs _
FileName:=ActiveWorkbook.Path "\OLD " & Range("D1").Value & ".XLSM"
This does exactly as it is supposed to however, i want to say basically
"activeworkbook.path" plus give it one further step and designate a folder called "old" that it will go to.
in essence it would look like this
\documents\test\my-file.xlsm
to this
\documents\test\OLD\my-file.xlsm
any hints?
You have a space in "\OLD ", and you are not closing off \OLD to be a folder.
The line should look like
ActiveWorkbook.SaveCopyAs _
FileName:=ActiveWorkbook.Path & "\OLD\" & Range("D1").Value & ".XLSM"
I would also strongly consider qualifying your Range("D1") with your worksheet.
Dim fileNameRng as range
Set fileNameRng = thisworkbook.worksheets("Sheet1").Range("D1")
ActiveWorkbook.SaveCopyAs _
FileName:=ActiveWorkbook.Path & "\OLD\" & fileNameRng.Value & ".XLSM"
There are numerous Q&A's for Excel WINDOWS, but none for Excel MAC that specifically answer this question.
Using Excel Mac 2011 VBA, how do I open a non-Excel file using the file's default app?
After much research and testing, I figured out the answer. See below.
This works running Microsoft Excel 14.7.2 (14.7.2) on macOS 10.12.6.
```vb
Sub open_file()
Dim scriptStr As String
Dim hfsPath As String
hfsPath = "~:Documents:Test File To Open From Excel.txt"
'--- Create AppleScript to Open Non-Excel File ---
' (Note: You cannot use POSIX path or POSIX commands)
' So, I have allowed for the tilde in a HFS path
' to mean the same as in a POSIX path: Users Home Folder
scriptStr = "set hfsPath to """ & hfsPath & """" & vbNewLine & _
"if (hfsPath starts with ""~"") then" & vbNewLine & _
" set homePath to (path to home folder) as text" & vbNewLine & _
" set hfsPath to homePath & (text 3 thru -1 of hfsPath)" & vbNewLine & _
"end if" & vbNewLine & _
"tell application ""Finder"" to open file hfsPath" & vbNewLine & _
"return hfsPath"
Debug.Print scriptStr
'--- Execute AppleScript to Open Non-Excel File ---
hfsPath = MacScript(scriptStr)
Debug.Print hfsPath
End Sub
```
For Excel Windows, see
How can Excel Windows VBA open file using default application