+ Reply to Thread
Results 1 to 8 of 8

Need a macro instead of SUMIF & COUNTIFS formula

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2012
    Location
    Tehran, Iran
    MS-Off Ver
    Excel 2007 , 2010
    Posts
    4

    Need a macro instead of SUMIF & COUNTIFS formula

    Hi
    I have 400 csv file that they are output of a program and now I need calculate something on them
    first of all I find a code to import all of my csv to an excel file
    then by using SUMIF and COUNTIF I calculate my needs But I Need to know if there are better way

    I Used Something like this
    =IF(J2=$E$2,COUNTIFS($A$3:$A$38,$A$43,E3:E38,"c")/(COUNTIFS($A$3:$A$38,$A$43,E3:E38,"c")+COUNTIFS($A$3:$A$38,$A$43,E3:E38,"w")))
    and
    =IF(F2=$F$2,SUMIF($A$3:$A$38,$A$44,F3:F38))
    ExcelForum.xlsx
    and as you see in the attached file the output is not suitable for bigger file
    is there any better solution ?

    thank you in advanced
    Keyvan,
    Last edited by Keyvan; 07-29-2013 at 07:47 PM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need a macro instead of SUMIF & COUNTIFS formula

    Maybe you could use Current Region and Offset to apply your formulas?

    Sample-
    Sub keyvan()
    Dim x As Long
    Range("A3").Select
    x = ActiveCell.CurrentRegion.Rows.Count
    Range("G" & x).Offset(2).Formula = "=SUM(F3:F" & x & ")"
    Range("L" & x).Offset(2).Formula = "=SUM(K3:K" & x & ")"
    
    End Sub











    sw

  3. #3
    Registered User
    Join Date
    05-28-2012
    Location
    Tehran, Iran
    MS-Off Ver
    Excel 2007 , 2010
    Posts
    4

    Re: Need a macro instead of SUMIF & COUNTIFS formula

    Thanks JOHN

    in fact I have a sheet with More than 2000 Columns and I need to run 3 formulas for every 5 columns based on values of first cell's of each row (status)
    I tried to use this code

    Option Explicit
    
    Sub keyvan()
    Dim i As Long, j As Long, x As Long, lColumn As Long
    
    Range("A3").Select
    
    lColumn = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
    x = ActiveCell.CurrentRegion.Rows.Count
        For i = 2 To lColumn Step 5
            Dim timecell As String
            timecell = i & x
                    Range(timecell).Offset(2).Formula = "=SUMIF($A$3:$A$38,Positive,i&3 : i&38)"
                    Range(timecell).Offset(3).Formula = "=SUMIF($A$3:$A$38,Negative,i&3 : i&38)"
                    Range(timecell).Offset(4).Formula = "=SUMIF($A$3:$A$38,Neutral,i&3 : i&38)"
        Next i
        For j = 4 To lColumn Step 5
            Dim answercell As String
            answercell = j & x
                    Range(timecell).Offset(2).Formula = "=COUNTIFS($A$3:$A$38,Positive,D3:D38,'c')/(COUNTIFS($A$3:$A$38,Positive,D3:D38,'c')+COUNTIFS($A$3:$A$38,Positive,D3:D38,'w'))"
                    Range(timecell).Offset(3).Formula = "=COUNTIFS($A$3:$A$38,Negative,D3:D38,'c')/(COUNTIFS($A$3:$A$38,Negative,D3:D38,'c')+COUNTIFS($A$3:$A$38,Negative,D3:D38,'w'))"
                    Range(timecell).Offset(4).Formula = "=COUNTIFS($A$3:$A$38,Neutral,D3:D38,'c')/(COUNTIFS($A$3:$A$38,Neutral,D3:D38,'c')+COUNTIFS($A$3:$A$38,Neutral,D3:D38,'w'))"
        Next j
    
    MsgBox "Done"
    
    End Sub
    But I get this error

    Run-time error '1004':
    Method 'Range' of Object '_Global' failed

  4. #4
    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: Need a macro instead of SUMIF & COUNTIFS formula

    Hi Keyvan

    It's not apparent to me what you wish to SUM nor where you wish to put it but you'll need this kind of Code. Perhaps this'll get you started.
    Sub keyvan()
        Dim i As Long, j As Long, x As Long, lColumn As Long
    
        Range("A3").Select
    
        lColumn = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
        x = ActiveCell.CurrentRegion.Rows.Count
        For i = 2 To lColumn Step 5
            '        Dim timecell As String
            '        timecell = x & "," & i
            '        timecell = i & x
            Cells(x, i).Offset(2).Formula = "=SUMIF($A$3:$A$38,""A""," & Range(Cells(3, i), Cells(38, i)).Address & ")"
            Cells(x, i).Offset(3).Formula = "=SUMIF($A$3:$A$38,""B""," & Range(Cells(3, i), Cells(38, i)).Address & ")"
            Cells(x, i).Offset(4).Formula = "=SUMIF($A$3:$A$38,""C""," & Range(Cells(3, i), Cells(38, i)).Address & ")"
            '                Cells(x, i).Offset(2).Formula = "=SUMIF($A$3:$A$38,Positive,i&3 : i&38)"
            '                Range(timecell).Offset(2).Formula = "=SUMIF($A$3:$A$38,Positive,i&3 : i&38)"
            '                Range(timecell).Offset(3).Formula = "=SUMIF($A$3:$A$38,Negative,i&3 : i&38)"
            '                Range(timecell).Offset(4).Formula = "=SUMIF($A$3:$A$38,Neutral,i&3 : i&38)"
        Next i
    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.

  5. #5
    Registered User
    Join Date
    05-28-2012
    Location
    Tehran, Iran
    MS-Off Ver
    Excel 2007 , 2010
    Posts
    4

    Re: Need a macro instead of SUMIF & COUNTIFS formula

    Hi Jalaske

    Thank you very much, It's exactly what I want

  6. #6
    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: Need a macro instead of SUMIF & COUNTIFS formula

    You're welcome...glad I could help. Thanks for the Rep.

  7. #7
    Registered User
    Join Date
    05-28-2012
    Location
    Tehran, Iran
    MS-Off Ver
    Excel 2007 , 2010
    Posts
    4

    Re: Need a macro instead of SUMIF & COUNTIFS formula

    You're welcome
    just another question
    could you please help me about this formula too ?!

    =COUNTIFS($A$3:$A$38,""Positive"",D3:D38,'c')/(COUNTIFS($A$3:$A$38,Neutral,D3:D38,'c')+COUNTIFS($A$3:$A$38,Neutral,D3:D38,'w'))
    Thank you again

    P.S. absolutely the firs part is enough
    COUNTIFS($A$3:$A$38,""Positive"",D3:D38,'c')
    Edit : I think I solve it

    =COUNTIFS($A$3:$A$38,""Positive""," & Range(Cells(3, j), Cells(38, j)).Address & ",""c"")
    Last edited by Keyvan; 07-29-2013 at 08:30 PM.

  8. #8
    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: Need a macro instead of SUMIF & COUNTIFS formula

    Hi Keyvan

    Good for you
    Edit : I think I solve it
    You're welcome...glad I could help. Thanks for the Rep.

+ 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. Formula needed for countif or countifs (may also need a sumif)
    By Petefav in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2013, 02:18 AM
  2. Sumif, sumifs, counta, countifs??
    By 1.zer0 in forum Excel General
    Replies: 3
    Last Post: 01-14-2012, 06:18 AM
  3. Macro to add range & criteria in COUNTIFS Formula
    By wspettus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2011, 03:34 AM
  4. Replies: 0
    Last Post: 12-16-2011, 09:01 AM
  5. converting COUNTIFS formula to macro
    By batjl9 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-14-2011, 03:37 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