+ Reply to Thread
Results 1 to 3 of 3

If Statements

  1. #1
    Registered User
    Join Date
    08-13-2013
    Location
    London, Englamd
    MS-Off Ver
    Excel 2007
    Posts
    5

    If Statements

    Hi All,

    I am having trouble ignoring certain rows in loop for a sumif calculation which populates a table of hours worked based on source data.
    The below table is in the worksheet "HrsByDisc" and the data that it using is in the


    A B C D E F G H I J K L
    5 Jan-17 Feb-17 Mar-17 Apr-17 May-17 Jun-17 Jul-17 Aug-17 Sep-17
    6 X Process 1000
    7 Electrical 2000
    8 X Civil 3000
    9 Piping 4000
    10 X Telecoms 5000


    I have written the below code which - if I exclude the code in red - will sumif the discipline across all rows and columns. I have added the code in red because I only want to sumif the rows that havw an X in column A, and ignore all of the others, but I can not get it to work.
    Can anyone tell me where I am going wrong?

    Thanks,

    Kind Regards,
    Simon



    Please Login or Register  to view this content.
    Sub Sumifs()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lr As Long, lc As Long
    Dim rngr As Range, rngc As Range, Cellr As Range, Cellc As Range, rangif As Range
    Set ws1 = ThisWorkbook.Sheets("Export")
    Set ws2 = ThisWorkbook.Sheets("HrsByDisc")
    lr = ws2.Cells(Rows.Count, "C").End(xlUp).Row
    lc = ws2.Cells(5, Columns.Count).End(xlToLeft).Column
    Set rngr = ws2.Range("c6:C" & lr)
    Set rngc = ws2.Range(Cells(5, "D"), Cells(5, lc))
    Set rngif = ws2.Range("a6:A")

    For Each Cellr In rngr
    For Each Cellc In rngc

    If rngif = "X" Then

    Cells(Cellr.Row, Cellc.Column) = Application.WorksheetFunction.Sumifs(ws1.Range("T:T"), ws1.Range("D:D"), Cellr.Value, ws1.Range("P:P"), Cellc.Value, ws1.Range("C:C"), ws2.Range("Actual"))

    Else

    Next Cellc
    Next Cellr

    End Sub
    Please Login or Register  to view this content.
    [/CODE]
    Last edited by SimonJF; 10-22-2017 at 03:48 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,485

    Re: If Statements

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    AA CodeTags.jpg
    Last edited by davesexcel; 10-22-2017 at 11:20 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: If Statements

    If you attach your workbook with some data I'll take a look. I doubt you need a nested For loop to go through every cell if you're only testing the cells in column A.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

+ 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. [SOLVED] Nested if statements containing multiple and statements
    By John M. in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-22-2014, 11:48 PM
  2. Reading If statements and formulating values from if statements
    By crnam in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-16-2013, 05:20 AM
  3. questions with if statements and nested if statements
    By Pat Excel in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 02-03-2013, 01:41 PM
  4. Multiple nested IF statements and AND statements
    By TonyGetz in forum Excel General
    Replies: 2
    Last Post: 12-14-2010, 03:07 AM
  5. Nested If statements / logic statements
    By Brainless_09 in forum Excel General
    Replies: 3
    Last Post: 06-17-2009, 02:59 PM
  6. Replies: 12
    Last Post: 05-15-2009, 08:38 AM
  7. [SOLVED] operator statements, shorting when reusing one of the statements?
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2005, 02:05 PM

Tags for this Thread

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