Giving Cells a Formula

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hey folks,

(Excel Noob here)

I need to give certain cells a formula through code. I need it to grab the
formulas from another Excel spreadsheet's cells. I think I'm pretty close
here, but the following errors out. Do you see what's wrong?

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula = "='"
& otherExcelFilePath & "'!OrigSheet:$A" & CStr(i)
Next i

So I want the cells in row rownum from cols colStart to colEnd to contain
the formulas in the other Excel spreadsheet. The other Excel Spreadsheet
should use it's worksheet OrigSheet, row A, cols colStart to colEnd.

Thx!
 
--

HTH

RP
(remove nothere from the email address if mailing direct)
 
John,

Not tested, but try

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula =
"='[" & _
otherExcelFilePath & "]OrigSheet'!$A" & CStr(i)
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Hi...Thx.

I tried your suggestion, but I get the error:
"You cannot change part of an array."

So, instead I tried using what you wrote like this:
ActiveWorkbook.Worksheets("MyWorksheet").Range("MyRange").Rows(rowcount).For
mulaArray = "='[" & thePathname & "]Reporting'!$A4"

but I get the error:
"Unable to set the FormulaArray property of the Range class"




Bob Phillips said:
John,

Not tested, but try

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula =
"='[" & _
otherExcelFilePath & "]OrigSheet'!$A" & CStr(i)
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)


John Smith said:
Hey folks,

(Excel Noob here)

I need to give certain cells a formula through code. I need it to grab the
formulas from another Excel spreadsheet's cells. I think I'm pretty close
here, but the following errors out. Do you see what's wrong?

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula = "='"
& otherExcelFilePath & "'!OrigSheet:$A" & CStr(i)
Next i

So I want the cells in row rownum from cols colStart to colEnd to contain
the formulas in the other Excel spreadsheet. The other Excel Spreadsheet
should use it's worksheet OrigSheet, row A, cols colStart to colEnd.

Thx!
 
Sorry John,

try this instead

For i = colStart To colEnd
iPos = InStrRev(otherExcelFilePath, "\")
otherExcelFilePath = "='" & Left(otherExcelFilePath, iPos - 2) & _
"[" & Right(otherExcelFilePath, Len(otherExcelFilePath) - iPos) &
"]"
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula = _
otherExcelFilePath & "OrigSheet'!$A" & CStr(i)
Next i

--

HTH

RP
(remove nothere from the email address if mailing direct)


John Smith said:
Hi...Thx.

I tried your suggestion, but I get the error:
"You cannot change part of an array."

So, instead I tried using what you wrote like this:
ActiveWorkbook.Worksheets("MyWorksheet").Range("MyRange").Rows(rowcount).For
mulaArray = "='[" & thePathname & "]Reporting'!$A4"

but I get the error:
"Unable to set the FormulaArray property of the Range class"




Bob Phillips said:
John,

Not tested, but try

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula =
"='[" & _
otherExcelFilePath & "]OrigSheet'!$A" & CStr(i)
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)


John Smith said:
Hey folks,

(Excel Noob here)

I need to give certain cells a formula through code. I need it to
grab
the
formulas from another Excel spreadsheet's cells. I think I'm pretty close
here, but the following errors out. Do you see what's wrong?

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula
=
"='"
& otherExcelFilePath & "'!OrigSheet:$A" & CStr(i)
Next i

So I want the cells in row rownum from cols colStart to colEnd to contain
the formulas in the other Excel spreadsheet. The other Excel Spreadsheet
should use it's worksheet OrigSheet, row A, cols colStart to colEnd.

Thx!
 
That was very helpful. Thanks alot. Just had to get rid of the minus 2
though :)

Bob Phillips said:
Sorry John,

try this instead

For i = colStart To colEnd
iPos = InStrRev(otherExcelFilePath, "\")
otherExcelFilePath = "='" & Left(otherExcelFilePath, iPos - 2) & _
"[" & Right(otherExcelFilePath, Len(otherExcelFilePath) - iPos) &
"]"
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula = _
otherExcelFilePath & "OrigSheet'!$A" & CStr(i)
Next i

--

HTH

RP
(remove nothere from the email address if mailing direct)


John Smith said:
Hi...Thx.

I tried your suggestion, but I get the error:
"You cannot change part of an array."

So, instead I tried using what you wrote like this:
ActiveWorkbook.Worksheets("MyWorksheet").Range("MyRange").Rows(rowcount).For
mulaArray = "='[" & thePathname & "]Reporting'!$A4"

but I get the error:
"Unable to set the FormulaArray property of the Range class"




Bob Phillips said:
John,

Not tested, but try

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula =
"='[" & _
otherExcelFilePath & "]OrigSheet'!$A" & CStr(i)
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)


Hey folks,

(Excel Noob here)

I need to give certain cells a formula through code. I need it to grab
the
formulas from another Excel spreadsheet's cells. I think I'm pretty close
here, but the following errors out. Do you see what's wrong?

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i,
rownum).Formula
 
That's odd. I tested that one, and that was where I got the -2 from :-).

Aah well!

Bob


John Smith said:
That was very helpful. Thanks alot. Just had to get rid of the minus 2
though :)

Bob Phillips said:
Sorry John,

try this instead

For i = colStart To colEnd
iPos = InStrRev(otherExcelFilePath, "\")
otherExcelFilePath = "='" & Left(otherExcelFilePath, iPos - 2) & _
"[" & Right(otherExcelFilePath, Len(otherExcelFilePath) - iPos) &
"]"
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i, rownum).Formula = _
otherExcelFilePath & "OrigSheet'!$A" & CStr(i)
Next i

--

HTH

RP
(remove nothere from the email address if mailing direct)


John Smith said:
Hi...Thx.

I tried your suggestion, but I get the error:
"You cannot change part of an array."

So, instead I tried using what you wrote like this:
ActiveWorkbook.Worksheets("MyWorksheet").Range("MyRange").Rows(rowcount).For
mulaArray = "='[" & thePathname & "]Reporting'!$A4"

but I get the error:
"Unable to set the FormulaArray property of the Range class"




John,

Not tested, but try

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i,
rownum).Formula
=
"='[" & _
otherExcelFilePath & "]OrigSheet'!$A" & CStr(i)
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)


Hey folks,

(Excel Noob here)

I need to give certain cells a formula through code. I need it to grab
the
formulas from another Excel spreadsheet's cells. I think I'm pretty
close
here, but the following errors out. Do you see what's wrong?

For i = colStart To colEnd
ActiveWorkbook.Worksheets("MyWorksheet").Cells(i,
rownum).Formula
=
"='"
& otherExcelFilePath & "'!OrigSheet:$A" & CStr(i)
Next i

So I want the cells in row rownum from cols colStart to colEnd to
contain
the formulas in the other Excel spreadsheet. The other Excel
Spreadsheet
should use it's worksheet OrigSheet, row A, cols colStart to colEnd.

Thx!
 

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

Back
Top