Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default loop variable range

Dave Thank you
It does exactly what I wanted.

What is the best way to check for illegal values? ie if either or both of
the two summed cells have a value of 0, I would want to skip to the next
icol

CR



"Dave Peterson" wrote in message
...
Dim iCol as long
dim FirstCol as long
dim LastCol as long

with worksheets("Somesheetnamehere")
firstcol = .range("d1").column
lastcol = .range("s1").column

for icol = firstcol to lastcol
'no check for illegal values!
if (.cells(3,icol).value + .cells(5,icol).value) _
.cells(1,icol).value then

'do something
else
'do nothing
end if
next icol
end with

Untested, uncompiled. Watch for typos.

CR wrote:

I have a Range with values D1:S5

I need to sum D3 and D5 and if it is D1 value then
Do something

Then loop through E to S, doing the same thing

Any help would be appreciated

Thanks
CR


--

Dave Peterson



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default loop variable range

Try

Sub Macro()
Dim rngCol As Range
For Each rngCol In Range("d1:S5").Columns
If rngCol.Cells(5, 1) + rngCol.Cells(3, 1) < 0 Then
If rngCol.Cells(5, 1) + rngCol.Cells(3, 1) rngCol.Cells(1, 1) Then
'dO SOMETHING
End If
End If
Next

End Sub

If this post helps click Yes
---------------
Jacob Skaria


"CR" wrote:

Dave Thank you
It does exactly what I wanted.

What is the best way to check for illegal values? ie if either or both of
the two summed cells have a value of 0, I would want to skip to the next
icol

CR



"Dave Peterson" wrote in message
...
Dim iCol as long
dim FirstCol as long
dim LastCol as long

with worksheets("Somesheetnamehere")
firstcol = .range("d1").column
lastcol = .range("s1").column

for icol = firstcol to lastcol
'no check for illegal values!
if (.cells(3,icol).value + .cells(5,icol).value) _
.cells(1,icol).value then

'do something
else
'do nothing
end if
next icol
end with

Untested, uncompiled. Watch for typos.

CR wrote:

I have a Range with values D1:S5

I need to sum D3 and D5 and if it is D1 value then
Do something

Then loop through E to S, doing the same thing

Any help would be appreciated

Thanks
CR


--

Dave Peterson




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default loop variable range

I'd use:


Option Explicit
Sub testme()
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long

With Worksheets("Somesheetnamehere")
FirstCol = .Range("d1").Column
LastCol = .Range("s1").Column

For iCol = FirstCol To LastCol
If Application.IsNumber(.Cells(3, iCol).Value) Then
If Application.IsNumber(.Cells(5, iCol).Value) Then
If .Cells(3, iCol).Value < 0 Then
If .Cells(5, iCol).Value < 0 Then
If (.Cells(3, iCol).Value + .Cells(5, iCol).Value) _
.Cells(1, iCol).Value Then

'do something
Else
'do nothing
End If
End If
End If
End If
End If
Next iCol
End With

End Sub

(compiled, but not tested)



CR wrote:

Dave Thank you
It does exactly what I wanted.

What is the best way to check for illegal values? ie if either or both of
the two summed cells have a value of 0, I would want to skip to the next
icol

CR

"Dave Peterson" wrote in message
...
Dim iCol as long
dim FirstCol as long
dim LastCol as long

with worksheets("Somesheetnamehere")
firstcol = .range("d1").column
lastcol = .range("s1").column

for icol = firstcol to lastcol
'no check for illegal values!
if (.cells(3,icol).value + .cells(5,icol).value) _
.cells(1,icol).value then

'do something
else
'do nothing
end if
next icol
end with

Untested, uncompiled. Watch for typos.

CR wrote:

I have a Range with values D1:S5

I need to sum D3 and D5 and if it is D1 value then
Do something

Then loop through E to S, doing the same thing

Any help would be appreciated

Thanks
CR


--

Dave Peterson


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default loop variable range

Perfect!
Thank you.

CR


"Dave Peterson" wrote in message
...
I'd use:


Option Explicit
Sub testme()
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long

With Worksheets("Somesheetnamehere")
FirstCol = .Range("d1").Column
LastCol = .Range("s1").Column

For iCol = FirstCol To LastCol
If Application.IsNumber(.Cells(3, iCol).Value) Then
If Application.IsNumber(.Cells(5, iCol).Value) Then
If .Cells(3, iCol).Value < 0 Then
If .Cells(5, iCol).Value < 0 Then
If (.Cells(3, iCol).Value + .Cells(5,
iCol).Value) _
.Cells(1, iCol).Value Then

'do something
Else
'do nothing
End If
End If
End If
End If
End If
Next iCol
End With

End Sub

(compiled, but not tested)



CR wrote:

Dave Thank you
It does exactly what I wanted.

What is the best way to check for illegal values? ie if either or both of
the two summed cells have a value of 0, I would want to skip to the next
icol

CR

"Dave Peterson" wrote in message
...
Dim iCol as long
dim FirstCol as long
dim LastCol as long

with worksheets("Somesheetnamehere")
firstcol = .range("d1").column
lastcol = .range("s1").column

for icol = firstcol to lastcol
'no check for illegal values!
if (.cells(3,icol).value + .cells(5,icol).value) _
.cells(1,icol).value then
'do something
else
'do nothing
end if
next icol
end with

Untested, uncompiled. Watch for typos.

CR wrote:

I have a Range with values D1:S5

I need to sum D3 and D5 and if it is D1 value then
Do something

Then loop through E to S, doing the same thing

Any help would be appreciated

Thanks
CR

--

Dave Peterson


--

Dave Peterson



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
loop variable range Jim Thomlinson Excel Programming 0 September 21st 09 04:28 PM
loop variable range Jim Thomlinson Excel Programming 0 September 21st 09 04:27 PM
loop variable range Dave Peterson Excel Programming 0 September 21st 09 04:24 PM
Referencing variable Range in a loop Samirkc Excel Programming 2 July 14th 06 12:49 PM
How to reference variable range in a loop Samirkc Excel Programming 0 July 13th 06 04:11 PM


All times are GMT +1. The time now is 04:20 AM.

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"