The Protect Worksheet was working when I had created the Excel spreadsheet by manually clicking the export button on the ribbon. I needed it more automated so I use the transfer spreadsheet 12xml to get the file in XLSX format. That works for the correct file format and I am still able to Protect Workbook. But the Worksheet Protect is failing. Run Time error 1004 Method "Protect of Object '_Worksheet" failed. I also tried just using WS.Protect to keep it simple and that failed the same way. I tried removing the WB.Worksheets("qryFCST_Template").Cells.Select line and that didn't help. I also tried moving the select line just above the Password Protect line and it still failed. Any suggestions would be appreciated
Here is the code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryFCST_Template", "C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx", True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qrySheetSummary", "C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx", True
Dim xlApp As Excel.Application
Dim WB As Excel.Workbook
Dim WS As Worksheet
Dim XLrange As Range
Set xlApp = New Excel.Application
Dim Z As Long
Set WB = xlApp.Workbooks.Open("C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx") 'file reference location
WB.Worksheets("qryFCST_Template").Cells.Select
Set WS = WB.Worksheets("qryFCST_TEMPLATE")
Z = WS.UsedRange.rows(WS.UsedRange.rows.Count).row
For i = 2 To Z
WS.Range("N" & i).Formula = "=L" & i & "+M" & i
Next i
WS.Cells.EntireColumn.AutoFit
WS.Cells.EntireRow.AutoFit
Set XLrange = WS.Range("A1:k1")
XLrange.Interior.Color = RGB(192, 192, 192) 'set the data columns from CASA range to gray
Set XLrange = WS.Range("L1:N1")
XLrange.Interior.Color = RGB(0, 255, 255) 'set the columns involved in INPUT to BLUE
Set XLrange = WS.Range("L2:M" & Z)
XLrange.Locked = False
WS.Protect Password:="BASA", allowformattingcells:=False, AllowFormattingColumns:=False, AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowUsingPivotTables:=False[/color]
WS.Name = "CASA WK41 TOPS DOWN FCST"
WB.Protect Password:="BASA"
WB.Save
WB.Close False
xlApp.Quit
Set xlApp = Nothing
Set WB = Nothing
Set WS = Nothing
Set XLrange = Nothing
Here is the code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryFCST_Template", "C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx", True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qrySheetSummary", "C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx", True
Dim xlApp As Excel.Application
Dim WB As Excel.Workbook
Dim WS As Worksheet
Dim XLrange As Range
Set xlApp = New Excel.Application
Dim Z As Long
Set WB = xlApp.Workbooks.Open("C:\Database\Excel_Output\WK41_FCST_COUNTRY.xlsx") 'file reference location
WB.Worksheets("qryFCST_Template").Cells.Select
Set WS = WB.Worksheets("qryFCST_TEMPLATE")
Z = WS.UsedRange.rows(WS.UsedRange.rows.Count).row
For i = 2 To Z
WS.Range("N" & i).Formula = "=L" & i & "+M" & i
Next i
WS.Cells.EntireColumn.AutoFit
WS.Cells.EntireRow.AutoFit
Set XLrange = WS.Range("A1:k1")
XLrange.Interior.Color = RGB(192, 192, 192) 'set the data columns from CASA range to gray
Set XLrange = WS.Range("L1:N1")
XLrange.Interior.Color = RGB(0, 255, 255) 'set the columns involved in INPUT to BLUE
Set XLrange = WS.Range("L2:M" & Z)
XLrange.Locked = False
WS.Protect Password:="BASA", allowformattingcells:=False, AllowFormattingColumns:=False, AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, AllowUsingPivotTables:=False[/color]
WS.Name = "CASA WK41 TOPS DOWN FCST"
WB.Protect Password:="BASA"
WB.Save
WB.Close False
xlApp.Quit
Set xlApp = Nothing
Set WB = Nothing
Set WS = Nothing
Set XLrange = Nothing