+ Reply to Thread
Results 1 to 8 of 8

Exporting sheets to FlatFiles in workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Exporting sheets to FlatFiles in workbook

    I have a code I have been using to export my sheets in Workbook as flatfiles; to just keep the values without the formulas. It is working fine but I am looking to amend the code to only export specific pages in each Workbook. What do I add in my code to specify certain sheets to export?
    Sub CreateWorkbooks()
    Dim wbDest As Workbook
    Dim wbSource As Workbook
    Dim sht As Object
    Dim strSavePath As String
    Dim r As Long, c As Long, ws As Worksheet
    On Error GoTo ErrorHandler
    
    Application.ScreenUpdating = False
    
    
    strSavePath = "S:\Location\"
    
    
    Set wbSource = ActiveWorkbook
    
    
    For Each sht In wbSource.Sheets
    r = sht.Rows.Find("*", , , , xlByRows, xlPrevious).Row
    c = sht.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
    sht.Copy
    Set ws = ActiveSheet
    ws.Range("A1").Resize(r, c).Value = sht.Range("A1").Resize(r, c).Value
    Set wbDest = ActiveWorkbook
    wbDest.SaveAs strSavePath & sht.Name
    wbDest.Close
    Next
    
    Application.ScreenUpdating = True
    
    ErrorHandler:
    
    End Sub
    Thanks

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,770

    Re: Exporting sheets to FlatFiles in workbook

    Macro loops through all sheets in workbook

    For Each sht In wbSource.Sheets
    Then you could add an "If" statment checking sheet name and let macro work on the specified sheets.

    Alf

  3. #3
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Exporting sheets to FlatFiles in workbook

    Thanks Alf.
    Actually is there anyway I can just export my whole workbook without formulas, and keeping the original with the macros/formulas??

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,770

    Re: Exporting sheets to FlatFiles in workbook

    You could loop through all he sheets in the workbook and use this command to change all formulas to value

    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    just be carefull that you don't save this file "over" your original file

    Alf

  5. #5
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Exporting sheets to FlatFiles in workbook

    Will this save it under one workbook? versus individual sheets?
    I am hoping to just replicate the workbook with only values without impacting the original workbook.

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,770

    Re: Exporting sheets to FlatFiles in workbook

    Will this save it under one workbook? versus individual sheets?
    Not sure of what you do mean. The command line will convert all formulas to values but this works only on the active sheet, so after looping through all sheets in a workbook you will have a workbook that contains only values and no formulas.

    You can then select to save each sheet as a separate workbook, or save the "whole" workbook.

    Alf

  7. #7
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Exporting sheets to FlatFiles in workbook

    Thanks Alf,
    What do I put in the code to save the "whole" workbook?

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,770

    Re: Exporting sheets to FlatFiles in workbook

    Not sure I read your code right but it seems to me that the code loops through every sheet in your "Master" file and saves each sheet as a separete file with sheet name as fil name.

    If this is so perhaps you could just loop through your master file, checking sheet name and delet the one do not wish to keep and transfer formulas to values for the other sheets. Then you save this modified file with a new name and as an xlsx file to get rid of the macro as well. This way you will still keep your original "Master" file intact.

    Macro something like this perhaps:

    Option Explicit
    
    Sub MakeFlat()
    Dim i As Integer
    
    Application.DisplayAlerts = False
    
    For i = 1 To Sheets.Count
    
    Sheets(i).Activate
    
    If ActiveSheet.Name = "This" Or ActiveSheet.Name = "That" Then
    
        ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    Else
    
         Sheets(i).Delete
    
    End If
    
    Next
    
    ActiveWorkbook.SaveAs Filename:="myFile.xlsx", FileFormat:=xlOpenXMLWorkbook
    
    Application.DisplayAlerts = True
    
    End Sub
    Alf

+ 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. Exporting each sheet to a new workbook and saving workbook in new folder with timestamp
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2013, 11:19 AM
  2. [SOLVED] Exporting Sheets into templated workbook
    By DataVanMan in forum Excel Programming / VBA / Macros
    Replies: 63
    Last Post: 04-10-2013, 01:53 PM
  3. Importing and Exporting data from workbook to workbook in excel
    By dans123 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-09-2012, 03:40 AM
  4. Exporting Workbook with new name
    By joehjas in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-13-2011, 03:08 AM
  5. Exporting Multiple Sheets from one workbook to another spreadsheet
    By jabryantiii in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-06-2010, 01:04 PM
  6. Automated Exporting Sheets?
    By thomas.szwed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2007, 07:44 AM
  7. [SOLVED] exporting to another workbook
    By steve in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-20-2005, 02:20 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