+ Reply to Thread
Results 1 to 5 of 5

Need to Paste Formulas and Not Values in Current VBA Code

Hybrid View

  1. #1
    Registered User
    Join Date
    10-02-2017
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    22

    Need to Paste Formulas and Not Values in Current VBA Code

    I have a pre-written VBA code that splits one worksheet out into multiple different worksheets based on the value in column B. It then saves each of the new files using the path in the tab "Settings". The only issue with the current code is that it only pastes the formula in column E to the first file created - all files created thereafter simply paste everything as values.

    Is there a way to edit this code to ensure that the formulas carry through to all of the newly created files, and not just the first one? Sample excel file with the code is attached.

    Thanks!

    Here is the current code:



    Option Explicit
    
    Sub Split_Data_in_workbooks()
    
    Application.ScreenUpdating = False
    
    Dim data_sh As Worksheet
    Set data_sh = ThisWorkbook.Sheets("Data")
    
    Dim setting_Sh As Worksheet
    Set setting_Sh = ThisWorkbook.Sheets("Settings")
    
    Dim nwb As Workbook
    Dim nsh As Worksheet
    
    ''''' Get unique supervisors
    
    setting_Sh.Range("A:A").Clear
    data_sh.AutoFilterMode = False
    data_sh.Range("B:B").Copy setting_Sh.Range("A1")
    
    setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes
    
    Dim i As Integer
    
    For i = 2 To Application.CountA(setting_Sh.Range("A:A"))
    
    data_sh.UsedRange.AutoFilter 2, setting_Sh.Range("A" & i).Value
    
    
    Set nwb = Workbooks.Add
    Set nsh = nwb.Sheets(1)
    
    data_sh.UsedRange.SpecialCells(xlCellTypeVisible).Copy nsh.Range("A1")
    nsh.UsedRange.EntireColumn.ColumnWidth = 15
    
    nwb.SaveAs setting_Sh.Range("H6").Value & "/" & setting_Sh.Range("A" & i).Value & ".xlsx"
    nwb.Close False
    data_sh.AutoFilterMode = False
    Next i
    
    setting_Sh.Range("A:A").Clear
    
    MsgBox "Done"
    
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: Need to Paste Formulas and Not Values in Current VBA Code

    hi, Try the corrected code
    Sub Split_Data_in_workbooks()
    
        Application.ScreenUpdating = False
        
        Dim data_sh As Worksheet
        Set data_sh = ThisWorkbook.Sheets("Data")
        
        Dim setting_Sh As Worksheet
        Set setting_Sh = ThisWorkbook.Sheets("Settings")
        
        Dim nwb As Workbook
        Dim nsh As Worksheet
        
        ''''' Get unique supervisors
        
        setting_Sh.Range("A:A").Clear
        data_sh.Range("B:B").Copy setting_Sh.Range("A1")
        
        setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes
        
        Dim i As Integer
        
        For i = 1 To Application.CountA(setting_Sh.Range("A:A"))
        
            Set nwb = Workbooks.Add
            ActiveSheet.Name = setting_Sh.Range("A:A").Cells(i)
            
            Set nsh = ActiveSheet
            data_sh.UsedRange.SpecialCells(xlCellTypeVisible).Copy nsh.Range("A1")
            nsh.UsedRange.EntireColumn.ColumnWidth = 15
            
            nwb.SaveAs setting_Sh.Range("H6").Value & "/" & nsh.Name & ".xlsx"
            nwb.Close False
        Next i
        setting_Sh.Range("A:A").Clear
        
        MsgBox "Done"
    End Sub
    Regards.

  3. #3
    Registered User
    Join Date
    10-02-2017
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    22

    Re: Need to Paste Formulas and Not Values in Current VBA Code

    Hmm it is keeping the formulas in column E but no longer splitting the documents out by the value in column B.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: Need to Paste Formulas and Not Values in Current VBA Code

    Try
    Sub Split_Data_in_workbooksx()
        Dim data_sh As Worksheet, setting_Sh As Worksheet, i As Integer
        Application.ScreenUpdating = False
        Set data_sh = ThisWorkbook.Sheets("Data")
        
        Set setting_Sh = ThisWorkbook.Sheets("Settings")
    
        ''''' Get unique supervisors
        
        setting_Sh.Range("A:A").Clear
        data_sh.AutoFilterMode = False
        data_sh.Range("B:B").Copy setting_Sh.Range("A1")
        
        setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes
        
        Sheets.Add Sheets(1)
        With Sheets(1)
            For i = 2 To Application.CountA(setting_Sh.Range("A:A"))
                data_sh.Cells(1).CurrentRegion.Copy .Cells(1)
                With .Cells(1).CurrentRegion
                    .AutoFilter 2, "<>" & setting_Sh.Range("A" & i).Value
                    .Offset(1).EntireRow.Delete
                    .AutoFilter
                    .Columns.ColumnWidth = 15
                End With
                .Copy
                With ActiveWorkbook
                    .Sheets(1).Name = setting_Sh.Range("A" & i).Value
                    .SaveAs setting_Sh.Range("H6").Value & "/" & setting_Sh.Range("A" & i).Value & ".xlsx"
                    .Close False
                End With
            Next
            Application.DisplayAlerts = False
            .Delete
            Application.ScreenUpdating = True
        End With
        setting_Sh.Range("A:A").Clear
        MsgBox "Done"
    End Sub

  5. #5
    Registered User
    Join Date
    10-02-2017
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    22

    Re: Need to Paste Formulas and Not Values in Current VBA Code

    This works perfectly, thank you!

+ 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. Issue with Current Code to Copy Paste Cells Inside a Table
    By VanChester in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-23-2017, 11:50 PM
  2. Paste certain formulas as formulas and others as values to a new workbook
    By fokk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-03-2017, 12:07 PM
  3. Need to paste info without formula , just figures itself but with my current code cant.
    By iljamolodih in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2015, 10:05 AM
  4. Copy Formulas and Paste into the Last Set # of Rows Only & Paste Values
    By Mr. Ice in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2014, 07:48 AM
  5. Code to paste values not formulas
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2014, 06:33 AM
  6. Using current month selection to copy/paste formulas and values
    By gigi85 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-03-2014, 07:51 AM
  7. [SOLVED] Changing Current Code from Paste Formulas to Paste Values
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-24-2013, 12:33 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