Results 1 to 10 of 15

Thread: VBA Macro To Create An Excel File With Same Sheet Name As Workbook Name

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi again Flupsi,

    Possibly you are asking for the File to be saved as a .csv File, but with the name of the Current Active Workbook ?

    If so the next code will do that:

    Code:
    '
     Sub CreateCSVFileNameFromActiveWorkbook() '   http://www.excelfox.com/forum/showthread.php/2123-Macro-to-crete-CSV-with-same-sheetname-as-xlsm-file
        With ActiveWorkbook
    Rem 1 The ActiveWorkbook Path
        Dim MyPathAW As String: MyPathAW = ActiveWorkbook.Path ' String path to Folder containing the current Active Workbook
            If Not Right(MyPathAW, 1) = "\" Then MyPathAW = MyPathAW & "\" ' This will usually always be needed
    Rem 2 The File name:
        '2a) ' ActiveWorkbook Full Name
        Dim MyFileAW As String: Let MyFileAW = ActiveWorkbook.Name 'The .Name Property of the Active Workbook returns the string name including the extension ( .xlsm, or .xls  etc. )
        '2b) ' ActiveWorkbook Name without extension
        Dim MyFileAWNameOnly As String: Let MyFileAWNameOnly = Left(MyFileAW, (InStrRev(MyFileAW, ".") - 1)) 'To Take off the bit after the . dot     (InStrRev(MyFileAW, ".")   gives the position of the last  .    This is also the first . looking from the right, but the position is counting from the left   Applying  -1  will give the postion just before the last  .     MyFileAWNameOnly then becomes the first   (InStrRev(MyFileAW, ".") - 1)   characters in MyFileAW  counting from the left.
        '2c) ' The File name I finally want
        Dim MyFileName As String: Let MyFileName = MyFileAWNameOnly & ".csv"
    Rem 3 save the File as a .csv File
         .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
         .Close False ' False prevents being asked to save changes. It will not prevent being asked if you wish to overwrite an existing File with this Path and Name
        End With
    End Sub
    Alan
    Last edited by DocAElstein; 09-13-2019 at 07:15 PM.

Similar Threads

  1. Replies: 4
    Last Post: 07-02-2013, 11:32 AM
  2. Replies: 4
    Last Post: 06-18-2013, 01:38 PM
  3. VBA To Create A New Workbook
    By cdurfey in forum Excel Help
    Replies: 9
    Last Post: 05-23-2013, 06:41 PM
  4. Excel VBA Macro To Open A File Through Browse Dialog Box
    By Safal Shrestha in forum Excel Help
    Replies: 2
    Last Post: 04-05-2013, 12:59 PM
  5. Replies: 1
    Last Post: 06-02-2011, 10:38 AM

Posting Permissions

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