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 6085277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:37:16+00:00 2026-05-23T11:37:16+00:00

i’m new to VBA and I’m having problems with arrays and ranges. I have

  • 0

i’m new to VBA and I’m having problems with arrays and ranges. I have to write a function to select portions of a range made of values with a time stamp and report them in a separate range.

The range is like this one:

            A           B
1   15-May-11 23:47:40  False
2   15-May-11 23:49:10  False
3   15-May-11 23:49:10  False
4   15-May-11 23:50:52  True
5   15-May-11 23:50:52  False
6   15-May-11 23:51:56  False
7   15-May-11 23:51:56  True
8   15-May-11 23:53:24  False
9   15-May-11 23:53:24  False

and I have prepared this function:

Function selectEvents(rangeTimeValue, Val As String)
    Dim outputRange() As Variant
    j = 1

    For i = 1 To rangeTimeValue.Height
    Val_recorded = rangeTimeValue(i, 2).Value
    Time_recorded = rangeTimeValue(i, 1).Value
        If Val_recorded = Val Then
            ReDim Preserve outputRange(j, 2) '(1)
            outputRange(j, 1) = Time_recorded
            outputRange(j, 2) = Val_recorded
            j = j + 1
        End If
    Next i

    selectEvents = Application.Transpose(outputRange) '(2)
End Function

now, there are two problems:

  1. the function breaks when the array
    is re-dimensioned the second time
    (1)
  2. I have an error also when
    pasting the results (2) because the
    range should be defined before I
    guess, but I don’t manage to find a
    way to pass the range’s dimension in
    a parametric way

I would be grateful of any help, Thanks!

  • 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-23T11:37:17+00:00Added an answer on May 23, 2026 at 11:37 am

    1. ReDim Preserve

    Quoting from VBA help

    If you use the Preserve keyword, you can resize only the last array dimension

    …while you are trying to ReDim Preserve the first dimension. Besides, ReDim Preserve is an expensive operation and you should avoid it unless it’s absolutely necessary, which it isn’t in your case. Since you can figure out the maximum number of elements your array will contain, just ReDim it once at the top of your code.

    Also, it is good practice to specify the lower bounds of your array explicitly, rather than just let them be whatever the default is and hope for the best.

    Example of how to do both these things:

    Dim nVal As Long
    nVal = WorksheetFunction.CountA(rangeTimeValue.Columns(1))
    ReDim outputRange(1 To nVal, 1 To 2)
    

    2. “Pasting” the results

    I’m not sure what you mean here, because you’re not pasting anything anywhere. Is this a typo where you meant pas*s*ing? Or do you mean transposing? I can’t figure out what you’re trying to do on that line with .Transpose. I don’t know. I’ll just take a stab in the dark:

    If you want to place the contents of your outputRange array somewhere on your sheet, then this is how you could do it:

    Dim rngOutput As Range
    Set rngOutput = Sheet1.Range("G12") ' or wherever
    rngOutput.Resize(UBound(outputRange, 1), UBound(outputRange, 2)) _
        = outputRange
    

    By the way, outputRange is a pretty terrible name for your array. It’s not a Range, so this name can lead to confusion, and I think it has indeed done so for you. Why not call it e.g. varOutput instead, since it’s a Variant array.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.