+ Reply to Thread
Results 1 to 3 of 3

Saving a file to new folder dependant on the first letter of the file name.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-13-2012
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    2

    Saving a file to new folder dependant on the first letter of the file name.

    Hi

    I'm trying to write a macro which will enable me to save a file to a specific folder depending on the first letter of the filename. The code also creates a folder if none already exists. The code below is what I have managed so far but I keep getting a Run-time error '438' I think when I try and define the fchoose variable. Any help would be greatly appreciated.

    Function dirExists(dirAndPath) As Boolean
    Dim tempVar
    On Error Resume Next
    tempVar = Dir(dirAndPath & "\*.*", vbDirectory)
    If Err = 0 And tempVar <> "" Then
        dirExists = True
    End If
    On Error GoTo 0
    End Function
    
    Sub SaveMe()
    Dim fname As String
    Dim Dname As String
    Dim fchoose As Variable
    
    Dim A_to_C As String
    Dim D_to_G As String
    Dim H_to_K As String
    Dim L_to_O As String
    Dim P_to_R As String
    Dim S_to_T As String
    Dim U_to_Z As String
    
    A_to_C = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\A - C\"
    D_to_G = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\D - G\"
    H_to_K = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\H - K\"
    L_to_O = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\L - O\"
    P_to_R = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\P - R\"
    S_to_T = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\S - T\"
    U_to_Z = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\U - Z\"
    
    fchoose = Left(Front_Sheet.Range("C2").Characters.Value, 1)
    
    If fchoose = "a" Or fchoose = "b" Or fchoose = "c" Then
    
    
    If Not dirExists(A_to_C & Front_Sheet.Range("C2").Value) Then MkDir A_to_C & Front_Sheet.Range("C2").Value
    Dname = A_to_C & Front_Sheet.Range("C2").Value & "\"
    fname = Front_Sheet.Range("C2").Value & Front_Sheet.Range("C6").Value & ".xlsm"
    ThisWorkbook.SaveAs Filename:=Dname & fname
    
    End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Saving a file to new folder dependant on the first letter of the file name.

    Dim fchoose As string
    fchoose = Left(Front_Sheet.Range("C2").Value, 1)

  3. #3
    Registered User
    Join Date
    08-13-2012
    Location
    Manchester
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Saving a file to new folder dependant on the first letter of the file name.

    Thanks, have sorted it now. Went with this and works a treat:

    Function dirExists(dirAndPath) As Boolean
    Dim tempVar
    On Error Resume Next
    tempVar = Dir(dirAndPath & "\*.*", vbDirectory)
    If Err = 0 And tempVar <> "" Then
        dirExists = True
    End If
    On Error GoTo 0
    End Function
    
    
    Private Sub Save_File_Click()
    
    Dim fname As String
    Dim Dname As String
    Dim fchoose As String
    Dim fpath As String
    
    ' Run the Error handler "ErrHandler" when an error occurs.
    On Error GoTo Errhandler
    
    
    fchoose = Left(Front_Sheet.Range("C2").Value, 1)
    
    If fchoose = "A" Or fchoose = "B" Or fchoose = "C" Or fchoose = "a" Or fchoose = "b" Or fchoose = "c" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\A - C\" Else
    If fchoose = "D" Or fchoose = "E" Or fchoose = "F" Or fchoose = "G" Or fchoose = "d" Or fchoose = "e" Or fchoose = "f" Or fchoose = "g" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\D - G\" Else
    If fchoose = "H" Or fchoose = "I" Or fchoose = "J" Or fchoose = "K" Or fchoose = "h" Or fchoose = "i" Or fchoose = "j" Or fchoose = "k" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\H - K\" Else
    If fchoose = "L" Or fchoose = "M" Or fchoose = "N" Or fchoose = "O" Or fchoose = "l" Or fchoose = "m" Or fchoose = "n" Or fchoose = "o" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\L - O\" Else
    If fchoose = "P" Or fchoose = "Q" Or fchoose = "R" Or fchoose = "p" Or fchoose = "q" Or fchoose = "r" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\P - R\" Else
    If fchoose = "S" Or fchoose = "T" Or fchoose = "s" Or fchoose = "t" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\S - T\" Else
    If fchoose = "U" Or fchoose = "V" Or fchoose = "W" Or fchoose = "X" Or fchoose = "Y" Or fchoose = "Z" Or fchoose = "u" Or fchoose = "v" Or fchoose = "w" Or fchoose = "x" Or fchoose = "y" Or fchoose = "z" Then fpath = "H:\Acquisitions\Private\Acquisitions\Project Appraisals CBA's\Current CBA's\U - Z\"
    
    
    If Not dirExists(fpath & Front_Sheet.Range("C2").Value) Then MkDir fpath & Front_Sheet.Range("C2").Value
    Dname = fpath & Front_Sheet.Range("C2").Value & "\"
    fname = Front_Sheet.Range("C2").Value & " " & Front_Sheet.Range("C6").Value & ".xlsm"
    ThisWorkbook.SaveAs Filename:=Dname & fname
    
    Exit Sub
    
    Errhandler:
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1