+ Reply to Thread
Results 1 to 11 of 11

vba code for excel formula

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    113

    Post vba code for excel formula

    Hi all,

    I want vba code for my excel formulas, I attached my original sheet and I want vba code for all the formulas in it.
    In my attachment yellow coloured columns contains the formulas based on its left column cell value.


    Thanks in advance

    Dev
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,627

    Re: vba code for excel formula

    Try this macro
    Sub Macro1()
     Dim TC As Long
     
     For TC = 0 To 18 Step 2
     Range("D3:D27").Offset(0, TC).FormulaR1C1 = "=RC[-1]*2.9768*4/4"
     Next TC
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-23-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: vba code for excel formula

    Dear kvsrinivasamurthy ,

    Thanks for the code, but I don't want to put any (macro) button on the sheet,Want the code which will work exactly the formula does.without putting any formulas in excel sheet.

    Thanks in advance
    Dev

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,627

    Re: vba code for excel formula

    Try this code
    Sub Macro1()
     Dim TC, TR As Long
     
     For TC = 0 To 18 Step 2
    	For TR = 3 To 27
    	
    	Range("D" & TR).Offset(0, TC) = Range("D"&TR).Offset(0, TC-1)*2.9768*4/4
     	
    	Next TR
    Next TC
    End Sub

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: vba code for excel formula

    Hi Dev

    Is your Pune Sheet complete or might there be additional items?
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Forum Contributor
    Join Date
    04-23-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: vba code for excel formula

    Dear jaslake,

    Yes there will be addition in Pune Sheet.

    Dev.

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: vba code for excel formula

    Hi Dev

    Will there be new Formulas with these new additions? Can you supply a Table of all possible Formulas?

  8. #8
    Forum Contributor
    Join Date
    04-23-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: vba code for excel formula

    Hi jaslake ,

    I have added all the possible new additions and formulas in new attachment (dev sample1).

    Thanks in advance

    Dev
    Attached Files Attached Files

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: vba code for excel formula

    Hi Dev

    The attached File has an "Initial Set Up" routine that creates a Sheet called "Lists". This Sheet contains a Table of all the Formulas as extracted from Sheet Pune. It also CLEARS ALL DATA AND FORMULAS from Sheet Pune, Columns C to the last Column to the Right.

    This Code must be run ONLY ONCE as it destroys all Formulas in Sheet Pune. The Code is in Module 2 and can be executed with CTRL + x
    Option Explicit
    
    Sub Initial_Setup()
        Dim ws As Worksheet, ws1 As Worksheet
        Dim LR As Long, i As Long, LC As Long
        Dim cel As Range
        Dim LastValue As String
    
        Application.ScreenUpdating = False
        Set ws = Sheets("Pune")
        If Not Evaluate("ISREF(Lists!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
        Else
            Sheets("Lists").Cells.Clear
        End If
        Set ws1 = Sheets("Lists")
    
        With ws
            .Unprotect
            LR = .Columns("A").Find(What:="TOTAL", LookIn:=xlValues, lookat:=xlPart).Row - 1
            LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious).Column
            .Range("A3:D" & LR).Copy ws1.Range("A3")
            With ws1
                .Activate
                LastValue = ""
                For Each cel In .Range("A4:A" & LR)
                    If Trim(cel.Value) <> "" Then
                        LastValue = cel.Value
                    Else
                        If LastValue <> "" Then cel.Value = LastValue
                    End If
                Next cel
                ActiveWindow.DisplayFormulas = True
            End With
            For i = 3 To LC Step 1
                .Range(.Cells(3, i), .Cells(LR, i)).ClearContents
            Next i
            .Protect
            .Activate
        End With
        Application.ScreenUpdating = True
    End Sub
    In Sheet Pune all of the Data Entry Cells have been unlocked (Columns C, E, G, etc to the Right). This Code is in the Workbook Open Event and Protects Sheet Pune but allows Macros to Update the Sheet
    Option Explicit
    
    Private Sub Workbook_Open()
        Dim ws As Worksheet
        Set ws = Sheets("Pune")
        'set protection using UserInterface to allow macros to work
        With ws
            .Protect _
                    DrawingObjects:=True, _
                    Contents:=True, _
                    Scenarios:=True, _
                    AllowFiltering:=True, _
                    UserInterfaceOnly:=True
            .EnableSelection = xlUnlockedCells
        End With
    End Sub
    This is the Code that does the Dirty work...it's in the Sheet Module of Pune
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myFormula As String
        If Target.Cells.Count > 1 Then Exit Sub
        Select Case Target.Column
        Case 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25
            Application.EnableEvents = False
            myFormula = Replace(Sheets("Lists").Cells(Target.Row, "D").Text, "C", ColumnLetter(Target.Column))
    
            Target.Offset(0, 1).Value = myFormula
            Target.Offset(0, 1).Value = Target.Offset(0, 1).Value
            Application.EnableEvents = True
        Case Else
            Exit Sub
        End Select
    End Sub
    
    
    Function ColumnLetter(ColumnNumber As Long) As String
    ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function
    You can add as many Columns to the Right as you wish. You CANNOT add rows (you can but it requires special instructions). Let me know of issues.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    04-23-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: vba code for excel formula

    Dear Sir,

    Thank you so much...


    Dev.

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: vba code for excel formula

    You're welcome...glad I could help.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 11-13-2013, 10:28 AM
  2. how to right excel formula with vba code.
    By Sushil.thakur in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2013, 04:59 AM
  3. Excel Formula into DBA code
    By moroformat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2013, 04:54 AM
  4. [SOLVED] Change my code into Excel formula code
    By DGARRETT1 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-04-2013, 07:51 PM
  5. Excel Formula in VBA code
    By kieranbop in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2011, 10:24 AM

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