If .Column > 7 And .Row > 25 then go to B8

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

Hi experts, this second line of code is making me feel foolish.
The first line does its thing where data is entered and upon enter moves
to the right until column 8 then correctly act like a carriage
return back to column B and next row down.

Once I get past G25 I want to return to B8... what have I got screwed up in
the second line?

If ActiveCell.Column > 7 Then ActiveCell.Offset(1, -6).Select
If ActiveCell.Column > 7 And ActiveCell.Row > 25 Then _
ActiveCell.Offset(-18, -6).Select

Thanks,
Howard
 
Howard said:
Once I get past G25 I want to return to B8... what have I got
screwed up in the second line?
If ActiveCell.Column > 7 Then ActiveCell.Offset(1, -6).Select
If ActiveCell.Column > 7 And ActiveCell.Row > 25 Then _
ActiveCell.Offset(-18, -6).Select

If ActiveCell.Column > 7 Then _
ActiveCell.Offset(IIf(ActiveCell.Row > 25,-18,1), -6).Select
 
joeu2004 said:
Howard said:
Once I get past G25 I want to return to B8... what have I got
screwed up in the second line?
[....]
If ActiveCell.Column > 7 Then _
ActiveCell.Offset(IIf(ActiveCell.Row > 25,-18,1), -6).Select

Alternatively:

If ActiveCell.Column > 7 Then _
If ActiveCell.Row > 25 Then Range("B8").Select _
Else ActiveCell.Offset(1,-6).Select
 
Howard said:
Hi experts, this second line of code is making me feel foolish.
The first line does its thing where data is entered and upon enter moves
to the right until column 8 then correctly act like a carriage
return back to column B and next row down.

Once I get past G25 I want to return to B8... what have I got screwed up in
the second line?

If ActiveCell.Column > 7 Then ActiveCell.Offset(1, -6).Select
If ActiveCell.Column > 7 And ActiveCell.Row > 25 Then _
ActiveCell.Offset(-18, -6).Select

Rather than moving around via offsets, just go straight to the cell you want:

If ActiveCell.Column > 7 Then
If ActiveCell.Row > 24 Then
Range("B8").Select
Else
Cells(ActiveCell.Row + 1, 2).Select
End If
Else
Cells(ActiveCell.Row, 8).Select
End If

Doing it this way is also somewhat clearer than your method, IMHO.
 
Howard wrote:












Rather than moving around via offsets, just go straight to the cell you want:



If ActiveCell.Column > 7 Then

If ActiveCell.Row > 24 Then

Range("B8").Select

Else

Cells(ActiveCell.Row + 1, 2).Select

End If

Else

Cells(ActiveCell.Row, 8).Select

End If



Doing it this way is also somewhat clearer than your method, IMHO.

Hi Auric_

Thanks for helping me out.
I tried all three suggestions and can't make it work. Here is the whole code and just to recap, this is what I'm trying to do.

Start in B8 and type in two characters and hit enter, code takes me to the next cell right and repeats until it gets to column 8, then does a carriage like return to column B next row down. When it gets to G25 and enter is hit should go back to B8.

Your codes sure look like that should happen but at G25 I get the carraige like return and if I continue to enter two characters and enter it just carriage returns on down column B. (out of range as the very first statement prompts a exit sub)

I'm puzzled to say the least, perhaps a look at the entire code will shed some light.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
' Active Range B8 to G25
If ActiveCell.Row < 8 Or ActiveCell.Row > 25 Or _
ActiveCell.Column < 2 Or ActiveCell.Column > 7 Then
Exit Sub
End If

Dim MyString, MyLen
MyLen = Target
MyString = Target.Value
MyLen = Len(MyString)

If MyLen = 2 Then
If ActiveCell.Column > 7 Then
If ActiveCell.Row > 24 Then
Range("B8").Select
Else
Cells(ActiveCell.Row + 1, 2).Select
End If
Else
Cells(ActiveCell.Row, 8).Select
End If
End If

End Sub

Thanks,
Howard
 
Howard,

Try this:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
' Active Range B8 to G25
If Target.Row < 8 Or Target.Row > 25 Or _
Target.Column < 2 Or Target.Column > 7 Then
Exit Sub
End If

If Len(Target.Value) = 2 Then
Select Case Target.Column
Case 2 To 6
Target.Offset(0, 1).Select
Case 7
If Target.Row = 25 Then
Range("B8").Select
Else
Target.Offset(1, -5).Select
End If
End Select
End If

End Sub
 
Howard,



Try this:



Option Explicit



Private Sub Worksheet_Change(ByVal Target As Range)

' Active Range B8 to G25

If Target.Row < 8 Or Target.Row > 25 Or _

Target.Column < 2 Or Target.Column > 7 Then

Exit Sub

End If



If Len(Target.Value) = 2 Then

Select Case Target.Column

Case 2 To 6

Target.Offset(0, 1).Select

Case 7

If Target.Row = 25 Then

Range("B8").Select

Else

Target.Offset(1, -5).Select

End If

End Select

End If



End Sub

Smack on, Ben. Why does it look so easy when you have the correct solution? <G>
Thanks again, I appreciate it and Auric_'s efforts also!

Regards,
Howard
 
Howard said:
Smack on, Ben. Why does it look so easy when you have the correct
solution? <G>
Thanks again, I appreciate it and Auric_'s efforts also!

