loop variable range

C

CR

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
 
P

Patrick Molloy

for cl = 5 to 20 'whatever S is

if ( cells(3,cl) + cells(5,cl) ) > cells(1,cl) then
'do something
next
 
D

Dave Peterson

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.
 
J

Jim Thomlinson

One way...

Dim rng As Range

For Each rng In Range("D1:S1")
If Application.Sum(rng.Offset(2, 0).Resize(3, 1)) > rng.Value Then
MsgBox rng.Address
End If
Next rng
 
J

Jacob Skaria

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

End Sub


If this post helps click Yes
 
J

Jim Thomlinson

I misread the question...

Dim rng As Range

For Each rng In Range("D1:S1")
If rng.Offset(2, 0).value + rng.Offset(4, 0).value > rng.Value Then
MsgBox rng.Address
End If
Next rng
 
C

CR

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
 
J

Jacob Skaria

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 said:
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
 
D

Dave Peterson

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 said:
.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)
 
C

CR

Perfect!
Thank you.

CR


Dave Peterson said:
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) _
'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)
 

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