Stop macro if cell selected

C

carruthers

Hi,

First time & new to excel macros. I have a macro which copies data &
inserts it into a viewable summary sheet which can be printed. The
range is 6 cells & if cell k7 is selected I want to stop the macro. I
have attempted to use IF command with Then End but it ignores it. Here
is my macro:

' refence point to start pasting sequence
Sheets("Alfa data").Select
Range("C225").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("c7").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
If ActiveCell.Address = "K7" Then End 'This is my problem area,
I want it to stop here.
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("Alfa Data").Select
Range("C226").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("c7").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(2, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.ScreenUpdating = True
Application.CutCopyMode = False
Range("A1").Select
 
D

Don Guillett

If ActiveCell.Address = "K7"
If ActiveCell.Address = "$K$7" then exit sub
You need to re-write to avoid selections since they are rarely necessary or
desirable.
example of a one liner
Sheets("Alfa data").range("C225").Copy
Sheets("Sheet4").Range("c7").End(xlToRight).Offset(,1)
 
C

carruthers

Don,

Adding "sub" did the trick, this is exactly what I needed to happen.
Thank you very much.
 
D

Don Guillett

Are you sure? Did you change the address to "$K$7" with the $$$$?
Did you re-write to remove selections?
 
C

carruthers

Hi Don,

Yes I added the $'s, I also removed the selections. Very neat.

Thank you again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top