Solved

VBA Array

Posted on 2013-06-25
4
247 Views
Last Modified: 2013-06-25
Hi

For some reason the following array won't populate 'Name' into A1.  Can someone explain why?

Sub ColumnHeaders()
    Dim myArray As Variant ' Variants can hold any type of data, including arrays
    Dim myCount As Integer
    'myArray = Range("A1:D1").Value
       
    'Fill the variant with array data
    myArray = Array("Name", "Address", "Phone", "Email")
   
    'Empty the array
    With Sheet1
        For myCount = 1 To UBound(myArray)
            .Cells(1, myCount).Value = myArray(myCount)
        Next myCount
    End With
   
End Sub

Greg
0
Comment
Question by:greg_c
  • 3
4 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39276786
With Sheet1
        For myCount = 1 To UBound(myArray)
            .Cells(1, myCount).Value = myArray(myCount - 1)
        Next myCount
    End With

Kevin
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 39276789
The default base for arrays is 0. So when you create the variant array:

    myArray = Array("Name", "Address", "Phone", "Email")

you are creating an array with elements 0 through 3, not 1 through 4.

Also, you can move a single dimension array into a range of cells with one statement:

    Sheet1.Range("A1:D1").Value = Array("Name", "Address", "Phone", "Email")

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39276791
If you want the default base to be 1 use this:

Option Base 1

at the top of your code module.

Kevin
0
 

Author Closing Comment

by:greg_c
ID: 39276819
Thank you.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question