Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:56:33+00:00 2026-05-31T05:56:33+00:00

EDIT: So it was really obvious, I needed the Preserve modifer for the Redim

  • 0

EDIT: So it was really obvious, I needed the “Preserve” modifer for the Redim statement.

I’m trying to write a UDF in Excel VBA which takes a column of prices and selects the n highest/lowest buys/sells (marked in the adjacent column), and outputs ‘yes’ or nothing depending on whether a given cell meets those criteria.

As an example, for n = 2 the input could look something like this:

Price Type Result
150 buy yes
170 sell
146 sell yes
167 buy
125 buy
164 sell yes

So I’m counting through the price column, putting each price in either a ‘buy’ or ‘sell’ array, but for some reason when I look at the results only the last entries are showing up correctly, with the rest of the entries = 0.

Here’s the code I’ve got:

Function included(prices As Range, entry As Range)
n = 2
bc = 1                'counter for the buys
sc = 1                'counter for the sells
Ub = 1                'upper bound for buys
Us = 1                'upper bound for sells
Dim b() As Double     'dynamic array for buys
Dim s() As Double     'dynamic array for sells

'collect the buys and the sells into two arrays
For i = 1 To prices.Rows.Count
    amt = prices(i).Value
    If prices(i).Offset(0, 1) = "buy" Then   'add to buy list
        ReDim b(1 To Ub)                     'reapply length
        b(bc) = amt                          'add the entry
        Ub = UBound(b) + 1                   'add one to the length
        bc = bc + 1                          'increase the counter by 1
    ElseIf prices(i).Offset(0, 1) = "sell" Then
        'add to s
        ReDim s(1 To Us)
        s(sc) = amt
        Us = UBound(s) + 1
        sc = sc + 1
    Else
        MsgBox "nothing"
    End If
Next

'check the resulting arrays (only the last value in b() and s() print out)
For i = 1 To UBound(b)
    If b(i) <> 0 Then
        MsgBox b(i)
    End If
Next
For i = 1 To UBound(s)
    If s(i) <> 0 Then
        MsgBox s(i)
    End If
Next

'still to do: 
' sort the buy and sell arrays in ascending and descending order respectively
' truncate the arrays to length n   
' check if entry is in one of the resulting arrays

End Function

I’m really new to VBA (and a mere Python novice), so maybe it’s something obvious? Thank you in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:56:35+00:00Added an answer on May 31, 2026 at 5:56 am

    From the msdn library entry for ReDim, the Preserve modifier needs to be included, so the code now reads ReDim Preserve b(1 To Ub). Without the modifier the statement was emptying the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I might be missing something really obvious. I'm trying to write a custom Panel
trying to do something pretty obvious really but its kinda not making any sense
Is the documentation for Rich Edit Controls really as bad (wrong?) as it seems
I posted another one on this,, but i had to edit it really much...
EDIT: I would really like to see some general discussion about the formats, their
Edit: When I say SQL Server, I'm really talking about the Management Studio. Sorry
EDIT: This question was initially too general, I think. So What I really need
I installed gvim on windows vista and got really excited to edit EVERYTHING with
Edit: This question was written in 2008, which was like 3 internet ages ago.
I know this is probably really obvious, but I cannot figure out why I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.