Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Help required with Excel Loops 1

Status
Not open for further replies.

hopelessliar

Technical User
Apr 29, 2002
89
GB
I have a macro that effectively protects a cell from being changed (see below) which I simply recorded. What I want to do, is turn this into a loop so that where A69 is selected and compared against A18, A70 will be compared to A19 and so on. I KNOW this is simple but I can't get the hang of it so any help would be greatfully received.

Thanks guys


Range("A69").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=A18"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "You cannot change this cell"
.InputMessage = ""
.ErrorMessage = _
"To protect the integrity of Station Lists, Stations can only be changed between A15 and A50." & Chr(10) & "" & Chr(10) & "Any changes made will affect all other brands." & Chr(10) & "" & Chr(10) & "Luc"
.ShowInput = True
.ShowError = True
End With
 
You could use

Formula1:="=" & selection.offset(-61,0).address

but are you sure you're not better off using the protection features of Excel?

Rob
[flowerface]
 
This sheet used to use protection features but I got fed up of users asking me why they couldn't type in it so I wnated to use an instructive error message.

I would imagine I would use the formaula you suggest like this:

Dim myRange as Range
For Each myRange in Range("A19:A25")
myRange.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & selection.offset(-61,0).address


.....>>
next myRange



Am I anywhere close?!
 
Exactly. Actually, I prefer not to use the selection object (it gets a little messy, and is hardly ever necessary) - you could simply use:

Dim myRange as Range
For Each myRange in Range("A19:A25")
With myRange.Validation
.Delete
.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & myRange.offset(-61,0).address

.....>>
next myRange

Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top