Click here to Skip to main content
15,891,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a VB code like the one given below.
I'm trying to achieve the same functionality in c#. But could not find the Replace function as in VB.
VB Code
VB
Index = 2;
formula = "8.09331951577158-3.45596769548775E-02*X^1+1.79729745455284E-05*X^2-3.31696353974908E-09*X^3+2.77566533348736E-13*X^4-8.40169194251658E-18*X^5";

Set xRange = vSheet.Range(vSheet.Cells(1, (Index * 4) - 1), vSheet.Cells(500, (Index * 4) - 1))
        Set yRange = vSheet.Range(vSheet.Cells(1, (Index * 4)), vSheet.Cells(500, (Index * 4)))

        xMinVal = 390
        
        xMaxVal = 13400
    
        step = (xMaxVal - xMinVal) / (500- 1)
        
        Xvalue(1, 1) = xMinVal
        For i = 2 To 500 - 1
            Xvalue(i, 1) = Xvalue(i - 1, 1) + step
        Next i
        Xvalue(500, 1) = xMaxVal
        
        xRange.value = Xvalue
        vSheet.Names.Add name:="X" & Index & "P", RefersTo:=xRange
        yRange = "=" & Replace(Formula, "X", "X" & Index & "P")

c# Code
C#
xRange = (Excel.Range)newWorksheet.get_Range((Excel.Range)newWorksheet.Cells[1, (2 * 4) - 1], (Excel.Range)newWorksheet.Cells[NB_POINTS_POLYNOME, (2 * 4) - 1]);
           yRange = (Excel.Range)newWorksheet.get_Range((Excel.Range)newWorksheet.Cells[1, (2 * 4)], (Excel.Range)newWorksheet.Cells[NB_POINTS_POLYNOME, (2 * 4)]);

           step = (xmax - xmin) / (NB_POINTS_POLYNOME - 1);
           xValue[1,0] = xmin;

           for (int i = 2; i < 500; i++)
           {
               xValue[i,0] = xValue[(i - 1),0] + step;
           }
           xValue[NB_POINTS_POLYNOME-1,0] = xmax;
           xRange.Value2 = xValue;
          
    newWorksheet.Names.Add("X", xRange);
yRange = (Excel.Range)newWorksheet.Range.Replace(formula, "X", "X", 2, Type.Missing, Type.Missing, Type.Missing, Type.Missing);


I am unable to retrieve Y range values. I am not getting how to replace the formula as in VB so that Yrange is retrieved correctly.

Thanks in advance.
Jyoti
Posted
Updated 5-May-14 10:04am
v2

"X" & Index & "P" is a string concatention in basic

http://msdn.microsoft.com/en-us/library/te2585xw.aspx[^]

Given Index = 2 the string is "X2P" change it in the two places it is used
 
Share this answer
 
Replace:
C#
yRange = (Excel.Range)newWorksheet.Range.Replace(formula, "X", "X", 2, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

with:
C#
yRange = "=" + Replace(formula, "X", "X" + Index.ToString() + "P");

which will give you new formula
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900