basic loop question

hfler

Board Regular
Joined
Jun 10, 2008
Messages
95
Someone told me I can't have a loop within a VBA function that offsets to different cells from current cells, such as

Code:
Function test(startdate As Variant, enddate As Variant) As Variant

..................... 'removed  code
 
    If currmo > ActiveCell And Not IsDate(ActiveCell) Then
        pricejump = Value.Range("frontmth_last").End(xlDown) + price
        price = pricejump
    ElseIf currmo < ActiveCell Then
        pricejump = ActiveCell.Offset(0, 1) + price
        price = pricejump
            If currmo < WorksheetFunction.EDate(currmo, 1) Then
            currmojump = currmo
            Else:
            currmojump = WorksheetFunction.EDate(currmo, 1)
            End If
        currmo = currmojump
    Else:
        Range("front_fstnot").Offset(i, 0).Active
        currmo = ActiveCell

If anyone has any insight, would appreciate some help! Thanks!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You can, but Excel will see no dependence on the active cell, so it won't update automatically when the selection changes.

Why would you want to?
 
Upvote 0
It's okay if it doesn't update automatically, if I can recalculate the cell and have it update. If you're so inclined, I've pasted my entire code below. I should add I'm a bit of a beginner... Understand if you don't want to parse through this, but would appreciate any suggestions. Thank you!

Code:
Function test(startdate as variable, enddate as variable) as variable

Dim i As Double
Dim currmo As Double
Dim currmojump As Double
Dim price As Double
Dim pricejump As Double
Dim space As Double
space = -DateDiff("m", enddate, startdate) '+ 1 <<not necessary b/c i starts at 0

Range("front_fstnot").Select
currmo = startdate
For i = 0 To space
    If currmo > ActiveCell And Not IsDate(ActiveCell) Then
        pricejump = Value.Range("frontmth_last").End(xlDown) + price
        price = pricejump
    ElseIf currmo < ActiveCell Then
        pricejump = ActiveCell.Offset(0, 1) + price
        price = pricejump
            If currmo < WorksheetFunction.EDate(currmo, 1) Then
            currmojump = currmo
            Else:
            currmojump = WorksheetFunction.EDate(currmo, 1)
            End If
        currmo = currmojump
    Else:
        Range("front_fstnot").Offset(i, 0).Active
        currmo = ActiveCell
    End If
Next i
price = pricejump / (space + 1)
test = price
 
End function
 
Upvote 0
I can't tell what your code is supposed to do at all.
 
Upvote 0
As far as I'm aware you can't do things like activating/selecting with a UDF.
Are you trying to do that?
 
Last edited:
Upvote 0
Sorry, not familiar with "UDF" - but if that's a custom function in VBA, then yes, I'm trying to activate cells using that. Does that not work?

If not, is there a way to create a function that will run a macro if a cell / sheet is refreshed using F9?
 
Upvote 0
UDF - user defined function.
If you are using what you posted on a worksheet that's what it is.
If you want code to run on calculation then you can use the worksheet Calculate event.
Can you explain what you are trying to do?
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,881
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top