Is there a tidier way to write this code?

CarrieAWalton

New Member
Joined
Sep 4, 2017
Messages
12
Hi all,

I'm updating a time in motion sheet at work. I have a worksheet full of command buttons which all input information into worksheets named "1st" through to "31st". These worksheets need to be password protected so that users can SEE the info contained within them, but not be able to EDIT the sheets (otherwise people fiddle their figures), so I'm adding code to unprotect the sheet, input the data, then protect it again.

However, my current code is messy (I don't fully understand VBA and tend to just bumble my way through) and I was hoping you lovely folks might be able to help me tidy it up. Here's a section of the code from one of my command buttons. I have eighteen to add the password unprotect/protect code to.

I hope you can help :)

Code:
Private Sub CommandButton6_Click()
'Call-Inbound
If Range("A2") = "1" Then
ans = InputBox("Case reference:", "Input")
Sheets("1st").Unprotect Password:="TIM"
Sheets("1st").Range("A" & Sheets("1st").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = ans
Sheets("1st").Protect Password:="TIM"
    End If
    If Range("A2") = "2" Then
ans = InputBox("Case reference:", "Input")
Sheets("2nd").Unprotect Password:="TIM"
Sheets("2nd").Range("A" & Sheets("2nd").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = ans
Sheets("2nd").Protect Password:="TIM"
    End If
    If Range("A2") = "3" Then
ans = InputBox("Case reference:", "Input")
Sheets("3rd").Unprotect Password:="TIM"
Sheets("3rd").Range("A" & Sheets("3rd").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = ans
Sheets("3rd").Protect Password:="TIM"
    End If
    If Range("A2") = "4" Then
ans = InputBox("Case reference:", "Input")
Sheets("4th").Unprotect Password:="TIM"
Sheets("4th").Range("A" & Sheets("4th").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = ans
    End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Or indeed is there any way of saying to Excel "any time a command button is clicked, unprotect any input sheet using password xx then protect again using password xx"

Hmm. Whilst that sounds easier, it sounds like it wouldn't be possible...
 
Upvote 0
Do all the buttons have similar code? If so, what differs between them?

You could certainly refactor that code like this:
Code:
Private Sub CommandButton6_Click()
'Call-Inbound
If Range("A2") = "1" Then
AddToSheet Sheets("1st")
ElseIf Range("A2") = "2" Then
AddToSheet Sheets("2nd")
elseIf Range("A2") = "3" Then
AddToSheet Sheets("3rd")
elseIf Range("A2") = "4" Then
AddToSheet Sheets("4th")
    End If
End Sub

Sub AddToSheet(ws as worksheet)
ans = InputBox("Case reference:", "Input")
with ws
   .Unprotect Password:="TIM"
   .Range("A" & .Rows.Count).End(xlUp).Offset(1).Value = ans
   .Protect Password:="TIM"
End With
End Sub
 
Upvote 0
Hi, wow thanks for the quick reply Rory!

The code for 16 of the buttons is exactly the same, they just add the data to different columns in the worksheets.

So in the Sub Addtosheet you've mentioned above, how would I distinguish which button added to which column?
 
Upvote 0
You could add an additional column letter argument like this:

Code:
Private Sub CommandButton6_Click()
'Call-Inbound
If Range("A2") = "1" Then
AddToSheet Sheets("1st"), "A"
ElseIf Range("A2") = "2" Then
AddToSheet Sheets("2nd"), "A"
elseIf Range("A2") = "3" Then
AddToSheet Sheets("3rd"), "A"
elseIf Range("A2") = "4" Then
AddToSheet Sheets("4th"), "A"
    End If
End Sub

Sub AddToSheet(ws as worksheet, sColumn as String)
ans = InputBox("Case reference:", "Input")
with ws
   .Unprotect Password:="TIM"
   .Range(sColumn & .Rows.Count).End(xlUp).Offset(1).Value = ans
   .Protect Password:="TIM"
End With
End Sub

so you can call Addtosheet from all the buttons. Assuming the logic to select the sheet name is the same for all, you might make that into a function to return the relevant sheet name too.
 
Upvote 0
Rory I owe you a very large drink :cool:

Not only has that neatened it up, it's made it a lot quicker to alter should anything need changing. Thanks so very VERY much xx
 
Upvote 0
Glad to help. :biggrin: I like gin...
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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