+ Reply to Thread
Results 1 to 7 of 7

Get last name of workbook

  1. #1
    Registered User
    Join Date
    08-08-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    27

    Smile Get last name of workbook

    Hi Friends,

    Good Morning....

    I am trying write code to find the last name of workbook and copy workbook. Paste those workbook in the respective files.

    Examaple:

    I have files like 401020_2350.xls, 400100_4060.xls, 134010_0585.xls and 348100_1153.xls.

    My requieremnt is if last name of workbook contains 2350 and 4060 then these workbooks get copy and paste in Folder Brazil. If workbook file contains 0585 and 1153 then these workbooks get copy and paste in folder Europe.


    I have searched in different sites....but i could get not any result.


    Thanks & regards,
    Pradeep

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Lightbulb Re: Get last name of workbook

    modify as needed. Good Luck
    Please Login or Register  to view this content.
    If you think this has satisfactorily answered your need. Please mark the thread as Solved.

    Warm regards,
    Jewel

  3. #3
    Registered User
    Join Date
    08-08-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Get last name of workbook

    Hi Jewel,

    Thanks so much....

    I have modified the codes as per my requirement but its not working.

    Please Login or Register  to view this content.
    Its surprise that macro runs and ends successfully but not copying files and paste in respective folders.

    Could you please suggest, if I have done any wrong in modifying the macro.

    Fortunately I am not getting any debug also.

    Thanks & regards,
    Pradeep
    Attached Files Attached Files
    Last edited by Leith Ross; 04-04-2014 at 09:03 PM. Reason: Added Code Tags

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Arrow Re: Get last name of workbook

    Here you go. I've made slight changes to the code:
    Please Login or Register  to view this content.
    The above code is working fine on my PC now.
    Notes:
    • Do make sure that "Brazil" & "Europe" folders don't have any files by the same file name(s) in them already.
    • I ran this Macro through a separate workbook (i.e. not the one that was to be sorted). Though I'm really sure if this is of any significance.
    Let me know if this worked for you, or if you had any issues.

    Warm regards,
    Jewel

  5. #5
    Registered User
    Join Date
    08-08-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Get last name of workbook

    Hi Jewel,

    Wowww..... thank you so much its working now perfectly.....

    As its opens files and save as in the respective folder.

    Based on your codes I am trying to edit with this code please suggest whether my code is correct or not because while run the macro
    its showing Run-Time Error "53" File not Found.

    ====================================================
    Sub Sort_Files()
    Dim oFSO As Object, oFiles As Object, oFile As Object
    Dim sOriginalFolder As String, sBrazilFolder As String, sEuropeFolder As String, sFileName As String


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    sOriginalFolder = "C:\Example\" '(Change to current directory. Must end with "\")
    sBrazilFolder = "C:\Example\Brazil\" '(Change to desired directory. Must end with "\")
    sEuropeFolder = "C:\Example\Europe\" '(Change to desired directory. Must end with "\")
    'FExtension = "*.xls"

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFiles = oFSO.GetFolder(sOriginalFolder).Files

    For Each oFile In oFiles

    sFileName = Split(oFile.Path, "\")(UBound(Split(oFile.Path, "\")))

    If Right(sFileName, 9) = "_2350.xls" Or Right(sFileName, 9) = "_4060.xls" Then

    oFSO.MoveFile Source:=sOriginalFolder, Destination:=sBrazilFolder
    Else

    ' Application.Workbooks.Open Filename:=oFile.Path
    ' With Workbooks(sFileName)
    ' .SaveAs Filename:=sBrazilFolder & sFileName
    ' .Close
    ' End With


    'End If


    If Right(sFileName, 9) = "_0585.xls" Or Right(sFileName, 9) = "_1153.xls" Then

    oFSO.MoveFile Source:=sOriginalFolder, Destination:=sEuropeFolder


    'Application.Workbooks.Open Filename:=oFile.Path
    'With Workbooks(sFileName)
    ' .SaveAs Filename:=sEuropeFolder & sFileName
    ' .Close
    ' End With
    End If
    End If
    Next oFile

    Set oFSO = Nothing
    Set oFiles = Nothing
    Set oFile = Nothing

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub

    =============================================================




    Thanks & regards,
    Pradeep
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Arrow Re: Get last name of workbook

    The object that is being evaluated/manipulated is "oFile" not "oFSO". Change this:
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    do the same for the sEuropeFolder line in the Else statement. As your original issue is solved. I request you to kindly mark the thread as SOLVED.
    For any follow-up queries, please start a new thread.

  7. #7
    Registered User
    Join Date
    08-08-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    27

    Thumbs up Re: Get last name of workbook

    Hi Jewel,

    Now its working perfectly....thanks so much for your help


    Thank & regards,
    Pradeep

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Code to cut cell from one workbook, close same workbook, and paste in different workbook
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-13-2014, 04:01 PM
  2. Copy data one workbook to another workbook without opening workbook
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2014, 11:28 AM
  3. [SOLVED] Copy Values From Each Workbook in Folder to a Single Sheet in New Workbook +Workbook names
    By Arsham24 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2012, 07:42 PM
  4. Save an open workbook, then open template workbook and close the saved workbook
    By ondvirg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2009, 10:20 PM
  5. Replies: 1
    Last Post: 04-01-2006, 03:50 PM

Tags for this Thread

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