Hi, I cannot get the below to work correctly for me. I am trying to copy two worksheets form a certain workbook in to a new workwork. This new workbook is then saved down every day in the following location T:\InterDepartment\Pricing\Loans\. I want to create a saving code that updates the file in to a corresponding month folder and then eventually a year folder. I keep gettting an error message on the code below ( which is mostly copied an applied from other websites etc)
Any help would be greatly appreciated
Sub New_Macro()
'
' New_Macro Macro
'Legend for codes used in the macro
Dim WS As Worksheet
Dim i As Integer
'Copies the sheets to a new workbook:
Sheets(Array("Prices", "Markit Compare")).Copy
With ActiveWorkbook
For Each WS In .Worksheets 'Goes through each worksheet in new workbook
With WS
.UsedRange.Value = WS.UsedRange.Value 'Writes the values
i = i + 1 'Adds one to i value
.Name = "Sheet " & i 'Names the sheet as "Sheet " and i value
End With
Next WS
Filename = "All Loan Prices"
'Defining new terms that are used in the code
Dim CheminDest, NomDest
YearFolder = Right(Year(Date), 4)
MonthFolder = MonthName(Month(Date), True) & " " & Right(Year(Date), 2)
CheminDest = "T:\InterDepartment\Pricing\Loans\" & YearFolder & "\"
If Len(Dir(CheminDest & MonthFolder, vbDirectory)) = 0 Then
MkDir CheminDest & MonthFolder
End If
If Len(Dir(CheminDest & MonthFolder, vbNormal)) <> 0 Then
ActiveWorkbook.Close SaveChanges:=False
Exit Sub
End If
ChDir _
"T:\InterDepartment\Pricing\Loans\" & YearFolder & "\" & MonthFolder
'Saves the file (text version)
ActiveWorkbook.SaveAs Filename:=Filename & " Text File.xls"
wkbk.Save
Set wkbk = Nothing
MsgBox "Text file has been saved ", vbInformation, "Data backup"
ActiveWorkbook.Close
End Sub
Bookmarks