trying to make a SaveAs macro to make my life easier


Posted by Zac on January 04, 2002 10:38 AM

Public Sub SaveAsB8()
ThisFile = Range("B8").Value
CurrentMonth = Range("F9").Value
CurrentJob = Range("B6").Value
FinalInfo = ("C:\Lab Analysis\" + CurrentMonth + "\" + CurrentJob + "\" + ThisFile)
ActiveWorkbook.SaveAs Filename:=FinalInfo
End Sub

basically, I want it to use 3 different cells for info to make the entire filename to a specific directory. I am working off just a few things I have seen here and about 1 hour in learning macros. Any help would be appreciated, comes back with a runtime error 13
(Type mismatch)

thanks!

Posted by Tom Urtis on January 04, 2002 10:48 AM

I'm not sure if you know the file path already, and you need to name the newly saved as file per values in cells. If I understand you correctly, try this one line of code.

ActiveWorkbook.SaveAs ("C:\Your\File\Path\" & Sheet1.Range("B8").Value & " " & Sheet1.Range("F9").Value & " " & Sheet1.Range("B6").Value & ".xls")


If I misunderstood you, sorry, and please repost.

Tom Urtis

Posted by Zac on January 04, 2002 11:29 AM

after fooling around with it for a little bit, I got it to save a basic file in a directory that already exists. Is there a way if make the macro check to see if a directory exists and if not, create that directory?

thanks for your help :)

Posted by Ivan F Moala on January 04, 2002 3:53 PM

Zac, as you ae new to macros it's a good idea to start with good practice habits....have a look
@ a documented solution to your Q. Repost if unsure................


Option Explicit
'The Option Explicit statement forces explicit declaration of all variables.
'Attempting to use an undeclared variable causes an error at compile time.
'It is a good way to ensure your coding is OK
'The Option Explicit statement is used at the module level only.

'No need to declare it Public as your routine
'by default is public, unless it is in a private module
Sub SaveAsB8()
Dim CDir As String
Dim ThisFile As String
Dim CurrentMonth As String
Dim CurrentJob As String
Dim FinalInfo As String
Dim msg As String

ThisFile = Range("B8").Value
CurrentMonth = Range("F9").Value
CurrentJob = Range("B6").Value

'Set your last active dir
'This is so that you can restore
'back to it when the macros finish

CDir = CurDir

'Always good to put error routines in for the unexpected!
'Now error check your data Input Range
If Application.WorksheetFunction.Or(ThisFile = "", CurrentMonth = "", CurrentJob = "") Then GoTo ErrName

'now check if Directory exists!
If Not DirExists("C:\Lab Analysis\" & CurrentMonth) Then
'Create 1st Dir = CurrentMonth
'Note: If Dir name is an invalid character then
'an error is generated [Path not found] so.....
On Error GoTo ErrDirCreate
MkDir ("C:\Lab Analysis\" & CurrentMonth)
'Obviously if the 1st Dir is not present then
'the 2nd level is not present so....Create the Dir
MkDir ("C:\Lab Analysis\" & CurrentMonth & "\" & CurrentJob)
End If

FinalInfo = ("C:\Lab Analysis\" + CurrentMonth + "\" + CurrentJob + "\" + ThisFile)

'Handle unexpected errors
On Error GoTo ErrFile
'Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=FinalInfo

'Now restore your last Current Dir setting
ChDrive ("C:\")
ChDir (CDir)

Exit Sub
ErrName:
MsgBox "Your data range MUST have valid data"

Exit Sub
ErrDirCreate:
'Note put this error routine here
'rather then using the general one
'as it will tell you where it was generated from
MsgBox "Error trying to create Directory." & vbCr & _
"Check your range inputs for valid Dir names" & vbCr & vbCr & _
"C:\Lab Analysis\" & CurrentMonth & vbCr & _
"OR" & vbCr & _
"C:\Lab Analysis\" & CurrentMonth & "\" & CurrentJob, _
vbCritical

Exit Sub
ErrFile:
'Build your general File Error msg
msg = "Error#:=" & Err.Number & vbCr
msg = msg & "Error message:=" & Err.Description & vbCr

MsgBox msg, vbMsgBoxHelpButton, "File error", Err.HelpFile, Err.HelpContext

End Sub


Function DirExists(Name As String) As Boolean
DirExists = True
On Error Resume Next
ChDir (Name)
If Err Then DirExists = Not DirExists
On Error GoTo 0
End Function

HTH

Ivan

Posted by Tom Urtis on January 04, 2002 9:06 PM

Ivan: Thank you, I didn't notice that follow up question til now. (nt)



Posted by Ivan F Moala on January 05, 2002 1:46 AM

Tom, no problem (nt)