Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Unprotect and protect sheet in a macro

Hello

When I run this macro half the time I get a fault in the paste special line,
The worksheet I paste into is protected (no password) so in the beginning I
unprotect it and in the end I protect it. It says the pastespecial method
is not working. Help me please, it drives me nuts. If the sheet is not
protected in the beginning all is working fine.

Sub kopiera()

Application.ScreenUpdating = False
Sheets("Faktura").Activate
Range("J5").Select
Selection.Copy
Workbooks.Open Filename:="C:\Excelprojekt/Faktura Åkeri\Reskontra1.xls"
Windows("Reskontra1.xls").Activate
Sheets("Reskontra").Activate
Sheets("Reskontra").Unprotect
Columns("A:A").Select
Selection.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(0, 0).Range("a1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
False, Transpose:=False

Windows("Faktura.xls").Activate
Sheets("Faktura").Activate
Range("G5").Select
Selection.Copy
Windows("Reskontra1.xls").Activate
Sheets("Reskontra").Activate
Columns("B:B").Select
Selection.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(0, 0).Range("a1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
False, Transpose:=False
Sheets("Reskontra").Protect
ActiveWorkbook.Save
ActiveWindow.Close



ActiveWorkbook.SaveAs Filename:="c:\Excelprojekt/Faktura
Åkeri\Fakturor\" & Range("G5") - 1 & "xls", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=True,
CreateBackup:=False

Sheets("Faktura").Select
Range("A21:B36").Select
Selection.ClearContents
Range("H5").Select
Selection.ClearContents
Range("H21:I36").Select
Selection.ClearContents

Range("A21").Select

Application.ScreenUpdating = True

ActiveWorkbook.SaveAs Filename:="C:\Excelprojekt/Faktura
Åkeri\Faktura.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False

Application.Quit

End Sub

TIA

Ola Sigurdh


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Unprotect and protect sheet in a macro

It's difficult to tell what's going on, but instead of using all the selects,
maybe you could use something like:

Option Explicit
Sub testme()

Dim wkbk As Workbook
Dim FoundCell As Range

Set wkbk = Workbooks.Open _
(Filename:="C:\Excelprojekt/Faktura Åkeri\Reskontra1.xls")

With wkbk.Worksheets("Reskontra")
With .Range("a:a")
Set FoundCell = .Cells.Find(what:="whatever", _
after:=.Cells(.Cells.Count), _
rest of .find command here)

End With

If FoundCell Is Nothing Then
MsgBox "not found"
Else
.Unprotect
FoundCell.Value _
= Workbooks("faktura.xls").Worksheets("faktura").Ran ge("a6")
.Protect
End If

End With

End Sub

I'm guessing that something is killing the contents of the clipboard--but since
you're just pasting values, you could just assign the values.

But you'll have to test it. I had no idea what to findnext or what the parms of
..Find should be (xlwhole/xlpart, etc).



Ola Sigurdh wrote:

Hello

When I run this macro half the time I get a fault in the paste special line,
The worksheet I paste into is protected (no password) so in the beginning I
unprotect it and in the end I protect it. It says the pastespecial method
is not working. Help me please, it drives me nuts. If the sheet is not
protected in the beginning all is working fine.

Sub kopiera()

Application.ScreenUpdating = False
Sheets("Faktura").Activate
Range("J5").Select
Selection.Copy
Workbooks.Open Filename:="C:\Excelprojekt/Faktura Åkeri\Reskontra1.xls"
Windows("Reskontra1.xls").Activate
Sheets("Reskontra").Activate
Sheets("Reskontra").Unprotect
Columns("A:A").Select
Selection.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(0, 0).Range("a1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
False, Transpose:=False

Windows("Faktura.xls").Activate
Sheets("Faktura").Activate
Range("G5").Select
Selection.Copy
Windows("Reskontra1.xls").Activate
Sheets("Reskontra").Activate
Columns("B:B").Select
Selection.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(0, 0).Range("a1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
False, Transpose:=False
Sheets("Reskontra").Protect
ActiveWorkbook.Save
ActiveWindow.Close

ActiveWorkbook.SaveAs Filename:="c:\Excelprojekt/Faktura
Åkeri\Fakturor\" & Range("G5") - 1 & "xls", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=True,
CreateBackup:=False

Sheets("Faktura").Select
Range("A21:B36").Select
Selection.ClearContents
Range("H5").Select
Selection.ClearContents
Range("H21:I36").Select
Selection.ClearContents

Range("A21").Select

Application.ScreenUpdating = True

ActiveWorkbook.SaveAs Filename:="C:\Excelprojekt/Faktura
Åkeri\Faktura.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False

Application.Quit

End Sub

TIA

Ola Sigurdh


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to protect/unprotect a sheet using a macro with password FredH Excel Discussion (Misc queries) 5 October 23rd 07 04:49 PM
Protect/Unprotect Sheet from Macro Nigel[_8_] Excel Programming 1 May 14th 04 05:55 PM
Excel VBA-Protect and unprotect sheet with BVA marfrk Excel Programming 1 May 3rd 04 12:37 PM
Protect/unprotect sheet with password with VBA? dragontale[_7_] Excel Programming 1 April 19th 04 09:29 PM
VBA code - protect and unprotect a sheet Jeff Excel Programming 2 December 2nd 03 11:44 PM


All times are GMT +1. The time now is 06:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"