4

I am creating column headers for a bunch of columns. I would like to do them in one like below - but I couldn't get the syntax right. Can anyone help?

Range("AB1:AE1").Value = ("UnitPriceUSD","TotalCostUSD","UnitPrice", "TotalCost")

1 Answer 1

6

Try this.

Dim arr As Variant
arr = Array("UnitPriceUSD", "TotalCostUSD", "UnitPrice", "TotalCost")

Range("AB1:AE1").Value = arr

Or even simpler:

Range("AB1:AE1").Value = Array("UnitPriceUSD", "TotalCostUSD", "UnitPrice", "TotalCost")
Sign up to request clarification or add additional context in comments.

4 Comments

do I need to declare any array first? I just tried this and it works - Range("AB1:AE1").Value = Array("UnitPriceUSD","TotalCostUSD","UnitPrice", "TotalCost"). Someone else posted this as an answer but deleted it.
Yes that seems to be enough, I always overcomplicate things.
Works for assigning across columns in a given row. But Range("A1:A2") = Array("a","b") assigns "a" to both A1 and A2.I assume the Array needs to be 2-dimensional, but Array only appears to support creation of 1D arrays.
@GrahamMonkman. Look at stackoverflow.com/q/67014070/1955013 for the Ted Williams comment. [{"a", "b"; "c", "d"}] will create a 2-dimensional array

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.