175 Lisas Report Builder
175 Lisas Report Builder
175 Lisas Report Builder
Dim AllMonths As String 'Holds the workbook name of the "All Months" file
Dim LatestMonth As String 'Holds the workbook name of the "Latest Month" file
Dim MyCostCentre As String 'Holds the cost centre name
Dim MySaveFormat As String 'Holds the name of the format we will save the output as
Dim MyEMail As String 'Holds the email address of the reipient
Dim MySheet1 As String 'Holds the name of "All" sheet for saving
Dim MySheet2 As String 'Holds the name of "Latest" sheet for saving
Dim FileSavePathAndName 'Holds the path and name for the save routines
Dim X As Integer 'A simple counter
Sub TheRunningOrder()
'The questions
'I have gotten macros to format and rename tabs but I am struggling with the following aspects:
'(1) giving an option to choose the downloaded workbooks to work on,
'(2) getting excel to export sheets depending on the cost centre owner into one report (I have a table defining cost centre own
'(3) saving this new file automatically in a new months folder but choosing where to save it.
'Am I asking too much???
Question1
Question2
'Question3 (See SavedFileName sub below)
End Sub
'Call the FilePicker so the user can pick the 2 appropriate files
'Notice here that we will loop twice, changing the title of the dialogue box each time.
'We will also capture the names of the workbooks to 2 variables...
'..."AllMonths" & "LatestMonth" so we can refer to them simply elsewhere
FilePickerButchered
End Sub
'Turn off the display alert (for when we delete the sheets we're done with
Application.DisplayAlerts = False
Next X
End Sub
'This sub opens the file selection dialogue box, and allows the user to select...
'...a file they want to do something with. Note, it only lets the user PICK a...
'file, you then have to decide what to do with it!
'Add a filter to show only Excel files (you can add multiple types one after the other)
.Filters.Clear 'You must clear old filters first
.Filters.Add "Excel Files", "*.xls?"
'.Filters.Add "Word Files ", "*.doc?"
'Actually shows the dialoge box, and captures either 0 (something was selected), or...
'-1 (nothing was selected, or Cancel was hit)
WhatWasClicked = .Show
'Read the name of the selected file (this includes the path by the way)
'...temporarily to the variable "FileName"...we will assign it to our declared
'...variable name, once we establish if this was the first, or second file selected (see below)
FileName = .SelectedItems(1)
'Open the selected file...
Workbooks.Open (FileName)
'Now we establish if this was the 1st, or 2nd file opened (using the current value of X)
If X = 1 Then AllMonths = ActiveWorkbook.Name
If X = 2 Then LatestMonth = ActiveWorkbook.Name
End If
End With
Next X
End Sub
'Question (3) saving this new file automatically in a new months folder but choosing where to save it.
'Folder names for the months (for the rest of the directory path)
If MonthName(Month(Now) - 1) = "January" Then MyYearAndMonth = Year(Now) & "\01 January\"
If MonthName(Month(Now) - 1) = "February" Then MyYearAndMonth = Year(Now) & "\02February\"
If MonthName(Month(Now) - 1) = "March" Then MyYearAndMonth = Year(Now) & "\03 March\"
If MonthName(Month(Now) - 1) = "April" Then MyYearAndMonth = Year(Now) & "\04 April\"
If MonthName(Month(Now) - 1) = "May" Then MyYearAndMonth = Year(Now) & "\05 May\"
If MonthName(Month(Now) - 1) = "June" Then MyYearAndMonth = Year(Now) & "\06 June\"
If MonthName(Month(Now) - 1) = "July" Then MyYearAndMonth = Year(Now) & "\07 July\"
If MonthName(Month(Now) - 1) = "August" Then MyYearAndMonth = Year(Now) & "\08 August\"
If MonthName(Month(Now) - 1) = "September" Then MyYearAndMonth = Year(Now) & "\09 September\"
If MonthName(Month(Now) - 1) = "October" Then MyYearAndMonth = Year(Now) & "\10 October\"
If MonthName(Month(Now) - 1) = "November" Then MyYearAndMonth = Year(Now) & "\11 November\"
If MonthName(Month(Now) - 1) = "December" Then MyYearAndMonth = Year(Now) - 1 & "\12 December\"
'Logical test to see if last month (the month we're reporting on)...
'...was December
If MonthName(Month(Now) - 1) <> "December" Then
'Save path for months 1-11
FileSavePathAndName = MyRoot & MyYearAndMonth & "Budget vs Actual - " & _
MonthName(Month(Now) - 1) & " " & Year(Now) & " " & MyCostCentre
Else
'Save path for month 12 (adjustment to the year (-1))
FileSavePathAndName = MyRoot & MyYearAndMonth & "Budget vs Actual - " & _
MonthName(Month(Now) - 1) & " " & Year(Now) - 1 & " " & MyCostCentre
End If
End Sub
Sheets(Array(MySheet1, MySheet2)).Select
'Sheets("Cost Centre1 All").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileSavePathAndName & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
'Sheets(Array(MySheet1, MySheet2)).Select
Sheets(Array(MySheet1, MySheet2)).Copy
ChDir "C:\Users\Alan\Desktop\Demo\2017\07 July"
ActiveWorkbook.SaveAs FileName:= _
FileSavePathAndName & ".xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
'This sub opens the file selection dialogue box, and allows the user to select...
'...a file they want to do something with. Note, ot only lets the user PICK a...
'file, you then have to decide what to do with it!
'The path and file name are written to, a variable...in this case FileName...
'...which we can then choose to open (FileName will have the path AND the file name automatically)
Dim FileName As String 'Holds the name of the selected file (and the path to it)
Dim WhatWasClicked As Integer 'Captures if a file was selected (0), or not (-1)
'Add a filter to show only Excel files (you can add multiple types one after the other)
'NOTE: The 1st argument is what appears in the box at the bottom left of the dialogue box
'The 2nd argument is the actual filter (* = any characted, ? = any letter)
'You must clear old filters first
.Filters.Clear
.Filters.Add "Excel Files", "*.xls?"
'.Filters.Add "Word Files ", "*.doc?"
'Actually shows the dialoge box, and captures either 0 (something was selected), or...
'-1 (nothing was selected, or Cancel was hit)
WhatWasClicked = .Show
End With
End Sub
'...The references to "Cost Centre1 Lastest", "Cost Centre1 All", were changed
'to the names we assigned to the two sheets, as was the path and file name
'...The references to "Cost Centre1 Lastest", "Cost Centre1 All", were changed
'to the names we assigned to the two sheets, as was the path and file name
November\"
2 December\"