Solved

VBA Array

Posted on 2013-06-25
4
237 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now