ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code does not select (https://www.excelbanter.com/excel-programming/454184-code-does-not-select.html)

[email protected]

Code does not select
 
I have this code

Dim Bruger As String
Bruger = UCase(InputBox("Indtast brugernavn"))
If Bruger = "BUTTERFLY" Then
ActiveSheet.Unprotect
Range("2:2,4:4,8:8").Select
Selection.Locked = False
ActiveSheet.Protect Contents:=True
ActiveSheet.EnableSelection = xlUnlockedCells
Else
ActiveSheet.Unprotect
Cells.Select
Selection.Locked = True
ActiveSheet.Protect
Exit Sub

For some reason it doesn't select anything, though it did on thr very first run.

Any ideas.

Jan

Claus Busch

Code does not select
 
Hi Jan,

Am Thu, 8 Nov 2018 06:10:46 -0800 (PST) schrieb :

[Code snipped]
For some reason it doesn't select anything, though it did on thr very first run.


you must not select when you refer directly.
Try:

Dim Bruger As String
Dim LCol As Integer
Dim LRow As Long

Bruger = InputBox("Indtast brugernavn")
With ActiveSheet
LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If UCase(Bruger) = "BUTTERFLY" Then
.Unprotect
Intersect(.Range(.Cells(1, 1), .Cells(LRow, LCol)), _
.Range("2:2, 4:4, 8:8")).Locked = False
.Protect Contents:=True
.EnableSelection = xlUnlockedCells
Else
.Unprotect
.Range(.Cells(1, 1), .Cells(LRow, LCol)).Locked = True
.Protect
End If
End With


Regards
Claus B.
--
Windows10
Office 2016

[email protected]

Code does not select
 
torsdag den 8. november 2018 kl. 15.42.03 UTC+1 skrev Claus Busch:
Hi Jan,

Am Thu, 8 Nov 2018 06:10:46 -0800 (PST) schrieb :

[Code snipped]
For some reason it doesn't select anything, though it did on thr very first run.


you must not select when you refer directly.
Try:

Dim Bruger As String
Dim LCol As Integer
Dim LRow As Long

Bruger = InputBox("Indtast brugernavn")
With ActiveSheet
LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If UCase(Bruger) = "BUTTERFLY" Then
.Unprotect
Intersect(.Range(.Cells(1, 1), .Cells(LRow, LCol)), _
.Range("2:2, 4:4, 8:8")).Locked = False
.Protect Contents:=True
.EnableSelection = xlUnlockedCells
Else
.Unprotect
.Range(.Cells(1, 1), .Cells(LRow, LCol)).Locked = True
.Protect
End If
End With


Regards
Claus B.
--
Windows10
Office 2016


Thank you. That did the trick.

Jan


All times are GMT +1. The time now is 12:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com