ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Restricting Macro Execution (https://www.excelbanter.com/excel-programming/284282-re-restricting-macro-execution.html)

John Wilson

Restricting Macro Execution
 
Terry,

Without going into a lot of detail deciphering your code, why not just
Select A1 when the macro starts? That way, it'll always run from the
correct cell.

e.g.
Instead of
If ActiveCell.Range("A1") Then
use
Range("A1").Select

You can then get rid of the Else, End If statements

John


Terry wrote:

I have used use Excel to create macro. I am trying to
modify macro to execute only when I put cursor in first
cell (A1). Otherwise, I want the macro to print out
statement in Message Box "Put Cursor in Correct Cell".
Any ideas? Thanks.
Terry

Sub SortRows()
'
' SortRow2 Macro
' Keyboard Shortcut: Ctrl+t

If ActiveCell.Range("A1") Then
ActiveCell.Range("A1:I19").Select
Selection.Sort Key1:=ActiveCell, Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
ActiveCell.Range("A1:I19").Select
Selection.Interior.ColorIndex = xlNone
ActiveCell.Range( _
"A1:I1,A3:I3,A5:I5,A7:I7,A9:I9,A11:I11,A13:I13 ,A15
:I15,A17:I17,A19:I19").Select
ActiveCell.Offset(18, 0).Range("A1").Activate
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Else
MsgBox "Put Cursor in Correct Cell"
End If
End Sub



Terry

Restricting Macro Execution
 
John,
If the macro is executed from other cell it could screw
up the whole worksheet. What I have presented here is
only a sample.
Terry

-----Original Message-----
Terry,

Without going into a lot of detail deciphering your

code, why not just
Select A1 when the macro starts? That way, it'll always

run from the
correct cell.

e.g.
Instead of
If ActiveCell.Range("A1") Then
use
Range("A1").Select

You can then get rid of the Else, End If statements

John


Terry wrote:

I have used use Excel to create macro. I am trying to
modify macro to execute only when I put cursor in first
cell (A1). Otherwise, I want the macro to print out
statement in Message Box "Put Cursor in Correct Cell".
Any ideas? Thanks.
Terry

Sub SortRows()
'
' SortRow2 Macro
' Keyboard Shortcut: Ctrl+t

If ActiveCell.Range("A1") Then
ActiveCell.Range("A1:I19").Select
Selection.Sort Key1:=ActiveCell,

Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
ActiveCell.Range("A1:I19").Select
Selection.Interior.ColorIndex = xlNone
ActiveCell.Range( _
"A1:I1,A3:I3,A5:I5,A7:I7,A9:I9,A11:I11,A13:I13 ,

A15
:I15,A17:I17,A19:I19").Select
ActiveCell.Offset(18, 0).Range("A1").Activate
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Else
MsgBox "Put Cursor in Correct Cell"
End If
End Sub


.



All times are GMT +1. The time now is 08:44 AM.

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