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. #7
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,458
    Rep Power
    10
    Hi again Flupsi,

    I think I understand now. Actually I see now you explained adequately in the first Post. Sorry I do not have too much experience with .csv files and I did not realise what happened with “Worksheet” names. So I have learned something too :-)
    _............................
    I think I can explain the problem.

    A CSV file is just a text file. It is not an Excel File. What you are seeing is not a Spreadsheet.
    If you open your CSV file in NotePad, you will see that it's just data.
    A CSV file stores no information at all on formatting, formulas, Worksheets etc. It has no worksheets. It is just a set of values, typically separated by some separator.
    If you open your file in Excel you are still seeing just data. It may appear to be in the form of a Spreadsheet. But it is not. The “Tab” at the bottom is not a Worksheet Name. It may appear to be a Worksheet, and you can even change its value within Excel. However, by saving as .csv the value at the bottom will have no meaning. After closing and opening the .csv File the value shown in that Tab will be again that of the File Name. It is not a Worksheet name.

    So you see you cannot change the Worksheet name of a .csv File. This is because A .csv File has no Worksheet. It is a simple File of data which, when "viewed in Excel, will be displayed in a "Worksheet type"format. The people who wrote Excel were in my opinion a bit silly to put a value in the tab at the bottom when the File is a .csv. They should have just left it blank or removed it all together to avoid the confusion, in my opinion. That value being shown in the tab is not a Worksheet name. It is the actual file name. The value there will always be given the File name. There is no way to get over that.

    I hope that helps a bit....
    _......

    The best I can suggest is to save as an .xlsx File if you want a Worksheet name.

    A simple way to do what you want then is to copy the first sheet. This automatically creates a new workbook with a sheet in it of the same name as that from which it is copied. That can be saved as a .xlsx file ( with the name of your choosing). There is no point in trying to save that file after as a .csv with a different name to the Worksheet name. If you have understood what I have said you will see that you will no longer have a Worksheet and the “value” written in the Tab below will be as previously just the File Name.

    So this code will do almost what you want, except that the file saved is a .xlsx File

    Code:
    Sub CopyToXLSX() '  http://www.excelfox.com/forum/showthread.php/2123-Macro-to-create-CSV-with-same-sheetname-as-Excel-xlsm-file?p=9924#post9924
    Rem 1 Workbooks and Worksheets Info
    '1a) The Required Workbook Path of File to be saved
    Dim MyPathAW As String: MyPathAW = "C:\Pinnacle Journal Templates\": ' If Not Right(MyPathAW, 1) = "\" Then MyPathAW = MyPathAW & "\" ' 'Makes sure the path name ends with "\" - not really necerssary as inspection shows we clearly have a last \
    '1b) File with first Worksheet which should be saved as a ( .xlsx ) File. Select the File required so that it is the Active Workbook ( Active File )
    Dim WBActive As Workbook                                             ' Dim:  ' Preparing a "Pointer" to an Initial "Blue Print" in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Objec of this type ) . This also us to get easily at the Methods and Properties throught the applying of a period ( .Dot) ( intellisense )                     '
    Set WBActive = ActiveWorkbook                                        ' Set now to the Current active Workbook, - That being "looked at" when running this code   , so that we carefull allways referrence this so as not to go astray through Excel Guessing inplicitly not the one we want...         Set: Values are filled at the memory locations and the directions there are specified in the variable "Blue Print "Pointer". In this case a Filled in Blue Print is passed.      http://www.excelforum.com/excel-programming-vba-macros/1138804-help-understanding-class-instancing-cant-set-ws-new-worksheet-intellisense-offers-it-4.html#post4387191Set ws = ActiveSheet ' Alternative to last line, make code apply to the current active sheet, - That being "looked at" when running this code        '
    Dim Ws1 As Worksheet: Set Ws1 = WBActive.Worksheets.Item(1) ' 'The Worksheets Collection Object of WBActive is used referrencing by Item number which is a consecutive count starting at 1 from the left  and increasing by 1 as you count to the right. This Worksheet has the .Name Property applied to return the String Tab Name
    'Dim Ws1Name As String             ' Prepares "Pointer" to a "Blue Print" (or Form, Questionnaire not yet filled in, a template etc.)"Pigeon Hole" in Memory, sufficient in construction to house a piece of Paper with code text giving the relevant information for the particular Variable Type. VBA is sent to it when it passes it. In a Routine it may be given a particular “Value”, or (“Values” for Objects).  There instructions say then how to do that and handle(store) that(those). At Dim the created Paper is like a Blue Print that has some empty spaces not yet filled in. A String is a a bit tricky. The Blue Print code line Paper in the Pigeon Hole will allow to note the string Length and an Initial start memory Location. This Location well have to change frequently as strings of different length are assigned. Instructiions will tell how to do this. Theoretically a specilal value vbNullString is set to aid in quich checks.. But...  http://www.mrexcel.com/forum/excel-questions/361246-vbnullstring-2.html#post44116
    'Let Ws1Name = Ws1.Name
    Rem 2 Build The string of the File name to be saved as:
    Dim MyFileAW As String: Let MyFileAW = WBActive.Name 'The .Name Property of the Active Workbook returns the string name including the extension ( .xlsm, or .xls  etc. )
    Dim MyFileAWNameOnly As String: Let MyFileAWNameOnly = Left(MyFileAW, (InStrRev(MyFileAW, ".") - 1)) ' ' ActiveWorkbook Name without extension:  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.
    Dim MyFileName As String: Let MyFileName = MyFileAWNameOnly & ".xlsx" 'Final required Full File Name
    	
    Rem 3 use the Copy Method applied to the fist worksheet of the Active Workbook to produce a copy Worksheet with the same name. Excel "houses this" automatically in a new File
    Ws1.Copy ' A new Worksheet of the same name as Ws1 is housed in a new File which becomes the new Active Workbook
    ActiveWorkbook.SaveAs Filename:=MyPathAW & MyFileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    '3b Close New created File
    ActiveWorkbook.Close '  False '
    End Sub
    Hope that is some help. Sorry I did not get the point quicker
    Let me know if you need more help.

    Alan
    Last edited by DocAElstein; 09-07-2016 at 07:09 PM.
    ….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!!

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
  •