macro to Save to Folder not working

jmurray394

New Member
Joined
Mar 7, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to get this to work where a macro from another excel workbook generates a new workbook with all the data specific to that company. That works perfectly fine and now the next thing I want it to do is when the new workbook is created, it automatically saves to a folder having the file name as the company that is in cell A1 and the date that's in Q1. There should be a parent folder called "Companies" and if there isn't one, the macro will create it along with the child folder for that specific company. The macro works fine in creating the parent and child folder for the first time. The issue is that if i run the macro to create an updated file for that company it won't save to the child/company folder and I get the error message that the folder already exists. How do I get this to work so that if the parent or child folder does already exists, the new file will still save there. Also, the new workbook does not actually autofit the columns.

VBA Code:
Sub SaveCompany()

Application.ScreenUpdating = False

Dim sourcewb, wb As Workbook
Dim sourcesht, newsht, Companysht As Worksheet
Dim lastrow, lastcol As Long
Dim Company As Variant
Dim cache As SlicerCache
Dim item As SlicerItem
Dim CompanyName, reportDate, Path, FNandDate As String
Dim CparentF As Object
Dim CchildF As Object


Set sourcewb = ThisWorkbook
Set sourcesht = sourcewb.Worksheets("Data Sheet")
Set  Companysht = sourcewb.Worksheets("Companies")
Set wb = Workbooks.Add
Set newsht = wb.Worksheets("Sheet1")
Cells.ClearContents
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''Select Company from list that will be copied to new workbook
Set cache = sourcewb.SlicerCaches(1)
For Each item In cache.SlicerItems
 If item.Selected = True Then
 Company = (item.Value)
 CompanyName = (item.Value)
 End If
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''Get report date, filer by Company, and copy to new wb
reportDate = Companysht.Range("M3")
sourcesht.ListObjects(1).Range.AutoFilter
sourcesht.ListObjects(1).Range.AutoFilter Field:=1, Criteria1:=company, Operator:=xlFilterValues
sht.Range("A1") = CompanyName
sht.Range("Q1") = reportDate
sourcesht.Columns("D:I").Copy sht.Range("A2")
sourcesht.Columns("K:U").Copy sht.Range("G2")
sht.Rows(2).EntireRow.Delete
sht.Rows(2).EntireRow.Delete
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''Format data in new workbook into a table
lastrow = sht.Cells(sht.Rows.Count - 1, 1).End(xlUp).row
lastcol = sht.Cells(2, sht.Columns.Count).End(xlToLeft).Column
sht.Range("A2", sht.Cells(lastrow, lastcol)).Select
sht.ListObjects.Add(xlSrcRange, Selection, , xlYes).TableStyle = "TableStyleMedium18"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''Format Company/Workbook Header 
sht.Range("A1:O1").Select
Selection.Font.Size = 16
 With Selection
 .HorizontalAlignment = xlCenter
 .VerticalAlignment = xlBottom
 .WrapText = False
 .Orientation = 0
 .AddIndent = False
 .IndentLevel = 0
Module4 - 2
 .ShrinkToFit = False
 .ReadingOrder = xlContext
 .MergeCells = True
 End With

sht.Range("P1") = "Report as of date:"
sht.Range("P1:Q1").Select
Selection.Font.Size = 14
sht.Range("A1:Q1").Select
 With Selection.Interior
 .Pattern = xlSolid
 .PatternColorIndex = xlAutomatic
 .ThemeColor = xlThemeColorDark1
 .TintAndShade = -0.149998474074526
 .PatternTintAndShade = 0
 End With

Selection.Font.Bold = True
sht.Columns.AutoFit
sht.Range("B:F").HorizontalAlignment = xlCenter
sht.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).EntireRow.Select
sht.Range(Selection, Selection.End(xlDown)).Select
Selection.Clear
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''Start naming file and file path
Set CparentF = CreateObject("Scripting.FileSystemObject")
Set CchildF = CreateObject("Scripting.FileSystemObject")

 If CparentF.FolderExists("\MY MAPPING IS HERE\Private\COMPANY SHEETS") Then

          If VsubF.FolderExists("\MY MAPPING IS HERE\Private\COMPANY SHEETS\" & CompanyName) Then
                 MsgBox "Folder Already Exists"
          Else
                VsubF.createfolder ("\MY MAPPING IS HERE\Private\COMPANY SHEETS\" & CompanyName)
                MsgBox "Folder didn't exist - Folder created."
          End If

Else
         VSF.createfolder ("\MY MAPPING IS HERE\Private\COMPANY SHEETS")
          MsgBox "Folder Created."
          VsubF.createfolder ("\MY MAPPING IS HERE\Private\COMPANY SHEETS\" & CompanyName)
          MsgBox "Folder didn't exist - Folder created."
End If

FNandDate = CompanyName & " " & Format(Range("Q1"), "mm-dd-yy")
Path = ("\MY MAPPING IS HERE\Private\COMPANY SHEETS\")
ActiveWorkbook.SaveAs Path & CompanyName & "\" & FNandDate & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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