Current cell formula and and vba string error

random_work

New Member
Joined
Apr 5, 2012
Messages
5
Hi.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Im trying to sum the cell to the left and the cell 2 steps to the left in a excel, I have to paste this code to different places in sheet decided by a macro.<o:p></o:p>
I found the formula to be<o:p></o:p>
=INDIREKT(ADRESS(RAD();KOLUMN()-2;4;1))+INDIREKT(ADRESS(RAD();KOLUMN()-1;4;1))<o:p></o:p>
Simular in englich<o:p></o:p>
=INDIRECT(ADDRESS (ROW();COLUMN()-2;4;1))+INDIRECT(ADDRESS (ROW();COLUMN()-1;4;1))<o:p></o:p>
This formula works but my problem are that I have to past it in to different cell with a macro/VBA and then the vba refuses…<o:p></o:p>
Simplified the code are<o:p></o:p>
Sub test()<o:p></o:p>
Dim del(1 To 2) As String
Dim iCount As Integer<o:p></o:p>
iCount = 1
‘del(1) = "INDIREKT(ADRESS(RAD();KOLUMN()-2;4;1))+INDIREKT(ADRESS(RAD();KOLUMN()-1;4;1))"
del(1)=”INDIRECT(ADDRESS (ROW();COLUMN()-2;4;1))+INDIRECT(ADDRESS (ROW();COLUMN()-1;4;1))”
ActiveCell = "=" + del(iCount)
End Sub<o:p></o:p>
This giving me error and the macro don’t add the string to activecell but if I past it manually it works.<o:p></o:p>
Why? Howe do I fix it and do any one have a better solution? <o:p></o:p>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try

del(1)=”=INDIRECT(ADDRESS (ROW();COLUMN()-2;4;1))+INDIRECT(ADDRESS (ROW();COLUMN()-1;4;1))”

note inserted '=' "=Indirect....

ActiveCell.Formula = del(iCount)
 
Upvote 0
That does not work.... Same error.
if i thry your sugestion in excell (swedish brand :-) )

Sub testx()
Dim del(1 To 2) As String
Dim iCount As Integer
iCount = 1
del(1) = "=INDIREKT(ADRESS(RAD();KOLUMN()-2;4;1))+INDIREKT(ADRESS(RAD();KOLUMN()-1;4;1))"
ActiveCell.Formula = del(iCount)
End Sub

It does not work.
 
Upvote 0
I just tested this and it works fine

Code:
Sub testx()
Dim del(1 To 2) As String
Dim iCount As Integer
iCount = 1
del(iCount) = "=INDIRECT(ADDRESS(ROW(),COLUMN()-2,4,1))+INDIRECT(ADDRESS(ROW(),COLUMN()-1,4,1))"
ActiveCell.Formula = del(iCount)
End Sub

selected a cell in column C
ran the macro
it added same row Col A to Same Row Col B
worked fine

why you are using INDIRECT?
 
Upvote 0
actually

del(iCount) = "=INDIRECT( " & ActiveCell.Offset(, -2).Address & ")+INDIRECT(" & ActiveCell.Offset(, -1).Address & ")"

Might be a better solution?

Or this to maintain relative reference

del(iCount) = "=INDIRECT( " & Replace(ActiveCell.Offset(, -2).Address, "$", "") & ")+INDIRECT(" & Replace(ActiveCell.Offset(, -1).Address, "$", "") & ")"
 
Last edited:
Upvote 0
It Works Thanks!
I think some thing are wrong in the translation betwean swedich and english versions.

Sub Strange()
Dim del(1 To 2) As String
Dim iCount As Integer
iCount = 1
'This work "english version" but not to type in to the cell manualy
del(iCount) = "INDIRECT(ADDRESS(ROW(),COLUMN()-2,4,1))+INDIRECT(ADDRESS(ROW(),COLUMN()-1,4,1))"
ActiveCell.Formula = "=" + del(iCount)
' This does not work "swedish version" but it works by typing in to the cell manualy
del(iCount) = "INDIREKT(ADRESS(RAD();KOLUMN()-2;4;1))+INDIREKT(ADRESS(RAD();KOLUMN()-1;4;1))"
ActiveCell.Formula = "=" + del(iCount)
' This is tested on Ver:14.0.4760.1000 32-bit swedish version of Excell
End Sub


Thanks a lot now it work for me any way!
 
Upvote 0
Im not using vba to calculate "the thing" im using vba to past the code in to a cell. VBA are using the "code" as text string.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
And when vba have paste the code in to a cell i believe excel should behave the same way if i pasted the code in the cell by hand ore pasted by vba code...<o:p></o:p>
Microsoft are notifyed lets se what they reply..
 
Upvote 0
You use commas in the string that you pass to the Formula property. They will be converted to semicolons automatically.
 
Upvote 0
! Ok. So a textstring that excel seas as a formula coming from vba/macro are handled different then the textstrings that comes from the Ctr+V /Crtl+C? That was odd but its a explanation. Thanks.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top