+ Reply to Thread
Results 1 to 11 of 11

Exporting Workbook with new name

Hybrid View

  1. #1
    Registered User
    Join Date
    11-10-2011
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    16

    Exporting Workbook with new name

    I'm currently working on a macro that export several rows and cells to a new workbook which then is saved as a txt-file with the same name as the original workbook.
    I have written and worked out everything except the saving-part. I can't work out how to save the new file with a different name everytime.

    Here's what i got so far:

    Sheets("Eksport").Visible = True
    Sheets("Eksport").Select
    ActiveSheet.Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:= "H:\My Documents\Ny Kalkuleringsmal\CSV\test.txt" _
        , FileFormat:=xlCSV, CreateBackup:=False
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Sheets("Eksport").Visible = False
    Last edited by joehjas; 12-12-2011 at 08:07 AM.

  2. #2
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Exporting Workbook with new name

    not quite sure what you mean by "different name every time" you couled forinstance date-stamp the file: if that's what you're thinking about?
    ActiveWorkbook.SaveAs Filename:= "H:\My Documents\Ny Kalkuleringsmal\CSV\test" & Date & ".txt" _
        , FileFormat:=xlCSV, CreateBackup:=False
    _______________________________________________
    Remember:

    Read the rules!

    Use code tags! Place [CODE] before the first line of code and [/CODE] after the last line of code.

    Mark your post [SOLVED] if it has been answered satisfactorily.
    To do this, click EDIT in your original post, click GO ADVANCED and set the PREFIX. It helps everybody!

    If you are approve (or disapprove) of any members response to your problem, please click the star icon underneath their post and comment. It matters.

  3. #3
    Registered User
    Join Date
    11-10-2011
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Exporting Workbook with new name

    What other operator can i use instead of Date, filename of the original workbook?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Exporting Workbook with new name

    You are exporting a worksheet not a workbook.

    something like
    With Sheet1
    .Visible = xlSheetVisible
    .Copy
    .Visible  = xlSheetVeryHidden
    End With
    ActiveWorkbook.SaveAs Filename:="H:\My Documents\Ny Kalkuleringsmal\CSV\test" & Format(Date, "ddmmyy") & Format(Time, "hhmmss") & ".txt" _
        , FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.Close
    Last edited by royUK; 12-13-2011 at 03:08 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    11-10-2011
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Exporting Workbook with new name

    Okay, maybe I am, but still; what are my options in naming the new file? Is there some way I can insert a different text each time(not date), maybe a increasing number for each time the file is saved?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Exporting Workbook with new name

    Quote Originally Posted by joehjas View Post
    Okay, maybe I am,
    It makes a big difference

  7. #7
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Exporting Workbook with new name

    to put it simple: you can achieve what ever you need; question is: what is it exactly that you want to achieve?
    eg. if you had to do it manually, what would you do, how would you name the files; and why would you name them the way you would?

    (the more info you give, the easier it is for us to understand what you want/need, thus easier to help)

  8. #8
    Registered User
    Join Date
    11-10-2011
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Exporting Workbook with new name

    Quote Originally Posted by wamp View Post
    to put it simple: you can achieve what ever you need; question is: what is it exactly that you want to achieve?
    eg. if you had to do it manually, what would you do, how would you name the files; and why would you name them the way you would?

    (the more info you give, the easier it is for us to understand what you want/need, thus easier to help)
    The original document, the excel-file, is a template for calculating sales quotes.
    The document is upon creation saved automatically with a number(increasing by 1 each time).
    What I want is the new file to have the same number.

  9. #9
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Exporting Workbook with new name

    ' on top of your code, you could
    public saveName As String
    saveName = ActiveWorkbook.Name
    
    'then later upon saving do 
    ActiveWorkbook.SaveAs Filename:="H:\My Documents\Ny Kalkuleringsmal\CSV\" & saveName & ".txt" _
        , FileFormat:=xlCSV, CreateBackup:=False
    in your example; it would be something like :
    public saveName as string
    
    sub yoursubname()
    With Ark1 ' change to the codename for "Eksport"
    .Visible = xlSheetVisible
    .Copy
    .VPageBreaks = xlSheetVeryHidden
    End With
    
    ActiveWorkbook.SaveAs Filename:="H:\My Documents\Ny Kalkuleringsmal\CSV\" & saveName & ".txt" _
        , FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.Close
    end sub
    Last edited by wamp; 12-12-2011 at 06:41 AM.

  10. #10
    Registered User
    Join Date
    11-10-2011
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Exporting Workbook with new name

    Nice, that works
    Any idea how to remove the .xlsm?
    So I'll have 10001.txt instead of 10001.xlsm.txt

  11. #11
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Exporting Workbook with new name

    alter saveName to:
    saveName = Left(ActiveWorkbook.Name, (Len(ActiveWorkbook.Name) - 5))
    'Explanation:
    'saveName= Left(whole name, length ; where Length = total length subtracted by 5 (".xlsm" = 5 characters)

+ 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