View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default SQL to unprotect and protect worksheet

That does not look all bad to me... Here would be my take on it...

Sub Workbook_Open()
Const pw As String = "test"

'Sheets("Distribution").Select
Sheets("Distribution").Protect pw, UserInterfaceOnly:=True

with Sheets("Totals").
.Unprotect pw
.range("A1").QueryTable.Refresh BackgroundQuery:=True 'Change A1?
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
end with

End Sub

--
HTH...

Jim Thomlinson


"drewship" wrote:

Jim,
Here is the macro:

Sheets("Totals").Select
ActiveSheet.Unprotect
Selection.QueryTable.Refresh BackgroundQuery:=False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

I want to put this in the workbook module so it runs when the workbook
opens, but I also want to protect several of the sheets before the user has
access to the spreadsheet. I am working on something like:

Sub Workbook_Open()
Dim pw As String: pw = "test"

Sheets("Distribution").Select
Sheets("Distribution").Protect pw, UserInterfaceOnly:=True

Sheets("Totals").Select
Sheets("Totals").Protect pw, UserInterfaceOnly:=True

Sheets("Totals").Select
ActiveSheet.Unprotect pw
Selection.QueryTable.Refresh BackgroundQuery:=True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub

Thanks.

"Jim Thomlinson" wrote:

Try recording a macro where you Unprotect the sheet, Refresh the query(s) and
re-protect the sheet. Attach that code to a button or if you want we can put
in in the workbook open event. Post what you get and we can run through it.
General questions would be the most logical spot for this type of question
when you do not want a macro answer...
--
HTH...

Jim Thomlinson


"drewship" wrote:

Thanks Jim. There is no SQL topic so I thought Programming was as close as I
would get to a correct topic.

If a macro can do this before the SQL kicks in then that will work for me. I
already have the protect and unprotect statements I need. How would I design
a macro to run before the SQL statements?

"Jim Thomlinson" wrote:

2 issues... The only way to do it is via macros. The other issue is that you
posted you question in the programming section where the most likely response
is going to involve a macro.
--
HTH...

Jim Thomlinson


"drewship" wrote:

Hello all.
I have 2 SQL queries that automatically import data from an Access database
into 2 different sheets on my spreadsheet when the spreadsheet opens. I need
to unprotect the sheet prior to the import, then protect it after the import.
I have looked at several posts but they all have to do with macros. Any help
would be appreciated.

Thanks in advance!!
Andrew