Results 1 to 10 of 15

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi Flupsi,
    I am not sure if I quite understand your question?

    Your original Code saves as Adjustment JNL.csv

    In your original code the following line
    .Sheets(1).Name = "JNL"
    changes the first Worksheet name.

    So if you do not want that change to happen, then simply remove that code line

    Code:
     Sub CreateCSVFile()
    Dim MyPath As String
    Dim MyFileName As String
    'The path and file names:
    MyPath = "C:\Journal Templates"
    MyFileName = "Adjustment JNL.csv"
    
        If Not Right(MyPath, 1) = "" Then MyPath = MyPath & ""
        
    With ActiveWorkbook
    
        .SaveAs Filename:= _
            MyPath & MyFileName, _
            FileFormat:=xlCSV, _
            CreateBackup:=False
        
        .Close False
    End With 
    End Sub
    Alan
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

  2. #2
    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.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    40
    Rep Power
    0
    Hi Alan

    Thanks for the help

    I need two small changes

    1) I need the csv file to be saved to "C:\Journal Templates" -(have amended this in the code)
    2) I need the sheet name to be the same as the xlsm workbook i.e "JNL"


    I have attached my sample workbook

    It would be appreciated if you could make the necessary change
    Attached Files Attached Files

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
  •