Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA Array

Posted on 2013-06-25
4
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

715 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