2
aProducts(lRow) = Array("Id Product", "Value A", "Value B")

'To copy value A
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, .Cells(aPos(2)).Value2, Empty)

'To copy value B
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, .Cells(aPos(3)).Value2)

Hi guys,

I use this piece of code in a loop to copy some values and their ID, but anyway, now I'd like to understand the usage of "Empty" in this code in order to add more value.

I tried:

aProducts(lRow) = Array("Id Product", "Value A", "Value B", "Value C")

'To copy value A
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, .Cells(aPos(2)).Value2, Empty)

'To copy value B
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, .Cells(aPos(3)).Value2)

'To copy value C
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, Empty, .Cells(aPos(4)).Value2)

"Execusiion error 9"

Thanks in advance and sorry for my approximate english :/

EDIT 1

you can get the file here : https://drive.google.com/file/d/0B5DpGwPWsIfbWWlJRDAzZldYek0/view?usp=sharing

EDIT 2

Thanks to all of you, I just found that i've forgot to remove

ReDim Preserve aProducts(lRow)
6
  • Did the error only occur after you added "To copy value C"? Commented Nov 16, 2016 at 13:35
  • the first code works when I don't try to add my value C Commented Nov 16, 2016 at 13:35
  • Can you provide a download link for the workbook? If not I'm going to have to see more code to determine the problem. Commented Nov 16, 2016 at 13:49
  • i'll edit, thank you for your time Commented Nov 16, 2016 at 13:52
  • It's a 1 hour meeting, can't post it before, sorry had no choice Commented Nov 16, 2016 at 14:02

2 Answers 2

3

Write the following:

Option Explicit

Sub TestMe()

    Dim arr_var     As Variant

    arr_var = Array(Empty, Empty, 5)
    Stop
End Sub

Then run it. Click with mouse over arr_var, press Shift + F2 and you will see this: "Leer" means "Empty" in German. enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Essentially, Empty is the initial / default value for any new array. You could also just issue the Stop after Dim arr_var(2) and you'd see that you've got an array of three elements all of which are Empty. So, by using Empty you are "resetting" the array to it's initial emptiness.
Oh right, didn't see the screenshot on mobile version thank you again, I'll be back in an hour :/
Thanks to your help, i found my mistake: i forgot to delete ReDim Preserve aProducts(lRow) that I forgot between my B and C values. Thanks for your tips, and have a great day @Vityata ! ;)
Welcome :) Its almost night at my place :)
Also have a great "almost night" :)
|
1

Does this work for you? It looks like you forgot to update the last one when you copied it.

aProducts(lRow) = Array("Id Product", "Value A", "Value B", "Value C")

'To copy value A
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, .Cells(aPos(2)).Value2, Empty, Empty)

'To copy value B
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, .Cells(aPos(3)).Value2, Empty)

'To copy value C
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, Empty, .Cells(aPos(4)).Value2)

I also think you wanted to update .Cells(aPos(4)).Value2) to have a 4 instead of a 3.

2 Comments

I tried it but forgot to change it and it did not work (first thing it tried xD), i'll edit
I'll post it after my meeting

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.