Help - Trying to protect form buttons

odonnks

New Member
Joined
Feb 9, 2019
Messages
4
Hello,
I have a macro that works as needed. The sheet has form buttons I want to protect but when I unlock cells A10 to M50 and protect the sheet I receive error 1004 saying a cell its trying to write to is protected and read only.

I'm not sure what I'm missing.

Any help will be appreciated
Thank you



Sub Macro1()
'
' Macro1 Macro
' Clear and upload new cal report
'

'
Range("A10:M50").Select
Selection.ClearContents

'
' Macro1 Macro
' Read text file
'

Dim vFilename As Variant

vFilename = Application.GetOpenFilename( _
FileFilter:="Text Files (*.txt), *.txt", _
Title:="Select Text File", _
ButtonText:="Select", _
MultiSelect:=False)

If vFilename = False Then Exit Sub 'user cancelled

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & vFilename, Destination:=Range("$N$10"))
.Name = ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(15, 10, 13, 12, 1, 6, 7, 7, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("A:K").Select
Range("K1").Activate
Selection.Delete Shift:=xlToLeft
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
15,092
Office Version
  1. 2010
Platform
  1. Windows
First you must unprotect, later protect the sheet. Change "abc" for your password.

Code:
Sub Macro1()
'
' Macro1 Macro
' Clear and upload new cal report
'


[COLOR=#0000ff]ActiveSheet.Unprotect "abc"[/COLOR]


Range("A10:M50").Select
Selection.ClearContents


'
' Macro1 Macro
' Read text file
'


Dim vFilename As Variant


vFilename = Application.GetOpenFilename( _
FileFilter:="Text Files (*.txt), *.txt", _
Title:="Select Text File", _
ButtonText:="Select", _
MultiSelect:=False)


If vFilename = False Then Exit Sub 'user cancelled


With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & vFilename, Destination:=Range("$N$10"))
.Name = ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(15, 10, 13, 12, 1, 6, 7, 7, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("A:K").Select
Range("K1").Activate
Selection.Delete Shift:=xlToLeft


[COLOR=#0000ff]ActiveSheet.Protect "abc"[/COLOR]


End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,013
Messages
5,856,828
Members
431,835
Latest member
AJBud2022

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top