Not able to copy a VBA array with formulas into an Excel range

  • Thread starter musicgold via OfficeKB.com
  • Start date
M

musicgold via OfficeKB.com

Hi,

I am trying to transfer the contents of an array into a range of Excel (red
line in the code below). Though I am able to do that when the array has
values, however, it is giving me an error when the array has formulas.
How can I get this to work?

VBA Code:
...
Dim Vma As Variant
...
TotalRows = .Range("Spread").Count

Vma = Application.Transpose(.Range("Spread"))


For i = TotalRows To 1 Step -1

Vma(i) = "=RC[-2]- RC[-1])"

Next

.Range("Spread") = Application.WorksheetFunction.Transpose(Vma)
...



Thanks,

MG.
 
D

Dave Peterson

Your formula isn't valid (the ()'s don't match):

Vma(i) = "=RC[-2]- RC[-1])"
should be:
Vma(i) = "=(RC[-2]- RC[-1]"
or
Vma(i) = "=(RC[-2]- RC[-1])"

I'm not sure if this is a simplified version, but how about:

with worksheets("somesheetnamehere")
.range("Spread").formular1c1 = "=RC[-2]- RC[-1]"
end with

ps. I'd be explicit with the .formular1c1 in your code, too.

musicgold via OfficeKB.com said:
Hi,

I am trying to transfer the contents of an array into a range of Excel (red
line in the code below). Though I am able to do that when the array has
values, however, it is giving me an error when the array has formulas.
How can I get this to work?

VBA Code:
...
Dim Vma As Variant
...
TotalRows = .Range("Spread").Count

Vma = Application.Transpose(.Range("Spread"))


For i = TotalRows To 1 Step -1

Vma(i) = "=RC[-2]- RC[-1])"

Next

.Range("Spread") = Application.WorksheetFunction.Transpose(Vma)
...

Thanks,

MG.
 
M

musicgold via OfficeKB.com

Dave,

Thanks. You are right; the formula was wrong. How silly of me!

MG

Dave said:
Your formula isn't valid (the ()'s don't match):

Vma(i) = "=RC[-2]- RC[-1])"
should be:
Vma(i) = "=(RC[-2]- RC[-1]"
or
Vma(i) = "=(RC[-2]- RC[-1])"

I'm not sure if this is a simplified version, but how about:

with worksheets("somesheetnamehere")
.range("Spread").formular1c1 = "=RC[-2]- RC[-1]"
end with

ps. I'd be explicit with the .formular1c1 in your code, too.
[quoted text clipped - 27 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top