Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Perplexing IF statement

Hi Experts,

Why do I know I'm going to be embarrassed by the answer to my query...?

Code takes me to the first empty cell to the right of the E column data.
I don't want to go past column 16 (col. P)
In a test row, i = 18 so there is an if statement to only go to 16 IF i 15.
However it takes me to column U which is 21.
Works just fine on other rows less than 16 but if i = 16 it goes to 21 also.

Option Explicit

Sub GoRT()
Dim i As Integer

If Not Intersect(ActiveCell, Range("E6:E20")) Is Nothing Then

i = ActiveCell.End(xlToRight).Column

' Msgbox to test i returns 18 on a test row
MsgBox i
' i is 16 so why does this if statement
' take me to column U which is 21

If i 16 Then
ActiveCell.Offset(0, 16).Select
Exit Sub
End If

ActiveCell.Offset(0, i - 4).Select
End If
End Sub

Thanks,
Howard
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Perplexing IF statement

On Saturday, February 2, 2013 12:08:19 PM UTC-8, Howard wrote:
Hi Experts,



Why do I know I'm going to be embarrassed by the answer to my query...?



Code takes me to the first empty cell to the right of the E column data.

I don't want to go past column 16 (col. P)

In a test row, i = 18 so there is an if statement to only go to 16 IF i 15.

However it takes me to column U which is 21.

Works just fine on other rows less than 16 but if i = 16 it goes to 21 also.



Option Explicit



Sub GoRT()

Dim i As Integer



If Not Intersect(ActiveCell, Range("E6:E20")) Is Nothing Then



i = ActiveCell.End(xlToRight).Column



' Msgbox to test i returns 18 on a test row

MsgBox i

' i is 16 so why does this if statement

' take me to column U which is 21



If i 16 Then

ActiveCell.Offset(0, 16).Select

Exit Sub

End If



ActiveCell.Offset(0, i - 4).Select

End If

End Sub



Thanks,

Howard



This is a typo, 16 should be 15
' i is 16 so why does this if statement
Howard
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Perplexing IF statement

On Saturday, February 2, 2013 12:08:19 PM UTC-8, Howard wrote:
Hi Experts,



Why do I know I'm going to be embarrassed by the answer to my query...?



Code takes me to the first empty cell to the right of the E column data.

I don't want to go past column 16 (col. P)

In a test row, i = 18 so there is an if statement to only go to 16 IF i 15.

However it takes me to column U which is 21.

Works just fine on other rows less than 16 but if i = 16 it goes to 21 also.



Option Explicit



Sub GoRT()

Dim i As Integer



If Not Intersect(ActiveCell, Range("E6:E20")) Is Nothing Then



i = ActiveCell.End(xlToRight).Column



' Msgbox to test i returns 18 on a test row

MsgBox i

' i is 16 so why does this if statement

' take me to column U which is 21



If i 16 Then

ActiveCell.Offset(0, 16).Select

Exit Sub

End If



ActiveCell.Offset(0, i - 4).Select

End If

End Sub



Thanks,

Howard


Drat!!! Two typos...

"If i 16 Then" should be 15

Howard
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Perplexing IF statement

Howard,

This code takes you to column 21 because of the line:

ActiveCell.Offset(0, 16).Select

That line will start from the active cell (which is in column E) and go over 16 columns. Since column E is the 5th column, 16 columns over from the active cell is column 21.

To fix, simply change the column offset to:

ActiveCell.Offset(0, 12).Select

Or you could remove the IF statement altogether and rewrite as:

Sub GoRT()
Dim i As Integer

If Not Intersect(ActiveCell, Range("E6:E20")) Is Nothing Then

i = ActiveCell.End(xlToRight).Column
i = WorksheetFunction.Min(i, 16) - 4
MsgBox i
ActiveCell.Offset(0, i).Select
End If
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Perplexing IF statement

On Saturday, February 2, 2013 12:08:19 PM UTC-8, Howard wrote:
Hi Experts,



Why do I know I'm going to be embarrassed by the answer to my query...?



Code takes me to the first empty cell to the right of the E column data.

I don't want to go past column 16 (col. P)

In a test row, i = 18 so there is an if statement to only go to 16 IF i 15.

However it takes me to column U which is 21.

Works just fine on other rows less than 16 but if i = 16 it goes to 21 also.



Option Explicit



Sub GoRT()

Dim i As Integer



If Not Intersect(ActiveCell, Range("E6:E20")) Is Nothing Then



i = ActiveCell.End(xlToRight).Column



' Msgbox to test i returns 18 on a test row

MsgBox i

' i is 16 so why does this if statement

' take me to column U which is 21



If i 16 Then

ActiveCell.Offset(0, 16).Select

Exit Sub

End If



ActiveCell.Offset(0, i - 4).Select

End If

End Sub



Thanks,

Howard


Thanks Ben,

The 16 + 5 = 21 eluded me.

Thanks again.

Howard


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perplexing string manipulation problem Russ Jones Excel Programming 4 July 10th 08 12:37 PM
New to Excel 2007 with a perplexing MACRO question Tangohammerli Excel Discussion (Misc queries) 2 February 22nd 08 02:36 AM
Perplexing sheets. ben Excel Programming 3 July 13th 05 07:08 PM
Help with perplexing form problem Frustrated Excel Programming 6 April 26th 04 09:21 PM


All times are GMT +1. The time now is 10:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"