File Distribution using VBA

ollieotis

New Member
Joined
Jun 6, 2006
Messages
44
Hello,

I use the following code to break up an excel file into multiple files for distribution. In the files being distributed, I would now like to add a sum function at the end of the last row in multiple columns.

Would appreciate any and all help I can get here. I'm a novice at best when it comes to VBA (I received a lot of help in the past from this board on the below code).

Thanks!


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("AV" & Rows.Count).End(xlUp).Row

wsData.Range("AV1:AV" & 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:=xlFilterInPlace, _
CriteriaRange:=rngCrit.Offset(-1).Resize(2), Unique:=True
wsData.Range("A1:BZ" & LastRow).Copy
wsNew.Range("A1").PasteSpecial xlPasteFormulas
wsNew.Range("A1").PasteSpecial xlPasteFormats
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 = ""
Range("AS:BZ").Delete
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

wsData.ShowAllData
wsCrit.Delete
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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