Solved

VBA Array

Posted on 2013-06-25
4
226 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)
Comment Utility
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
Comment Utility
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)
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 …
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now