Skip to content

VBA-tools2/modArraySupport2.vba

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

modArraySupport2.vba

Excel VBA module with functions for (VBA) arrays.

This is essentially a rewrite of Charles H. Pearson’s http://www.cpearson.com/excel/vbaarrays.htm. Thank you for the great work!

This repository exists to bundle and publish all improvements made to the original module.

Features

  • (hmm, do you have a good idea for this point?)

Prerequisites / Dependencies

Since this is an Excel VBA module you obviously need Microsoft Excel.

How to install / Getting started

Add the file(s) from the src folder to your project. Yes, it’s that simple.

If you need some more advice on how to do that, expand me.
  1. Open Microsoft Excel.

  2. Open the Visual Basic Editor (VBE) (Alt+F11).

  3. Add the file(s) in the src folder to your VBA project.

    • With Rubberduck:

      1. Right-click on the project to which you want to add the file(s) in the “Code Explorer” (to show it press Ctrl+R) and click on Add  Existing Files…​.

      2. Select all files in the src folder and click on Open.

    • Without Rubberduck:

      1. Select all files in the src folder in Windows File Explorer.

      2. Drag-and-drop them to the corresponding project in VBE’s “Project Explorer”.
        (To show it press Ctrl+R. Hit it twice if the Code Explorer shows up first.)

  4. Check, if there are obvious errors by compiling the project (Debug  Compile ‹project name›).

  5. Save the file/project.

    1. Be sure that the file/project you want to save is “active” in the VBE by checking, if its name is shown in VBE’s title bar.
      (If it’s not, open a (class) module of the corresponding project (and close it again).)

    2. Press the “Save” button (the disc symbol similar to 💾) in VBE’s toolbar.

    3. Check that the file (really) was saved by having a look at the “last modified date” of the (project) file in the Windows File Explorer.

Usage / Show it in action

A typical use case to use the functions is when you write functions that need a vector and should work with data from VBA and/or from a worksheet. In VBA you of course directly create the (1D) vector, but when reading vector data from a worksheet, you get a (2D) array/a matrix. Here is the code I usually use to get the vector, where 5 functions of this project are used. (This is used e.g. in modPolynomReg module (see Used By section for the source of that module).)

Private Function ExtractVector( _
    ByVal Source As Variant, _
    ByRef DestVector As Variant _
        ) As Boolean

    Select Case NumberOfArrayDimensions(Source)
        Case 2
            If UBound(Source, 1) > 1 And UBound(Source, 2) = 1 Then
                If Not GetColumn(Source, DestVector, 1) Then Exit Function
            ElseIf UBound(Source, 1) = 1 And UBound(Source, 2) > 1 Then
                If Not GetRow(Source, DestVector, 1) Then Exit Function
            Else
                Exit Function
            End If
        Case 1
            If Not CopyArray(Source, DestVector, False) Then Exit Function
            Dim N As Long
            N = UBound(DestVector) - LBound(DestVector) + 1
            If Not ChangeBoundsOfVector(DestVector, 1, N) Then Exit Function
        Case 0
            ReDim DestVector(0)
            DestVector(0) = Source
        Case Else
    End Select

    ExtractVector = True

End Function

Running Tests

Yes, Unit Tests in Excel are possible. For that you need to have the awesome Rubberduck AddIn installed (and enabled).

The procedure is very similar to the one given in the How to install / Getting started section, but this time add the files from the tests folder (instead of the files from the src folder). When you are done with that, additionally perform the following steps.

  1. Check that the files from the src folder are present in the project as well or that you have a reference to project with the source files.[1]
    (Otherwise tests will/should fail.)

  2. Add a reference to the “Rubberduck AddIn” to the project (with the test modules).

    If you don’t have a clue how to do that, expand me.
    • With Rubberduck:

      1. Right-click somewhere on the project in the “Code Explorer” and click on Add/Remove References…​.

      2. Add the reference.

        1. Type (parts of) the library name in the search box until you see it in below (left) list or it is empty.

          • If the AddIn file name is in the lower left list box:

            1. Select the library in the (left) list and

            2. click on the button to add it to the project references.

          • If the the AddIn file name is not in the list:

            1. Click on the Browse…​ button.

            2. Browse to the folder where the AddIn is located.

            3. Select the AddIn and

            4. press the Open button.

      3. Click on the OK button to close the window.

    • Without Rubberduck:

      1. Open the Reference manager in the VBE (Tools  References…​).

        • If the AddIn project name is in the list of available references:

          1. Add a checkmark to the corresponding library.

        • If it’s not in the list:

          1. Click on the Browse…​ button.

          2. Browse to the folder where the AddIn is located.

          3. Select the AddIn and

          4. press the Open button.

      2. Click on the OK button to close the window.

      3. Save the file/project.

  3. Open the “Test Explorer” (Rubberduck  Unit Tests  Test Explorer).

  4. Run the tests by clicking Run  All Tests.

Used By

This project is used by (at least) these projects:

If you know more, I’ll be happy to add them here.

Known issues and limitations

None that I am aware of.

Contributing

All contributions are highly welcome!!

If you are new to git/GitHub, please have a look at https://github.com/firstcontributions/first-contributions where you will find a lot of useful information for beginners.

I recently was pointed to https://www.conventionalcommits.org which sounds very promising. I’ll use them from now on too (and hopefully don’t forget it in a hurry).

FAQ

  1. What are the '@... comments good for in the code?

    You should really have a look at the awesome Rubberduck project!

License

MIT


1. See the collapsible in the next point on how to do that. If you use the Rubberduck way, before adding the reference you first need to switch to the Projects tab.

About

Excel VBA module with functions for (VBA) arrays.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages