VBA Lock then Copy question

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to figure out a way for a button click to lock a specified range of cells Then to Copy other specified cells.


I'm working with the following:


Code:
 Dim response As VbMsgBoxResult
response = MsgBox("ONCE YOU SELECT THIS BUTTON:                                                                                                                                                                                                                                      IT WILL COPY THE INFORMATION AND LOCK THE SHEET!!!                                                                                                                                                                                       DO YOU WANT TO CONTINUE?", vbYesNo, "ProjectUpdate!")
  

Range("B11:U180").Select
  
  ActiveSheet.Protect Password:="[EMAIL="G@cvXpcpSp96"]PASSWORD[/EMAIL]", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowSorting:=True
  
  
Worksheets(2).Range("C182:D182").Copy

Exit Sub
   If response = vbNo Then
    MsgBox "            PLEASE MAKE SURE THAT Your info is UP-TO-DATE!", vbCritical, "ProtectUpdate!"
End If
    End Select
       
Exit Sub
End Sub

If you run this, you'll see what I'm talking about. It won't lock the specified range and then it won't copy the other range??


I can't seem to figure this one out.

Anyone have any suggestions on how to write this code??

Thanks,
Pinaceous
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
For the first part, you haven't locked the cells, just selected them. Try:

Range("B11:U180").Locked = True

For the second, you copied, but didn't specify a destination. Try:

Worksheets(2).Range("C182:D182").Copy Destination:=Sheets(1).Range("A1")

Change the Destination as needed. Note that "Destination:=" isn't required, I just put it there for clarity.

HTH
 
Upvote 0
Just change

Code:
Range("B11:U180").Select

to

Code:
Range("B11:U180").Locked = True

You'll probably want to unlock all the cells first though.


You'll need to specify a destination for your copy, eg.

Code:
Worksheets(2).Range("C182:D182").Copy Worksheets(1).Range("A1")
 
Upvote 0
Hello everyone,

Thanks for cleaning up a few of those issues.

I still have a question.

What if I don't specify a destination for the paste and just have it highlight those specified cells for the user to select its destination independently from the sub. Is this possible?

I know it is possible when it stands alone, for example:
Code:
Sub Copy() Worksheets(2).Range("C182:D182").Copy End Sub


Any more helpful hints??

Thank you both for responding!

Pinaceous
 
Upvote 0
Also, how could I password lock my original proposed selection??

Thanks
 
Upvote 0
What if I don't specify a destination for the paste and just have it highlight those specified cells for the user to select its destination independently from the sub. Is this possible?

That could be complicated, especially if the destination isn't on the same sheet as the copy range. I'd let/ask the user to select the range first, then feed it to the destination. Take a look at the Application.InputBox method in the helpfile for that.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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
Back
Top