+ Reply to Thread
Results 1 to 9 of 9

VBA countif formula for increment columns

  1. #1
    Registered User
    Join Date
    08-21-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    VBA countif formula for increment columns

    Hello,

    This is my first post and I've searched everywhere for this. However, as I'm a VBA beginner I am struggling to simplify this VBA formula.

    I have 2 sheets.
    1) Sheet 1 is the source data sheet with survey responses for each question in column format (eg A-Z).
    2) Sheet 2 is an array table which counts the survey resources where multiple criterions are matched (ie using COUNTIF formula).

    The formula is generally the same. Two of the criterions are in row 1 of sheet 2. Only the columns change and they progress consecutively (eg from A to B) for each row of the array table. Please refer to the columns in bold font. As there are 75 survey questions, I would prefer not to copy and paste the recorded VBA formula.

    Below is an example of a formula which I would like to loop and repeat.

    Sheet2.Range("C2").FormulaR1C1 = _
    "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C1,'RPT - Rank'!R1C)"
    Sheet2.Range("C2").AutoFill Destination:=Range("C2:G2"), Type:=xlFillDefault

    Sheet2.Range("C3").FormulaR1C1 = _
    "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C2,'RPT - Rank'!R1C)"
    Sheet2.Range("C3").AutoFill Destination:=Range("C3:G3"), Type:=xlFillDefault


    Can anyone help? I need this next week.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA countif formula for increment columns

    Maybe try this?
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-21-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA countif formula for increment columns

    Hi, the FillRight works. For the FillDown, it is close but I need the following column to increment for each row after the first row:

    Eg if this script, 'RPT - Rank'!R1C2, is column B then for the next row, it should reference column C, and for the next column, column D etc.

    Any help would be appreciated!

  4. #4
    Registered User
    Join Date
    08-21-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA countif formula for increment columns

    Oops, the above post is not right.

    The FillRight works. For the Filldown, this is actually what I need:

    Eg if this script, 'Sheet1'!C1, is column B then for the next row, it should reference column C, and for the next column, column D etc.

    Sorry for the confusion. Any help would be appreciated!

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA countif formula for increment columns

    try changing to
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-21-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA countif formula for increment columns

    It doesn't work! When I changed it, it became column D instead of A with no $ and copied column D down all the below rows. What I would like is for the following rows, the column will change from column D to E, F, G etc. Is that clearer?

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA countif formula for increment columns

    Try this. I don't know how to do this without a VBA loop.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-21-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: VBA countif formula for increment columns

    This looks promising but I'm unsure how to use this. I've created and attached an example, would you be able to show me how this VBA loop work?

    I'm having problems uploading an example but the idea as below:

    Sheet 2
    # COL81_DG 1 2 3 4
    1 text 0 0 1 0
    2 text 1 1 0 0
    3 text 2 0 0 0
    4 text 1 0 1 0
    5 text 0 0 2 0


    Sheet 1
    COL1 COL2 COL3 COL4 COL5 etc COL80 COL81
    4 4 2 3 3 ab COL81_DE
    4 3 2 2 2 ab COL81_DE
    4 4 4 4 4 ab COL81_DF
    1 3 2 4 4 ab COL81_DG
    4 1 1 3 3 ab COL81_DE
    2 2 2 4 4 ab COL81_DF
    1 3 1 2 4 ab COL81_DE
    4 3 1 2 3 ab COL81_DE
    4 2 2 2 4 ab COL81_DG

  9. #9
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA countif formula for increment columns

    I can't really visualize what you are actually trying to do as well, or rather it's very difficult. I don't know what you mean by you are unsure how to use this either.
    my question would be, are the formulas inserted into Sheet2.Range("C2:G76") working as you intended?

    as for the 'For' loop used in my previous post, I can try my best to explain how it works but it's best you search online or the Help for more detailed information on its usage.

    in this line, ' For i = 2 To 76 ' it is telling VBA to start the loop with a variable holding the value '2' and loop it till the variable is '76'.
    in the first time it is looped through,
    this line ---------------> Sheet2.Range("C" & i).FormulaR1C1 = "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C" & i - 1 & ",'RPT - Rank'!R1C)"
    would be equivalent to -> Sheet2.Range("C" & 2).FormulaR1C1 = "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C" & 2 - 1 & ",'RPT - Rank'!R1C)"
    and then --------------> Sheet2.Range("C2").FormulaR1C1 = "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C1,'RPT - Rank'!R1C)"

    in the last loop through,
    it would be -> Sheet2.Range("C" & 76).FormulaR1C1 = "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C" & 76 - 1 & ",'RPT - Rank'!R1C)"
    and then ---> Sheet2.Range("C76").FormulaR1C1 = "=COUNTIFS('Sheet1'!C81,""*""&'RPT - Rank'!R1C2&""*"",'Sheet1'!C75,'RPT - Rank'!R1C)"
    Last edited by millz; 08-25-2013 at 11:16 AM.

+ 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. Increment columns in INDEX and MATCH formula - ROWS and COLUMNS
    By nickmangan in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2012, 10:38 AM
  2. Replies: 0
    Last Post: 05-11-2012, 11:51 AM
  3. Increment Certain Number of Columns in Formula
    By BigLa in forum Excel General
    Replies: 6
    Last Post: 12-18-2011, 07:53 PM
  4. Two Columns- Countif formula?
    By meliss421 in forum Excel General
    Replies: 1
    Last Post: 03-31-2011, 08:29 AM
  5. Increment Rows Across Columns in copied SUMIF formula??
    By Chizilla in forum Excel General
    Replies: 3
    Last Post: 02-10-2010, 05:56 PM

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