Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?

Sub getDataInfo2(sRange As Range)
Const maxr = 15
Const defSize = 100
Dim row As Integer
Dim col As Integer
Dim i As Integer
Dim size As Integer
Dim buffer(defSize)

' Get data from source range
row = sRange.row
col = sRange.Column
size = 0
For i = 0 To maxr
If sRange.Cells(row, col).Value < "" Then
' This if() NEVER passes - ???????????????????????
buffer(size) = sRange.Cells(row, col).Value
size = size + 1
End If
col = col + 1
Next i
End Sub


Sub test()
Dim baseBook As Workbook
Dim currSheet As Worksheet
Dim sRange as Range

set baseBook = ThisWorkbook
set currSheet = baseBook.Sheets(2)
currSheet.Activate
set sRange = currSheet.Range("A1:J1")
' I CAN SEE CELL VALUES IN THE RANGE HERE
Call getDataInfo2(sRange)
End Sub

Any clues?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default How come VBA if clause fails?

On 17 Jan., 23:10, Zilla wrote:
I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?

Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)

* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub

Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range

* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub

Any clues?


Hi

Try this

Sub getDataInfo2(sRange As Range)
Const defSize = 100
Dim size As Integer
Dim buffer(defSize)


' Get data from source range

size = 0
For Each c In sRange
If c.Value < "" Then
buffer(size) = c.Value
size = size + 1
End If
Next
End Sub

//Per
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 5:26*pm, Per Jessen wrote:
On 17 Jan., 23:10, Zilla wrote:





I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi

Try this

Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)

* * ' Get data from source range

* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub

//Per- Hide quoted text -

- Show quoted text -


Thanks again Jason. I'll try your code tomorrow. But why didn't my
version work? Just curious...
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 6:14*pm, Zilla wrote:
On Jan 17, 5:26*pm, Per Jessen wrote:





On 17 Jan., 23:10, Zilla wrote:


I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi


Try this


Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range


* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub


//Per- Hide quoted text -


- Show quoted text -


Thanks again Jason. I'll try your code tomorrow. But why didn't my
version work? Just curious...- Hide quoted text -

- Show quoted text -


Sorry, I meant Pe "Jessen" :)
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default How come VBA if clause fails?

But why didn't my
version work? Just curious


For i = 0 To maxr
If sRange.Cells(row, col).Value < "" Then
' This if() NEVER passes - ???????????????????????
buffer(size) = sRange.Cells(row, col).Value
size = size + 1
End If
col = col + 1
Next i



"Zilla" wrote:

The For ... Next loop is useless because you do not use the variable i in
the intervening code to do anything. You would need to use it like
Cells(row, i) or
Cells(i, col) depending on which way you want to move.

When you used sRange.Cells(row, col), you essentially create a circular
reference. Cells(row, col) by definition are part of sRange. Remember row =
sRange.Row?
You should not have used sRange as part of the cell designation but just use
the
If Cells(i, col) = etc. Top down Parent/Child would be
Workbook.Sheet.Range or Cell. The cell is a range, so if you use Cells(row,
col) don't use a Range variable and vice versa.

It takes a while to pull all this stuff together, but you're getting there.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 5:26*pm, Per Jessen wrote:
On 17 Jan., 23:10, Zilla wrote:





I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi

Try this

Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)

* * ' Get data from source range

* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub

//Per- Hide quoted text -

- Show quoted text -


Ok I just tried it, and I get the opposite effect that is, the if()
clause ALWAYS passes; I know c.Value is sometimes "" (I do a
MsgBox(c.Value) to prove it). Is this the only way to check for an
empty cell?
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 7:08*pm, JLGWhiz wrote:
But why didn't my

version work? Just curious


For i = 0 To maxr

* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i

"Zilla" wrote:

The For ... Next loop is useless because you do not use the variable i in
the intervening code to do anything. *You would need to use it like
Cells(row, i) or
Cells(i, col) depending on which way you want to move.

When you used sRange.Cells(row, col), you essentially create a circular
reference. *Cells(row, col) by definition are part of sRange. *Remember row =
sRange.Row?
You should not have used sRange as part of the cell designation but just use
the
If Cells(i, col) = *etc. * Top down Parent/Child would be
Workbook.Sheet.Range or Cell. *The cell is a range, so if you use Cells(row,
col) don't use a Range variable and vice versa.

It takes a while to pull all this stuff together, but you're getting there..


Oh, I see the coding error now in my orig code - a typical cut and
paste error! The line should be

NOT
col = col + 1

BUT THIS
col = col + i
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 7:10*pm, Zilla wrote:
On Jan 17, 5:26*pm, Per Jessen wrote:





On 17 Jan., 23:10, Zilla wrote:


I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi


Try this


Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range


* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub


//Per- Hide quoted text -


- Show quoted text -


Ok I just tried it, and I get the opposite effect that is, the if()
clause ALWAYS passes; I know c.Value is sometimes "" (I do a
MsgBox(c.Value) to prove it). Is this the only way to check for an
empty cell?- Hide quoted text -

- Show quoted text -


