I have a MS Access database that exports a queries as sheets to an excel file. Then (this is the part where I’m getting stuck) it is supposed to open the newly created excel file, apply conditional formatting, close and save the excel file. And finally, create a folder (using today’s date) and moving the excel file to the newly created folder.
Here is the code that I am using. I get a run-time error ’75’ (path/file access). But sometimes the code gets stuck in the middle of formatting the excel file. Any help will be appreciated. Note: the code for conditional formatting is from ‘recording a macro’.
DoCmd.TransferSpreadsheet acExport, 10, "query1", _
"C:ArchiveMyFile " & Format(Date, "mm-DD-yy") & ".xlsx", True, "MySheet1"
DoCmd.TransferSpreadsheet acExport, 10, "query2", _
"C:ArchiveMyFile " & Format(Date, "mm-DD-yy") & ".xlsx", True, "MySheet2"
Call createFolder
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Excel.Workbook
Dim xlSh As Object 'Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") 'New Excel.Application
Set xlWB = xlApp.Workbooks.Open("c:ArchiveMyFile " & Format(Date, "mm-DD-yy") & ".xlsx")
Set xlSh = xlWB.Sheets("MySheet1")
xlApp.Visible = True
Set xlWB = ActiveWorkbook
'What follows is code from recording a macro to format a date column with red
Sheets("MySheet1").Select
Selection.AutoFilter
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
ActiveWindow.LargeScroll ToRight:=-1
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(LEN(H2)>0,TODAY()-H2>=15)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A2").Select
'the close the book
Close Workbook
'move today's file to folder with today's date
Dim sfolder As String
Dim dfloder As String
sfolder = "c:Archive"
dfolder = "c:Archive" & Format(Date, "mm-DD-yy") & ""
Name sfolder & "MyFile " & Format(Date, "mm-DD-yy") & ".xlsx" As dfolder & "prefix " & Format(Date, "mm-DD-yyyy") & ".xlsx"
End function
Public Sub createFolder()
'Create the subfolders with today's date
If Len(Dir("c:\Archive" & Format(Date, "mm-DD-yy"), vbDirectory)) = o Then
MkDir "c:Archive" & Format(Date, "mm-DD-yy")
Else
MsgBox ("folder with today's date already exists. Plesae check.")
End If
End Sub