+ Reply to Thread
Results 1 to 4 of 4

SUMIFS based on variable entered through InputBox

Hybrid View

  1. #1
    Registered User
    Join Date
    11-14-2014
    Location
    UK
    MS-Off Ver
    2010 32bit
    Posts
    18

    SUMIFS based on variable entered through InputBox

    Hi, I wonder if someone may be able to help me get out of a hole I have dug for myself

    Here is some sample data:
    Capture.PNG

    I need to do two different kinds of calculations on a different sheet. The first is just summing the amount per school on a specific week (variable LastWeek entered by a user through an InputBox at the beginning of the routine):
    Range("I29,I30,I31,I32,I33,I34,I35,I37,I38,I39,I40,I41").FormulaR1C1 = "=SUMIFS('ALL DATA'!C23,'ALL DATA'!C1,Report!RC[-8],'ALL DATA'!C25,""13-14 Wk " & LastWeek & """)"
    (where the first criteria C23 is the amount, the second criteria C1 is the school name and the third criteria C25 is the week number)

    That works fine.

    But the second, I need to sum all weeks up to the current week (year to date), based on the week that the user enters (it is not necessarily THIS week). So in the image above, if the user entered that it is week 3, I would like to sum the amount for weeks 1, 2 and 3, for each individual school. Is the user entered 7, then it would sum all weeks up to week 7.

    I really don't know where to start. Should I create some kind of variable array on a different sheet that is built after the user enters the week number, and then try to reference the data against that? I think I have reached my limits in VBA!

    Sorry if my explanation is not great, it's hard to explain, but any help or pointers would be most gratefully appreciated!

    Many thanks in advance for any help you may be able to offer.
    Richard

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: SUMIFS based on variable entered through InputBox

    codes on 2 buttons in file

    Sub till_week()
    wknr = InputBox("Till Weeknumber ?")
    If wknr = "" Then Exit Sub
    lr = Range("B" & Rows.Count).End(xlUp).Row
    Range("B2", "B" & lr).Copy Range("F2")
    Range("F2:F" & lr).RemoveDuplicates Columns:=1, Header:=xlNo
    lr2 = Range("F" & Rows.Count).End(xlUp).Row
    Range("F1").Value = "Total Till Week " & wknr
    For i = 2 To lr2
        For x = 2 To lr
            If Val(Right(Range("A" & x), 2)) <= wknr And Range("B" & x) = Range("F" & i) Then
                tot = tot + Range("C" & x).Value
            End If
        Next x
        Range("G" & i) = tot
    Next i
    End Sub
    Sub week()
    wknr = InputBox("Weeknumber ?")
    If wknr = "" Then Exit Sub
    lr = Range("B" & Rows.Count).End(xlUp).Row
    Range("B2", "B" & lr).Copy Range("I2")
    Range("I2:I" & lr).RemoveDuplicates Columns:=1, Header:=xlNo
    lr2 = Range("I" & Rows.Count).End(xlUp).Row
    Range("I1").Value = "Total From Week " & wknr
    For i = 2 To lr2
        For x = 2 To lr
            If Val(Right(Range("A" & x), 2)) = wknr And Range("B" & x) = Range("I" & i) Then
                tot = tot + Range("C" & x).Value
            End If
        Next x
        Range("J" & i) = tot
    Next i
    End Sub
    Kind regards
    Leo
    Attached Files Attached Files

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: SUMIFS based on variable entered through InputBox

    Alternatively, you could extract the weeknumber values in a separate data column and use the SUMIFS criteria on the numeric values in that column.

    Put this in D2 and copy down to return the numeric week numbers.
    =RIGHT(A2,2)*1

    Then use something like this as the column D sumif criteria where intLastWeek is an integer
    ""<=" & intLastWeek & """
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    11-14-2014
    Location
    UK
    MS-Off Ver
    2010 32bit
    Posts
    18

    Re: SUMIFS based on variable entered through InputBox

    Thank you Antwerp and Florida! Thanks so much to both of you... both solutions work well, but AlphaFrog, yours fits the bill nicely for my needs (now why didn't I think of that . Easy when you know how.

    This forum is excellent, thanks so much for your time and expertise. Thank you from Chichester, UK!

+ 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] sumifs with variable based on range
    By The Phil in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-19-2015, 02:33 AM
  2. Autonumber Based on a Variable from an InputBox
    By Blondie0317 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-09-2013, 05:18 PM
  3. Asterix when password entered into InputBox
    By adste89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2012, 10:59 AM
  4. [SOLVED] Copy sheet based on Inputbox - data to be copied variable.
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 09-07-2012, 10:40 AM
  5. Detect if InputBox password already entered in same instance
    By RedWing in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-17-2011, 01:54 PM
  6. [SOLVED] Error handling on InputBox entry when nothing is entered
    By Kungfauxn00b in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2011, 04:45 AM
  7. [SOLVED] Make date entered in an InputBox UK format
    By Co-op Bank in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-10-2006, 11:20 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