+ Reply to Thread
Results 1 to 6 of 6

VBA SUMIF syntax issues

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    VBA SUMIF syntax issues

    I'm trying to write the following SUMIF statement, but the syntax it screwing me up. I'm doing something wrong...

    In a For...Next statement:

    Cell.Offset(0, 3) = Application.WorksheetFunction.SumIf(AWB.sSh.Range("A8:A" & lastRow), Cell.Value, AWB.sSh.Range("G8:G" & lastRow))
    Here's what I'm trying to do:

    1. use SUMIF via Application.WorksheetFunction.Sumif()

    2. Set the first range to a workbook I have previously defined as "AWB", to a sheet on that workbook that I have previously defined as "sSH", within the range "A8:A" & lastRow), I have previously defined "lastRow"

    3. Set the second range to the current Cell.Value in the For statement

    4. Set the third range again to AWB.sSH.Range("G8:G" & lastRow)

    Logically, I understand what I'm trying to do, but I'm not sure how to make this work in VBA.

  2. #2
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: VBA SUMIF syntax issues

    Hi

    AWB.sSh.Range("A8:A" & lastRow)
    This syntax is wrong. sSh is not a method or a property of AWB.

    If you defined sSh as a worksheet in workbook AWB, use instead:

    cell.Offset(0, 3) = Application.WorksheetFunction.SumIf(sSh.Range("A8:A" & lastRow), cell.Value, sSh.Range("G8:G" & lastRow))
    This should work. If it doesn't please post the initialisation of AWB and sSh.

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: VBA SUMIF syntax issues

    I've previously defined a certain workbook in my sub as AWB, and a sheet in that workbook as sSH.

    I can't use

    .SumIf(sSh.Range("A8:A" & lastRow)
    because that doesn't link back to the AWB workbook at all. The first and third arguments in this link to this other workbook.

    The syntax may be totally wrong, but the variables are defined correctly, because I can do something like this:

    AWB.sSH.Range("A1").Value = "hello"
    and it works just fine.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA SUMIF syntax issues

    How did you define sSH?
    Last edited by Norie; 07-31-2014 at 06:20 PM.
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: VBA SUMIF syntax issues

    Dim sSH   as Worksheet
    Dim AWB  as Workbook
    
    Set AWB = Activeworkbook
    Set sSH = Sheets(1)
    I used the macro recorder to see how Excel did it itself, and then used that to work out the error in my code....

    This is now working:

    Cell.Offset(0, 3) = "=SUMIF('[" & AWB.Name & "]Data'!$A$8:A" & lastRow & ",A2,'[" & AWB.Name & "]Data'!$G$8:G" & lastRow & ")"
    I'd still like (for other applications) to know whether or not using "sSH" instead of "Data!" (the actual sheet name) is possible, but I haven't gotten it to work yet. This statement works however, so for now it's good enough

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA SUMIF syntax issues

    Sorry, you've lost me.

    The code you just posted is totally different to the code in the first post.

    Also, the code you've posted to set a reference, sSH, to a worksheet in the workbook referenced by the variable AWB isn't right.

    This is how you would set sSH to refer to the first worksheet in the active workbook.
    Dim AWB As Workbook
    Dim sSH As Worksheet
    
        Set AWB = Activeworkbook
        Set sSH = AWB.Sheets(1)
    Last edited by Norie; 08-01-2014 at 08:05 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. IF, AND & OR Syntax Issues
    By Steve0492 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2013, 06:22 AM
  2. [SOLVED] Looking for better SUMIF syntax
    By peri1224 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-20-2012, 10:27 AM
  3. =SUMIF syntax
    By Tunturi in forum Excel General
    Replies: 2
    Last Post: 07-04-2012, 07:04 AM
  4. IF/THEN Syntax issues
    By EdinNH in forum Excel General
    Replies: 6
    Last Post: 06-12-2012, 05:21 PM
  5. Syntax issues with the OR function
    By aikorei in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-10-2007, 04:35 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