Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a Do Loop 2

Status
Not open for further replies.

RSJohnson

Programmer
Jan 30, 2004
45
US
I am trying to loop through a worksheet and calculate four (4) percentages. At the end of each loop I need to move to the next row and do the same calculations for the new row and so on. I assume that if the myRange is assigned a new value (for the next range) no changes to the functions that do the calculation need to be made because their references to other cells are relative to the new value of myRange or am I wrong, again?

Though the right cell is selected an Error 424 (Object required) is raised at the statement; “Set myRange = myRange.Offset(1, 0).Select”

The variable myRange is dimensioned in the General Declearations.

Can someone suggest what I am doing wrong?
Here’s my code:

Sub CalculatePercents()
Dim myStop As Integer, myCount As Integer

'Determines the number of records (rows) and assigns _
value to variable.
myStop = Range("A1").CurrentRegion.Rows.Count

'Range("CB4").Select
Set myRange = Range("CB4")

'Counter needed to end looping.
myCount = 1

'Do Until myStop = myCount
'Calculates funding percentages for a row.
Call GeneralFundPercent
Call SpecialFundPercent
Call TotalFedFunds
Call FederalFundPercent
Call TotalPercent

'Moves next row.
Set myRange = myRange.Offset(1, 0).Select

'Counter
myCount = myCount + 1
'Loop

End Sub
 
Just a quick glance, but shouldn't

Code:
   'Do Until myStop = myCount
be
Code:
   'Do Until myCount = myStop
so that the loop will process more than 1 (the value of myCount) records?

< M!ke >
 
Hi,

Try to avoid selecting faq707-4105 How Can I Make My Code Run Faster?
Code:
Sub CalculatePercents()
    Dim myStop As Integer, myCount As Integer
    
    'Determines the number of records (rows) and assigns _
        value to variable.
    With Range(&quot;A1&quot;).CurrentRegion
        myStop = .Rows.Count
        Set rngLoop = Range(Cells(.Row, &quot;CB&quot;), Cells(myStop, &quot;CB&quot;))
    End With
    
    
    For Each myRange In rngLoop
        'Calculates funding percentages for a row.
        Call GeneralFundPercent
        Call SpecialFundPercent
        Call TotalFedFunds
        Call FederalFundPercent
        Call TotalPercent
        
    Next
   
End Sub


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Just drop the select
try this:
Set myRange = myRange.Offset(1, 0)

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Skip,
I dimmed rngLoop as a range and the code worked. How would I get your code to start at CB4 and not CB1?

Mike and Geoff I modified my code with your suggestions and and the code works. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top