• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • SALE 20% Off All Courses
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

VBA Function: Results to spilling array created in VBA.|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA Function: Results to spilling array created in VBA.|VBA & Macros|Excel Forum|My Online Training Hub
                    BLACK FRIDAY SALE 20% OFF ALL COURSES
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA Function: Results to spilling a…
sp_PrintTopic sp_TopicIcon
VBA Function: Results to spilling array created in VBA.
Avatar
KENNETH BATILO
Member
Members
Level 0
Forum Posts: 29
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
1
October 21, 2023 - 3:31 am
sp_Permalink sp_Print

Hello all!

I am looking to see if there is a way to have a custom function to spill an array built in VBA?

 

So the sample code below is creating a simple number array from 1 to 5, but I am not sure how to make this work.

Thanks in advance!

 

Function TEST()
Set TestArray = CreateObject("System.Collections.ArrayList")

For i = 1 To 5
TestArray.Add i
Next i

TEST = TestArray

End Function

Avatar
Cedric McKeever
Member
Members
Level 0
Forum Posts: 31
Member Since:
January 20, 2019
sp_UserOfflineSmall Offline
2
October 22, 2023 - 2:57 am
sp_Permalink sp_Print

Something from ChatGPT:

You need to return a 2D variant array for the function to spill in Excel.

The function should be set up to work as an array function.

Here's a modified version of your VBA code that returns a 2D variant array, which can be spilled in Excel:

 

vba

Copy code

Function TEST() As Variant

    Dim TestArray() As Variant

    Dim i As Integer

 

    ' Redim the variant array to hold the values

    ReDim TestArray(1 To 5, 1 To 1)

 

    For i = 1 To 5

        TestArray(i, 1) = i

    Next i

 

    TEST = TestArray

End Function

How to use this:

 

Press ALT + F11 to open the VBA editor.

Insert a new module.

Paste the function into this module.

Return to Excel, and in a cell, simply type =TEST(). The array will spill from that cell downwards, displaying numbers 1 through 5.

Remember that whenever you edit VBA UDFs (User Defined Functions), you might need to recalculate your sheet to see the updated results if you've changed the code. You can do this by pressing F9.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Alan Lim, Tony Brett, Agnes Welch
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Catalin Bombea: 1864
SunnyKow: 1432
Anders Sehlstedt: 886
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 241
Hans Hallebeek: 222
Jessica Stewart: 219
A.Maurizio: 213
Newest Members:
Agnes Welch
ANTONIO FERNANDEZ FLORO
Arisa Atajewa
Tony Brett
Martha Hennen
Gweneth Henderson
EDWARD JAMES
Ashley Williams
Brian Sutera
Marc Frappier
Forum Stats:
Groups: 3
Forums: 24
Topics: 6638
Posts: 29117

 

Member Stats:
Guest Posters: 49
Members: 33061
Moderators: 2
Admins: 3
Administrators: Mynda Treacy, Philip Treacy, Jessica
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.