+ Reply to Thread
Results 1 to 14 of 14

Excel formula to VBA code conversion

  1. #1
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Excel formula to VBA code conversion

    Hi all,
    I just registered to this forum, sorry to start with a question.
    I have written two formulas in excel and want to extend to many cells in a certain interval and generalize to other similar worksheets. So, I need to write them as vba code. The formulas:

    =INDEX('sheet1'!$A2:$BI$7000,MATCH(MAX('sheet1'!I2:I7),'sheet1'!I2:I7,0),COLUMN('sheet1'!A1))

    =IF(C3=W$3,100,IF(C3<W$4,TREND($V$3:$V$4,W$3:W$4,C3),IF(C3=W$4,85,IF(C3<W$5,TREND(V$4:V$5,W$4:W$5,C3),IF(C3=W$5,5,IF(C3<W$6,TREND($V$5:$V$6,W$5:W$6,C3),0))))))

    I appreciate if any1 can help me to come up with vba codes.

    thanks in advance

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel formula to VBA code conversion

    Welcome to the Forum

    Formula #1
    Please Login or Register  to view this content.
    Formula #2
    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Excel formula to VBA code conversion

    Hi secure
    I find the easiest way is to start a macro recording
    select the cell with the formula and then goto the formula bar select the end of function and press enter.
    the recorded macro will convert the function to RC format
    hope it helps
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    thank you very much guys for the quick replies.
    however, what I need is vba program instead of rc codes. i need to extend them to many cells which satisfy other conditions and it's hard to follow the cell numbers in RC code.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel formula to VBA code conversion

    Quote Originally Posted by secure View Post
    thank you very much guys for the quick replies.
    however, what I need is vba program instead of rc codes. i need to extend them to many cells which satisfy other conditions and it's hard to follow the cell numbers in RC code.
    The formulas need to be in R1C1 format to write to other cells,

    Please Login or Register  to view this content.
    We can't give any better cod without knowing exactly what you want

  6. #6
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    OK, I attached a sample sheet from my data. For each "Name Code" we have 6 rows of info. using this formula I choose the row with maximum "T-Trace" value and get the equivalent values in the row from the columns: A,B,G,L,H,F,J,K,AA and AB. So I will have 10 cells containing this formula:
    Please Login or Register  to view this content.
    Now I calculate the linear interpolation for the above found value comparing it with the score value from "Score Table" using this:
    Please Login or Register  to view this content.
    This formula falls either in exact matches of the score or an interpolation of two values.
    Thanks again for the efforts.
    Attached Files Attached Files

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel formula to VBA code conversion

    Soyou want each formulawritten10 times in a new sheet?

  8. #8
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    this is true for the first set of the data. the sample sheet contains 3 sets (each set includes 6 rows), the real file contains above 100 sets. so the 10 formulas for the first 6 row will be repeated above 100 times to obtain a table of say, 100*10. the second formula acquires this 100*10 and results in a table of 100*7.

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Excel formula to VBA code conversion

    hi secure
    Maybe
    Please Login or Register  to view this content.

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel formula to VBA code conversion

    You coulduse
    Please Login or Register  to view this content.
    Pike:

    You are using With so it should be .Cells

  11. #11
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    Thanks so much guys, will check and revert

  12. #12
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    Thanks all for the helps. But I'm still having problem. I'm not good with RC formulas and maybe that's why. Here's the issue:
    1- The columns I pick the info from are not in a certain order and do not fall in any math formula. So apparently I have to rewrite formula for each cell. This is in fact alright as it is only 10 formulas to be written.
    2- My sample data contains only three sets of info to be picked, while the real data is above 100 sets. So I need to create a loop for this part. i.e. I don't mind to have 10 formulas in a loop of >100.
    3- Each set includes 5 rows, so the loop should increase by step of 5.

    Problem: how to apply the loop and increment (e.g. for i= 1 to 7000 step 5) to cells inside the RC formula?

    Thanks again
    Last edited by secure; 01-10-2010 at 01:20 AM.

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel formula to VBA code conversion

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!

  14. #14
    Registered User
    Join Date
    01-09-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Excel formula to VBA code conversion

    Thanks for replying roy,
    The attached workbook contains 2 sheets, "sheet1" is a data sample with 3 sets as mentioned. Another sheet, "Pilot" includes the calculations I aim. "Pilot" has 3 tables, the first table is directly resulted from data ("sheet1") using "INDEX" formula, the second one compares table 1 to some reference values from table 3 using IF and TREND formula which I use to calculate interpolation.

    First question, consider this sample be extended.
    Second question, since the end of data sets are unspecified and may vary, can I use "IsNonText" or any other function to determine the end of calculation?

    Thanks again,
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1