+ Reply to Thread
Results 1 to 20 of 20

Converting formula to a code

Hybrid View

  1. #1
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Converting formula to a code

    I have a simple formula of multiplication

    For ex: =A1*60.

    I would like to know how can i convert this simple formula into a macro coding.

    Can any one help me.
    Last edited by Saky; 11-30-2010 at 12:28 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,355

    Re: Converting formula to a code

    Hi Saky,

    Using VBA, if you wanted your above formula in A2 the code would be:

    Range("A2").Formula = "= A1 * 60"

    hth
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Hi Marvin,

    Thanks for your quick revert!!!!

    If i need this macro to function when i change the cursor from one cell to the other...i meant to say if i want to do it change event how do i need to start the VBA coding.
    Private Sub Worksheet_Change(ByVal Target As Range)
    Range("A2").Formula = "= A1 * 60"
    End sub
    Is that correct???
    Last edited by Saky; 11-29-2010 at 10:39 AM.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    One example
    Range("A2") = Range("A1") * 60
    What are you really wanting to do?
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Please wrap your code in code tags, before the moderators get you...

    Forum rules
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button.

    You need to use the Worksheet SelectionChange event

    An example of what you are trying to do would not go amiss.

    Cheers.

  6. #6
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    oops i am sorry...i have done it...Thanks for saving me

    Marcol, can you give me tell me how to use the Worksheet SelectionChange event.

    If possible can you give me the exact code for the above formula using change event..

    thanks a ton...

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Reading your question again, it might be that either event change could solve your problem.

    If we have an example workbook to work with then your problem will be quickly solved.

    Show the range you want to apply the rule to, and the destination of the results.

    Without a workbook we are only guessing.

  8. #8
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Sorry for troubling you without giving the sample workbook Marcol.

    I have attached a workbook.

    In that if i enter values in say A3, then C3 should give me the result by multiplying the value into 60. and the same way if i enter the value in B3 then C4 sholud show me the result.

    Can you help me to do this using VBA coding. i want this to happen till the range A20 to D20
    Attached Files Attached Files
    Last edited by Paul; 11-30-2010 at 03:33 AM. Reason: Removed quote of full post, unnecessary.

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Try this in the worksheet module
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim isect As Range
    
        If Target.Cells.Count > 1 Then Exit Sub
    
        On Error GoTo ResetApplication
        Application.EnableEvents = False
    
        Set isect = Intersect(Target, Range("A3:A20"))
    
        If Not isect Is Nothing Then
            If Target <> "" Then
                Range("C" & Target.Row) = Target * 60
            Else
                Range("C" & Target.Row) = ""
            End If
        End If
    
    ResetApplication:
        Err.Clear
        On Error GoTo 0
        Application.EnableEvents = True
        Set isect = Nothing
    
    End Sub

    Hope this helps
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Friend, Thanks for your great help...This code is awesome it works great!!!!

    But in my original excel instead of column A,B and C,D i have the columns as Col O,P and Q,R. So i just copy and pasted the coding and made some changes as below. But it is not working in it...Can you clarify where am i going wrong...

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim isect As Range
    
        If Target.Cells.Count > 1 Then Exit Sub
    
        On Error GoTo ResetApplication
        Application.EnableEvents = False
    
        Set isect = Intersect(Target, Range("O3:O20"))
    
        If Not isect Is Nothing Then
            If Target <> "" Then
                Range("Q" & Target.Row) = Target * 60
            Else
                Range("Q" & Target.Row) = ""
            End If
        End If
    
    ResetApplication:
        Err.Clear
        On Error GoTo 0
        Application.EnableEvents = True
        Set isect = Nothing
    
    End Sub
    Last edited by Paul; 11-30-2010 at 03:33 AM. Reason: Removed quote of full post, unnecessary.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Seems to work for me
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Quote Originally Posted by Marcol View Post
    Seems to work for me
    Marcol,

    Its working in the attached excel in this thread...but when i paste the same coding in my original excel it is not working..

    I have hereby attached my original excel...Can you kindly check and advice me where am i going wrong

    Column M is where i will enter the value and i want the result in column O.

    Thanks for your patience...
    Attached Files Attached Files

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    You already have code in the worksheet module for Sheets("Status") the new code must be added to that code.

    You have added the code in a standard module, it will not work from there.

    I'll look at it later, look back in a few hours, remember your time is 5:30 hrs ahead of us in the UK.

  14. #14
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Am really sorry for disturbing you in a very early hours....I don't know that u r located in UK.

    No problem you take ur time and reply me...Now the time here is 3.30(noon).. I will be office till 9.30 PM(night) that will be 4 clock eve in UK. If possible you could reply me during that time it would be great..

    You are so kind Marcol....Thanks for your help so far......

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    What version of Excel are you using?
    Your file is 2003 but some conditions you have applied will not work in that version, you will need 2007 or later.

    Do you need this workbook to work in 2003 and later?

    Please update your profile to indicate which version of Excel you are using.

  16. #16
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Marcol,

    I am using Microsoft Office Excel 2003. I do not have any advanced version. I have updated the same in my profile.

    I want this macro to function in Excel 2003. Can you kindly help.

  17. #17
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Hi Marcol,

    Any luck on this? Can we write the coding in Module2 and sheet2 tabs and can we use the change and worksheet activate events to fire the code.
    As i did in my attached excel...Is that can be done???

  18. #18
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Try this version of your workbook

    1/. I have added a sheet "Lookups" and created dynamic named lists for each catagory you require for data validation, you can add /change/ delete any data in these lists.
    Don't leave blank cells in these lists, if you want to delete an item use Delete Cells > Shift Cells Up

    This makes the code in the worksheet change event much simpler.

    This Lookup sheet can be hidden.

    2/. I have changed you macro "Test" to "TestStatus" and moved it's trigger to the ThisWorkbook module, Workbook_Open event, and also called it in the Worksheet_Change event (Sheets("Status"))

    I have assumed that each project will have an SI number and the last row in this column ("A") determines the number of rows to check the status as changes are made.

    3/, 2003 can only handle three conditional formats, I have changed the base colour of Column J to allow the colour codes you require

    Try changing the data in Column E to test the drop-Down in F

    Try changing dates in columns H K & L to se the result in column I, you probably don't need the validation in column I, but I have left it just in case.

    Hope this helps
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    11-17-2010
    Location
    India
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    83

    Re: Converting formula to a code

    Marcol,

    Thanks is a very simple word for your great help!!!!! Really this excel works great and satisfy my expectation.

    Marcol am really sorry for troubling you so long to make you do my homework..

    Really you helped me a lot.. Thanks a million again for your time and help!!!

  20. #20
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Converting formula to a code

    Happy to help.

    You would do well to have a look as this link on Dynamic Named Lists

    http://www.cpearson.com/Excel/excelF.htm#DynamicRanges

    and follow the links from there

+ 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