Ok, I know why - the passing cells have white space, so c.Value < ""
indeed since c.Value = " " (3 spaces) for example.
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default How come VBA if clause fails?

On 18 Jan., 01:39, Zilla wrote:
On Jan 17, 7:10*pm, Zilla wrote:





On Jan 17, 5:26*pm, Per Jessen wrote:


On 17 Jan., 23:10, Zilla wrote:


I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi


Try this


Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range


* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub


//Per- Hide quoted text -


- Show quoted text -


Ok I just tried it, and I get the opposite effect that is, the if()
clause ALWAYS passes; I know c.Value is sometimes "" (I do a
MsgBox(c.Value) to prove it). Is this the only way to check for an
empty cell?- Hide quoted text -


- Show quoted text -


Ok, I know why - the passing cells have white space, so c.Value < ""
indeed since c.Value = " * " (3 spaces) for example.- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -


Hi Zilla

Try

If Trim(c.Value) <...

Regards,

Per
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 7:08*pm, JLGWhiz wrote:
But why didn't my

version work? Just curious


For i = 0 To maxr

* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i

"Zilla" wrote:

The For ... Next loop is useless because you do not use the variable i in
the intervening code to do anything. *You would need to use it like
Cells(row, i) or
Cells(i, col) depending on which way you want to move.

When you used sRange.Cells(row, col), you essentially create a circular
reference. *Cells(row, col) by definition are part of sRange. *Remember row =
sRange.Row?
You should not have used sRange as part of the cell designation but just use
the
If Cells(i, col) = *etc. * Top down Parent/Child would be
Workbook.Sheet.Range or Cell. *The cell is a range, so if you use Cells(row,
col) don't use a Range variable and vice versa.

It takes a while to pull all this stuff together, but you're getting there..


I read your reply again, this time carefully :) I'm studying more
about Cells and Range objects. Thanks!


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How come VBA if clause fails?

On Jan 17, 7:59*pm, Per Jessen wrote:
On 18 Jan., 01:39, Zilla wrote:





On Jan 17, 7:10*pm, Zilla wrote:


On Jan 17, 5:26*pm, Per Jessen wrote:


On 17 Jan., 23:10, Zilla wrote:


I have the following subroutine. Why can't I see the passed-in Range's
Cells(x,y).Value?


Sub getDataInfo2(sRange As Range)
* * Const maxr = 15
* * Const defSize = 100
* * Dim row As Integer
* * Dim col As Integer
* * Dim i As Integer
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range
* * row = sRange.row
* * col = sRange.Column
* * size = 0
* * For i = 0 To maxr
* * * * If sRange.Cells(row, col).Value < "" Then
* * * * ' This if() NEVER passes - ???????????????????????
* * * * * * buffer(size) = sRange.Cells(row, col).Value
* * * * * * size = size + 1
* * * * End If
* * * * col = col + 1
* * Next i
End Sub


Sub test()
* * Dim baseBook As Workbook
* * Dim currSheet As Worksheet
* * Dim sRange as Range


* *set baseBook = ThisWorkbook
* *set currSheet = baseBook.Sheets(2)
* *currSheet.Activate
* *set sRange = currSheet.Range("A1:J1")
* *' I CAN SEE CELL VALUES IN THE RANGE HERE
* *Call getDataInfo2(sRange)
End Sub


Any clues?


Hi


Try this


Sub getDataInfo2(sRange As Range)
* * Const defSize = 100
* * Dim size As Integer
* * Dim buffer(defSize)


* * ' Get data from source range


* * size = 0
* * For Each c In sRange
* * * * If c.Value < "" Then
* * * * * * buffer(size) = c.Value
* * * * * * size = size + 1
* * * * End If
* * Next
End Sub


//Per- Hide quoted text -


- Show quoted text -


Ok I just tried it, and I get the opposite effect that is, the if()
clause ALWAYS passes; I know c.Value is sometimes "" (I do a
MsgBox(c.Value) to prove it). Is this the only way to check for an
empty cell?- Hide quoted text -


- Show quoted text -


Ok, I know why - the passing cells have white space, so c.Value < ""
indeed since c.Value = " * " (3 spaces) for example.- Skjul tekst i anførselstegn -


- Vis tekst i anførselstegn -


Hi Zilla

Try

If Trim(c.Value) <...

Regards,

Per- Hide quoted text -

- Show quoted text -


Thanks, that was perfect!
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
PERCENTILE with an IF Clause Zeelotes Excel Worksheet Functions 3 April 19th 23 02:11 PM
IF Clause dpal Excel Worksheet Functions 8 July 19th 07 07:32 PM
Can I use a between clause or in clause on an IF statement ssciarrino Excel Programming 2 May 4th 07 04:48 PM
IF Clause juergenkemeter[_2_] Excel Programming 6 January 10th 06 11:17 PM
"Between" in an IF clause gavin Excel Discussion (Misc queries) 5 May 2nd 05 09:27 PM


All times are GMT +1. The time now is 12:38 PM.

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

About Us

"It's about Microsoft Excel"