Solved

Excel VBA that resets a variable every 29 rows

Posted on 2013-11-13
6
189 Views
Last Modified: 2013-11-14
I have a code that loops through Range([F4], [D10000].End(xlUp).Offset(0, 2)), and looks for a the value of the variable iRow. Right now, iRow is determined by a helper column (Column G) which cycles through the numbers 4 to 32. The code finds the value for iRow in Column G:
iRow = cel.Offset(0, 1)

Open in new window

But I would like to determine iRow with code that resets itself to 4 every 29 rows without the need for the helper column. When the code is looking at F4, iRow = 4, for F5, iRow = 5, etc. And the series repeats itself starting with F33, F62, F91 etc.

Here's my current code:
For Each cel In Range([F4], [D10000].End(xlUp).Offset(0, 2)) 
    iRow = cel.Offset(0, 1)
    iCol = [AllWeeks].Find(what:=cel.Offset(0, -1), LookIn:=xlValues).Column - 1 'Cells(4, iCol).Select 'Cells(iRow, 100).End(xlToLeft).Offset(0, -1).Select
    Set lft = Cells(iRow, iCol)
    Set rgt = lft.Offset(0, 1)
    If rgt = "" Then
        Set rgt = rgt.End(xlToLeft)
        Set lft = rgt.Offset(0, -1)
    End If 'Range(lft, rgt).Select
    lCel = lft.Address(0, 0)
    rCel = rgt.Address(0, 0)    
    cel.Formula = "=IFERROR(AVERAGE(" & lCel & ":" & rCel & ")/30.5*7,"""")"
    If cel.Offset(0, 2) = "RFS" Then cel = ""  
Next cel

Open in new window

All I need is something that goes back to iRow = 4 when the macro gets to F33, and again at F62, and F91 etc.

I hope i haven't made this too confusing.

Thanks,
John
0
Comment
Question by:gabrielPennyback
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Dontmilkthis
ID: 39646704
so can you simply use the 'mod' operator to get the desired irow?

irow = cel.Row mod 29


at F4:
cel.Row = 4
4 mod 29 = 4
irow = 4

at F33
cel.Row = 33
33 mod 29 = 4
irow = 4

at F34
cel.Row = 34
34 mod 29 = 5
irow = 5


and so on...
0
 
LVL 5

Accepted Solution

by:
Dontmilkthis earned 280 total points
ID: 39646805
I had a further thought on this issue and realised a slight problem with the code, in that you'll get 0,1,2,3 sequences, which I imagine aren't wanted based on your explanation.

I believe this would fix it

irow = ((cel.Row - 4) mod 29) + 4
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 220 total points
ID: 39646940
I put your code in a sub so I could check Compilation.

To avoid needing the auxiliary column, I used iRow as a counter. It is initialized to 3, then an IIf statement either adds 1 or resets it to 4. Though this is one more statement than the Mod approach suggested by Dontmilkthis, you may find the logic easier to  understand.
Sub Test()
Dim cel As Range, lft As Range, rgt As Range
Dim iRow As Long, iCol As Long
Dim lCel As String, rCel As String
iRow = 3
For Each cel In Range([F4], [D10000].End(xlUp).Offset(0, 2))
    iRow = IIf(iRow = 32, 4, iRow + 1)
    iCol = [AllWeeks].Find(what:=cel.Offset(0, -1), LookIn:=xlValues).Column - 1 'Cells(4, iCol).Select 'Cells(iRow, 100).End(xlToLeft).Offset(0, -1).Select
    Set lft = Cells(iRow, iCol)
    Set rgt = lft.Offset(0, 1)
    If rgt = "" Then
        Set rgt = rgt.End(xlToLeft)
        Set lft = rgt.Offset(0, -1)
    End If 'Range(lft, rgt).Select
    lCel = lft.Address(0, 0)
    rCel = rgt.Address(0, 0)
    cel.Formula = "=IFERROR(AVERAGE(" & lCel & ":" & rCel & ")/30.5*7,"""")"
    If cel.Offset(0, 2) = "RFS" Then cel = ""
Next cel
End Sub

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 5

Expert Comment

by:Dontmilkthis
ID: 39646952
I'd be interested in seeing which is more efficient from a CPU cycle point of view...

might be worth actually testing that aspect if they both work as expected.

performing two additions and a mod operation might be a bit more expensive than the if else statement and an addition.
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 39648736
Thank you both! Brad, I actually came up with something last night based on iRow +1, but  the Double "I" If statement is new to me and more elegant than my If-Then statement.

Dontmilkthis, I didn't know how to use the mod statement and I love the fact that it's a one-liner.

Again, thank you both.

~ John
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 39648738
They should have an"A+ Sublime" category.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question