|
|
Question : How to prevent Run Time error 1004, the cell or chart you are trying to change is protected ?
|
|
I am developing an Access application with Excel using an Access interface. I import a txt file into an Excel workbook.
When I execute the following subroutine for the first time, it runs successfully and the Excel workbook gets populated. However, when I execute this subroutine for a 2nd time, I get the following:
Run-time error 1004 The cell or chart you are trying to change is protected and therefore read only.
To modify a protected cell or chart, first remove protection using the Unprotect Sheet command (Tools menu, protection submenu). You may be prompted for a password.
The compiler stops on the line:
xlWb2.ActiveSheet.UsedRange.Copy Destination:=xlWsh.Range("A9")
----------------------------------------------------------
Private Sub Wachovia_Ret_Edit() Dim xlApp As Excel.Application Dim xlWb1 As Excel.Workbook, xlWb2 As Excel.Workbook Dim xlWsh As Excel.Worksheet Dim fil As String, irows As Integer Dim iShift As Integer fil = "C:\wachoviartn1.txt" ' Text file import - change folder and filename Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True xlApp.DisplayAlerts = False Set xlWb1 = xlApp.Workbooks.Open("C:\wachovia.xls") ' Existing Excel workbook - change folder and filename Set xlWb2 = xlApp.Workbooks.Open(fil) Set xlWsh = xlWb1.Worksheets("RTN") ' Change to sheet of interest irows = xlWb2.ActiveSheet.UsedRange.Rows.count xlWb2.ActiveSheet.UsedRange.Copy Destination:=xlWsh.Range("A9") xlWb2.Close SaveChanges:=False xlWsh.Cells.Font.Size = 6 xlWsh.Range("B7").HorizontalAlignment = xlRight xlWsh.Range("D7").HorizontalAlignment = xlRight xlWsh.Range("E:E").ColumnWidth = 85 xlWsh.Range("A1") = "M S" xlWsh.Range("A2") = "Return Item Instructions" xlWsh.Range("A4") = "To: W Bank" xlWsh.Range("A5") = "Att: _____________________" xlWsh.Range("A7") = "Account Number" xlWsh.Range("B7") = "Paid Date" xlWsh.Range("C7") = "Check Number" xlWsh.Range("D7") = "Amount" xlWsh.Range("E7") = "Reason For Return " xlWsh.Range("E1") = "Date: " & Format(Now(), "mm/dd/yyyy") With xlWsh.Range("D9") With .Offset(irows + 7).Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With End With With xlWsh.Range("A9") .Resize(irows).TextToColumns Destination:=xlWsh.Range("A9"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="|", _ FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 2), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True .Resize(RowSize:=irows, ColumnSize:=5).Borders.LineStyle = xlContinuous With .Offset(irows + 7).Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Offset(irows + 8) .Offset(0, 0) = "Gary O" .Offset(0, 3) = "W Bank" .Offset(1, 0) = "Karen H" .Offset(2, 0) = "Jeannine E" .Offset(3, 0) = "Cinzia M" .Offset(5, 0) = "M S" .Offset(5, 3) = "Return Items Area" .Offset(7, 0) = "Bank Dept." .Offset(7, 3) = "Acknowledgement" .Offset(8, 0) = " (517) 790-5436,5427" .Offset(9, 0) = "Fax # (517) 790-5444,5445" .Offset(9, 3) = "Fax # (515) 433-2153" .Offset(11, 1) = "Page _____ Of _____" End With End With xlWsh.Columns.AutoFit xlWsh.Columns("B:B").EntireColumn.AutoFit xlWsh.Columns("D:D").EntireColumn.AutoFit xlWsh.Cells.Locked = True xlApp.Intersect(xlWsh.Range("A" & irows + 8 & ":D" & irows + 16), xlWsh.UsedRange).Locked = False xlApp.Intersect(xlWsh.Range("E9:E" & irows + 16), xlWsh.UsedRange).Locked = False 'start at row 9 plus [irows - 1] plus 8 additional rows xlWsh.Range("D9:D65535").NumberFormat = "#,###.00" xlWsh.Protect PASSWORD:="myPassword" xlApp.DisplayAlerts = True With xlWsh.PageSetup .LeftMargin = xlApp.InchesToPoints(0.1) .RightMargin = xlApp.InchesToPoints(0.1) .TopMargin = xlApp.InchesToPoints(0.1) .BottomMargin = xlApp.InchesToPoints(0.1) .HeaderMargin = xlApp.InchesToPoints(0.1) .FooterMargin = xlApp.InchesToPoints(0.1) .Orientation = xlLandscape .FitToPagesWide = 1 .FitToPagesTall = 1 End With Set xlWsh = Nothing Set xlWb1 = Nothing Set xlWb2 = Nothing Set xlApp = Nothing End Sub
|
Answer : How to prevent Run Time error 1004, the cell or chart you are trying to change is protected ?
|
|
Try to unprotect worksheet before the line where it stops xlWsh.Unprotect "myPassword"
Regards, Curt
|
|
|
|
|