Code pasting values, not formulas

ollieotis

New Member
Joined
Jun 6, 2006
Messages
44
Hello,

I have the following code set up to break up a single workbook into multiple workbooks for a distribution process. The code works great (thanks to those on this forum who helped me get it together a few years ago!), but with the advanced filter function, it's pasting only values, not formulas. I'd like to retain any relevant formulas when breaking up workbooks. Would anyone know how I could embed that into this code?

Thanks in advance!

Sub DistributeRows()

Dim wbNew As Workbook
Dim wsData As Worksheet
Dim wsCrit As Worksheet
Dim wsNew As Worksheet
Dim rngCrit As Range
Dim LastRow As Long

Set wsData = Worksheets("FileDistribution")
Set wsCrit = Worksheets.Add

LastRow = wsData.Range("BI" & Rows.Count).End(xlUp).Row

wsData.Range("BI1:BI" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True

Set rngCrit = wsCrit.Range("A2")
While rngCrit.Value <> ""
Set wsNew = Worksheets.Add
wsData.Range("A1:BZ" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCrit.Offset(-1).Resize(2), CopyToRange:=wsNew.Range("A1"), Unique:=True
wsNew.Name = rngCrit
Columns("A:BZ").AutoFit

Range("A1").Select
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
ActiveSheet.PageSetup.PrintArea = ""
wsNew.Copy

Set wbNew = ActiveWorkbook
wbNew.SaveAs ThisWorkbook.Path & "\" & rngCrit, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
wbNew.Close SaveChanges:=True
Application.DisplayAlerts = False
wsNew.Delete
rngCrit.EntireRow.Delete

Set rngCrit = wsCrit.Range("A2")

Wend

wsCrit.Delete
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
ollieotis,

This is not tested!!

But try replacing your first filter/copy/paste...
Code:
wsData.Range("BI1:BI" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True

with .....
Code:
wsData.Range("BI1:BI" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
wsCrit.Range("A1").PasteSpecial Paste:=xlPasteFormulas

If that does the trick then you can similarly edit your second filter/copy/paste.

Hope that helps.
 
Upvote 0
Thank you, Tony. I couldn't get that to work though... probably user error, but I keep getting run time errors.
 
Upvote 0
ollieotis,

My stupid fault!!! Trying to paste without copy!!!

Try.......

Code:
wsData.Range("BI1:BI" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
wsData.Range("BI1:BI" & LastRow).Copy
wsCrit.Range("A1").PasteSpecial Paste:=xlPasteFormulas

Hopefully that's better.
 
Upvote 0
Maybe (tested with a very small data sample)

Instead of

Code:
wsData.Range("A1:BZ" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCrit.Offset(-1).Resize(2), CopyToRange:=wsNew.Range("A1"), Unique:=True

This
Code:
wsData.Range("A1:BZ" & LastRow).AdvancedFilter Action:=xlFilterInPlace, _
        CriteriaRange:=rngCrit.Offset(-1).Resize(2), Unique:=True
wsData.Range("A1:BZ" & LastRow).SpecialCells(xlCellTypeVisible).Copy
wsNew.Range("A1").PasteSpecial xlPasteFormulas

M.
 
Upvote 0
I'm almost certain I'm missing something really obvious, but my three year old who is sitting next to me desperately wants my attention and I'm struggling to focus...

That said, I've tried copying in that code and I can't get it to work. If I copy to just the first set, I get the same results. If I copy the code to both filter sections, the macro runs on the first filename in perpetuity. I have to hit escape to get out.

Thanks!
 
Upvote 0
I think Snakehip is right - SpecialCells(xlCellTypeVisible) works but it's not necessary

Maybe if you use for the first filter Snakehip's code

Code:
wsData.Range("BI1:BI" & LastRow).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
wsData.Range("BI1:BI" & LastRow).Copy
wsCrit.Range("A1").PasteSpecial Paste:=xlPasteFormulas

and for the second

Code:
wsData.Range("A1:BZ" & LastRow).AdvancedFilter Action:=xlFilterInPlace, _
        CriteriaRange:=rngCrit.Offset(-1).Resize(2), Unique:=True
wsData.Range("A1:BZ" & LastRow).Copy
wsNew.Range("A1").PasteSpecial xlPasteFormulas

M.
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,360
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top