0

I am struggling with a conversion to array in VBA :

Dim list As Range
Set list = Range(series, series.End(xlDown))
list.Select
Dim table() As Variant
Set table = list.value

My understanding is that value returns an array right ? I don't get why VBA tells me that I "can't assign to array".

My list range looks like that in Excel at the Select, and i aim at having that as an array so that I can format my dates using something like Format(table.Rows(i).Columns(1).value, "yyyy-mm-dd"), looping on my table.

02-Sep-09   1.00
18-Sep-09   1.00
16-Oct-09   1.00
20-Nov-09   1.00
18-Dec-09   1.00
19-Mar-10   1.00
18-Jun-10   1.00

By the way, is it possible to modify the table in place ?

Thanks !

4 Answers 4

4

There are a number of problem here.

Problem 1

Set table = list.value

table is not an object so you cannot set it. Try:

table = list.value

Problem 2

series is a VBA keyword associated with charts. Please pick a name, such as MyWSTable, which means nothing to VBA.

Problem 3

A worksheet name is not itself a range. Try:

Dim Table() As Variant
Table = Names("MyWSTable").RefersToRange.Value

Note: you do not need variable list nor do you need to select the range.

Answer to formatting question

The following code will reformat your dates:

For inxrow = 1 To UBound(Table, 1)
  For inxcol = 1 To UBound(Table, 2)
    Table(inxrow, 1) = "ddd d mmm yyyyy"
    Table(inxrow, 2) = ""
  Next
Next

Names("MyWSTable").RefersToRange.NumberFormat = Table
6
  • 5
    Extra warning. Moving dates from a worksheet to an array then back again is not safe unless you use American dates. The move from sheet to array is OK. But, on the move back, if the date can be interpreted as an American date, it will be. So "16 Oct 2009" will remain unchanged because "16/10/2009" is not a valid American date but "2 Sep 2009" will become "9 Feb 2009". You must convert dates to unambiguous strings before copying the array to or back to the worksheet. Nov 30, 2011 at 11:01
  • 3
    this is not true if you use the numeric Date data type; in that case, the date is represented as an IEEE double. And how did you choose the dates that are the birthdays of my father and mother in law?
    – phoog
    Nov 30, 2011 at 19:28
  • I discovered this problem some years ago when I first tried Excel VBA. It was a while before I realised that many of the dates in my database were wrong and it then took me a lot of experimentation before I identified the cause. I had started with Date variables but I tried other formats but the only reliable method I found of placing a date in a cell was to use strings in the format ddmmyyyy. I have kept the same approach ever since so it is possible that this error was fixed in a later version of Excel without me noticing. You will have to ask jerome G where he got the dates. Nov 30, 2011 at 23:53
  • I omitted an "m" from the format string in my previous comment. I meant "ddmmmyyyy". For example: 2Dec2011. Dec 2, 2011 at 16:12
  • SQL (at least JET SQL) requires American date format for date literals, which would explain why database dates were incorrect, but if you have a VBA Date-type value, the value is purely numeric and does not have a culture-specific format. The culture specific format only comes into play when you are converting to a string (e.g., for display) or from a string (e.g., parsing input).
    – phoog
    Dec 2, 2011 at 17:11
2

You can remove your select: list.Select

To be sure you won't get errors when assigning a range to an array, you'd better declare your array as a variant:

Dim table As Variant
table = Range(series, series.End(xlDown)).Value
1

That error message is popping because you are using SET to fill the array. Drop the SET and it should load.

You can fil directly the array as follows:

Dim table() As Variant
table=Range(series, series.End(xlDown))

Omiting the Select step makes your code safer and faster.

Unfortunately, you can not load the values directly as dates. You will have to loop throu every item of the array and turn them into date.

0

You are going about this incorrectly. You should use the NumberFormat property of the range to specify the display format. Something like this:

Range("A:A").NumberFormat = "yyyy-mm-dd"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.