+ Reply to Thread
Results 1 to 2 of 2

Insert three formulas one in each column paste values

Hybrid View

  1. #1
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Insert three formulas one in each column paste values

    Hi.
    I need to put a formula in each row and then drag down, and paste values​​.

    tried the code below, but it was cool.
    Sub InputFormula()
    Dim lr As Range
        'cell I1=If(J2<>"""",H2,"""")
        'celula J1=If(Countif(B:B,H2)>0,Countif(B:B,H2)
        'celula K1=If(Sumif(B:B,H2,C:C)>0,Sumif(B:B,H2,C:C),"""")
        
        With Sheets("Result")
        Set lr = .Cells(.Rows.Count, "H").End(xlUp).Offset(, 1)
        With .Range("I2", lr)
            .Formula = "=If(J2<>"""",H2,"""")"
            .Value = .Value 'if you want to convert the formulae to hard values.
        End With
        Set lr = .Cells(.Rows.Count, "H").End(xlUp).Offset(, 2)
        With .Range("J2", lr)
            .Formula = "=If(Countif(B:B,H2)>0,Countif(B:B,H2),"""")"
            .Value = .Value
        End With
        
        Set lr = .Cells(.Rows.Count, "H").End(xlUp).Offset(, 3)
        With .Range("K2", lr)
            .Formula = "=If(Sumif(B:B,H2,C:C)>0,Sumif(B:B,H2,C:C),"""")"
            .Value = .Value
        End With
        End With
    
    End Sub
    if someone can improve this code, I'd be grateful!
    Thank you!!!
    Last edited by marreco; 10-27-2012 at 12:38 PM.
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Insert three formulas one in each column paste values

    How about (untested)

    Sub InputFormula()
        Dim lr As Long
        Const sFormula1 As String = "=If(J2<>"""",H2,"""")"
        Const sFormula2 As String = "=If(Countif(B:B,H2)>0,Countif(B:B,H2),"""")"
        Const sFormula3 As String = "=If(Sumif(B:B,H2,C:C)>0,Sumif(B:B,H2,C:C),"""")"
    
        With Sheets("Result")
            lr = .Cells(.Rows.Count, "H").End(xlUp).Row + 1
            .Range("I2").Resize(lr).Formula = sFormula1
            .Range("J2").Resize(lr).Formula = sFormula2
            .Range("K2").Resize(lr).Formula = sFormula3
            .Range("I2").Resize(lr, 3).Value = .Range("I2").Resize(lr, 3).Value
        End With
    End Sub
    HTH
    Regards, Jeff

+ 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