....and joeu2004.
 
How about...
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
' Active Range B8 to G25

If Not Intersect(Target, Range($B$8:$G$25) Then Exit Sub
If Len(Target.Value) = 2 Then
Select Case Target.Column
Case 2 To 6
Target.Offset(0, 1).Select
Case 7
If Target.Row = 25 Then
Range("B8").Select
Else
Target.Offset(1, -5).Select
End If
End Select
End If

End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
How about...







If Not Intersect(Target, Range($B$8:$G$25) Then Exit Sub













--

Garry



Free usenet access at http://www.eternal-september.org

Classic VB Users Regroup!

comp.lang.basic.visual.misc

microsoft.public.vb.general.discussion

Hi GS,

Much cleaner than "If yada-yada End If".

However, it does error out with "Invalid Character" as soon as I pasted it in my code, entire line to red and refers to first $ sign. ??

Thanks,
Howard
 
Hi Howard,

Am Fri, 22 Feb 2013 08:38:59 -0800 (PST) schrieb Howard:
However, it does error out with "Invalid Character" as soon as I pasted it in my code, entire line to red and refers to first $ sign. ??

the quotes are missing
try:
If Not Intersect(Target, Range("$B$8:$G$25")) Then Exit Sub
You can also try:
If Not Intersect(Target, Range("B8:G25")) Then Exit Sub


Regards
Claus Busch
 
Yes, I see that I also forgot to wrap the range address in double
quotes. My bad...

If Not Intersect(Target, Range("$B$8:$G$25") Then Exit Sub

My apologies...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Howard formulated the question :
Hmmmmm! The revised does the same thing.

Howard

Yes, but with 1/4 the required processing! Your If construct requires
VBA to do 4 processes (1 for each condition) whereas the Intersect
function does the same thing in a single process. That makes using
Intersect() 300% more efficient in this context.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Howard formulated the question :




Yes, but with 1/4 the required processing! Your If construct requires

VBA to do 4 processes (1 for each condition) whereas the Intersect

function does the same thing in a single process. That makes using

Intersect() 300% more efficient in this context.



--

Garry



Free usenet access at http://www.eternal-september.org

Classic VB Users Regroup!

comp.lang.basic.visual.misc

microsoft.public.vb.general.discussion

Hi Garry and Claus,

I'm feeling like a real troublemake here.

I have tried all corrected versions the two of you offered. If I make entries outside the B8:G25 range I get the familiar "Run-time error 91
Object... Block variable not set" Code line is yellow highlighted up to but excluding Exit Sub.

On Error Resume Next seems to cure it, is it proper to do that?

(Maybe my Excel has the Flu, or maybe I do.)

Howard
 
Hi Howard,

Am Fri, 22 Feb 2013 09:17:04 -0800 (PST) schrieb Howard:
I have tried all corrected versions the two of you offered. If I make entries outside the B8:G25 range I get the familiar "Run-time error 91
Object... Block variable not set" Code line is yellow highlighted up to but excluding Exit Sub.

On Error Resume Next seems to cure it, is it proper to do that?

try:
If Intersect(Target, Range("B8:G25")) Is Nothing _
Then Exit Sub


Regards
Claus Busch
 
Hi Howard,



Am Fri, 22 Feb 2013 09:17:04 -0800 (PST) schrieb Howard:







try:

If Intersect(Target, Range("B8:G25")) Is Nothing _

Then Exit Sub





Regards

Claus Busch

--

Win XP PRof SP2 / Vista Ultimate SP2

Office 2003 SP2 /2007 Ultimate SP2

Great, works just fine now, without the on error statement.

You guy's and gal's are the best.

Thanks much.

Regards,
Howard
 
Howard expressed precisely :
Hi Garry and Claus,

I'm feeling like a real troublemake here.

I have tried all corrected versions the two of you offered. If I make
entries outside the B8:G25 range I get the familiar "Run-time error 91
Object... Block variable not set" Code line is yellow highlighted up to but
excluding Exit Sub.

On Error Resume Next seems to cure it, is it proper to do that?

(Maybe my Excel has the Flu, or maybe I do.)

I don't advise using an error escape. Claus' revision is the way to go,
and is what I should have posted if things around me at the time
weren't so hectic. (I was at the hospital receiving treAtment via IV
and kept getting interupted. No excuse, though! I apologize for all the
extra trouble!)

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Howard expressed precisely :







I don't advise using an error escape. Claus' revision is the way to go,

and is what I should have posted if things around me at the time

weren't so hectic. (I was at the hospital receiving treAtment via IV

and kept getting interupted. No excuse, though! I apologize for all the

extra trouble!)



--

Garry



Free usenet access at http://www.eternal-september.org

Classic VB Users Regroup!

comp.lang.basic.visual.misc

microsoft.public.vb.general.discussion

GS,

No need for appoligies, for sure. You have helped me often and I appreciate your patience and your good advice.

Got it working just fine.

I had it in the back of my mind that error escape should be used very sparingly, if at all.

A question on that very thing. Often when using the change event sub for some activity if I click, drag and highlight portions of the worksheet not included in the change event code, say to delete stuff I don't need I get anerror about invalid entry or the such. On Error Resume Next seems to eliminat that, is that good practice??

Howard
 
Back
Top