SumIf Utilizing a Dynamic Column












0















I am attempting to create a template that will pull data from multiple files that I will save down in my documents. The template will pull data and utilize a SumIf function to reference a snack on sheet 1, and sum the numbers next to it on sheets 2 & 3. My assumption is that the snack will always be in column A, but depending on the file I download, the "Quantity" column may differ.



The code below tries to reference Lc as the sum range, but through tinkering with the language, I am getting




Error '1004': Application-defined or object-defined error -or-
Error '13': Type Mismatch




I have thought about a range.address, but I'm unsure of how to go about it. Is it necessary? Any insight would be very appreciated.



Snacks:
Snacks



Dim Lr, Lc As Long

Lr = Cells(Rows.Count, "A").End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
For a = 1 To Lr

Cells(a, 3).Value = Application.WorksheetFunction.SumIf(Worksheets("Sheet 2").Range( _
"A2:A" & Lc), Worksheets("Sheet 1").Cells(a, 1), Worksheets("Sheet 2").Range( _
"A2:A" & Lc))

Next a


--------------------------------Update--------------------------



Thanks for the valuable insight guys.



@QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? Essentially I want it to sum "Cheetos" initially, but next time I would like it to sum "Doritos". You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is.



@Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Also, great catch on the LastRow issue - after working thru this for a while, it's the small things that catch me out.










share|improve this question




















  • 2





    "For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

    – Michal Rosa
    Nov 24 '18 at 2:01






  • 1





    SUMIF second argument should not be an array of values but a single criteria.

    – QHarr
    Nov 24 '18 at 6:57













  • QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

    – VBAINPROGRESS
    Nov 24 '18 at 18:38
















0















I am attempting to create a template that will pull data from multiple files that I will save down in my documents. The template will pull data and utilize a SumIf function to reference a snack on sheet 1, and sum the numbers next to it on sheets 2 & 3. My assumption is that the snack will always be in column A, but depending on the file I download, the "Quantity" column may differ.



The code below tries to reference Lc as the sum range, but through tinkering with the language, I am getting




Error '1004': Application-defined or object-defined error -or-
Error '13': Type Mismatch




I have thought about a range.address, but I'm unsure of how to go about it. Is it necessary? Any insight would be very appreciated.



Snacks:
Snacks



Dim Lr, Lc As Long

Lr = Cells(Rows.Count, "A").End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
For a = 1 To Lr

Cells(a, 3).Value = Application.WorksheetFunction.SumIf(Worksheets("Sheet 2").Range( _
"A2:A" & Lc), Worksheets("Sheet 1").Cells(a, 1), Worksheets("Sheet 2").Range( _
"A2:A" & Lc))

Next a


--------------------------------Update--------------------------



Thanks for the valuable insight guys.



@QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? Essentially I want it to sum "Cheetos" initially, but next time I would like it to sum "Doritos". You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is.



@Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Also, great catch on the LastRow issue - after working thru this for a while, it's the small things that catch me out.










share|improve this question




















  • 2





    "For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

    – Michal Rosa
    Nov 24 '18 at 2:01






  • 1





    SUMIF second argument should not be an array of values but a single criteria.

    – QHarr
    Nov 24 '18 at 6:57













  • QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

    – VBAINPROGRESS
    Nov 24 '18 at 18:38














0












0








0








I am attempting to create a template that will pull data from multiple files that I will save down in my documents. The template will pull data and utilize a SumIf function to reference a snack on sheet 1, and sum the numbers next to it on sheets 2 & 3. My assumption is that the snack will always be in column A, but depending on the file I download, the "Quantity" column may differ.



The code below tries to reference Lc as the sum range, but through tinkering with the language, I am getting




Error '1004': Application-defined or object-defined error -or-
Error '13': Type Mismatch




I have thought about a range.address, but I'm unsure of how to go about it. Is it necessary? Any insight would be very appreciated.



Snacks:
Snacks



Dim Lr, Lc As Long

Lr = Cells(Rows.Count, "A").End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
For a = 1 To Lr

Cells(a, 3).Value = Application.WorksheetFunction.SumIf(Worksheets("Sheet 2").Range( _
"A2:A" & Lc), Worksheets("Sheet 1").Cells(a, 1), Worksheets("Sheet 2").Range( _
"A2:A" & Lc))

Next a


--------------------------------Update--------------------------



Thanks for the valuable insight guys.



@QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? Essentially I want it to sum "Cheetos" initially, but next time I would like it to sum "Doritos". You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is.



@Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Also, great catch on the LastRow issue - after working thru this for a while, it's the small things that catch me out.










share|improve this question
















I am attempting to create a template that will pull data from multiple files that I will save down in my documents. The template will pull data and utilize a SumIf function to reference a snack on sheet 1, and sum the numbers next to it on sheets 2 & 3. My assumption is that the snack will always be in column A, but depending on the file I download, the "Quantity" column may differ.



The code below tries to reference Lc as the sum range, but through tinkering with the language, I am getting




Error '1004': Application-defined or object-defined error -or-
Error '13': Type Mismatch




I have thought about a range.address, but I'm unsure of how to go about it. Is it necessary? Any insight would be very appreciated.



Snacks:
Snacks



Dim Lr, Lc As Long

Lr = Cells(Rows.Count, "A").End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
For a = 1 To Lr

Cells(a, 3).Value = Application.WorksheetFunction.SumIf(Worksheets("Sheet 2").Range( _
"A2:A" & Lc), Worksheets("Sheet 1").Cells(a, 1), Worksheets("Sheet 2").Range( _
"A2:A" & Lc))

Next a


--------------------------------Update--------------------------



Thanks for the valuable insight guys.



@QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? Essentially I want it to sum "Cheetos" initially, but next time I would like it to sum "Doritos". You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is.



@Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Also, great catch on the LastRow issue - after working thru this for a while, it's the small things that catch me out.







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:54







VBAINPROGRESS

















asked Nov 24 '18 at 1:45









VBAINPROGRESSVBAINPROGRESS

11




11








  • 2





    "For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

    – Michal Rosa
    Nov 24 '18 at 2:01






  • 1





    SUMIF second argument should not be an array of values but a single criteria.

    – QHarr
    Nov 24 '18 at 6:57













  • QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

    – VBAINPROGRESS
    Nov 24 '18 at 18:38














  • 2





    "For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

    – Michal Rosa
    Nov 24 '18 at 2:01






  • 1





    SUMIF second argument should not be an array of values but a single criteria.

    – QHarr
    Nov 24 '18 at 6:57













  • QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

    – VBAINPROGRESS
    Nov 24 '18 at 18:38








2




2





"For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

– Michal Rosa
Nov 24 '18 at 2:01





"For a = 1 To LastRow" - where have you defined LastRow? "("A:A" & Lc)" - that should be ("A2:A" & Lc)

– Michal Rosa
Nov 24 '18 at 2:01




1




1





SUMIF second argument should not be an array of values but a single criteria.

– QHarr
Nov 24 '18 at 6:57







SUMIF second argument should not be an array of values but a single criteria.

– QHarr
Nov 24 '18 at 6:57















QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

– VBAINPROGRESS
Nov 24 '18 at 18:38





QHarr I really appreciate the insight. My question is, how to I create a "single criteria" when I want the criteria to change each time it loops? You will see above that I have swapped to cells(a, 1) instead of the Range("A:A") for the criteria. I am getting a value of 0 because I don't think the cells(a, 1) is looping in the same way that the overarching loop is. Michal Rosa Thank you, I am no longer getting the error - it is running without error, but all of the SumIf values are returning as "0". Great catch on the LastRow issue ^

– VBAINPROGRESS
Nov 24 '18 at 18:38












1 Answer
1






active

oldest

votes


















0














It's not a terribly efficient way of doing things, but if you really want to do your calculations this way, your code should look as follows:



Dim l As Long

l = Cells(Rows.Count, "A").End(xlUp).Row

For a = 2 To l

Cells(a, 3).Value = Application.WorksheetFunction. _
SumIf(Worksheets("Sheet 1").Range("A2:A" & l), _
Cells(a, 1).Value, _
Worksheets("Sheet 1").Range("C2:C" & l)) + _
Application.WorksheetFunction. _
SumIf(Worksheets("Sheet 2").Range("A2:A" & l), _
Cells(a, 1).Value, _
Worksheets("Sheet 2").Range("C2:C" & l))

Next a


Assuming your data inputs are in sheets "Sheet 1" and "Sheet 2", in columns A and C.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454497%2fsumif-utilizing-a-dynamic-column%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's not a terribly efficient way of doing things, but if you really want to do your calculations this way, your code should look as follows:



    Dim l As Long

    l = Cells(Rows.Count, "A").End(xlUp).Row

    For a = 2 To l

    Cells(a, 3).Value = Application.WorksheetFunction. _
    SumIf(Worksheets("Sheet 1").Range("A2:A" & l), _
    Cells(a, 1).Value, _
    Worksheets("Sheet 1").Range("C2:C" & l)) + _
    Application.WorksheetFunction. _
    SumIf(Worksheets("Sheet 2").Range("A2:A" & l), _
    Cells(a, 1).Value, _
    Worksheets("Sheet 2").Range("C2:C" & l))

    Next a


    Assuming your data inputs are in sheets "Sheet 1" and "Sheet 2", in columns A and C.






    share|improve this answer






























      0














      It's not a terribly efficient way of doing things, but if you really want to do your calculations this way, your code should look as follows:



      Dim l As Long

      l = Cells(Rows.Count, "A").End(xlUp).Row

      For a = 2 To l

      Cells(a, 3).Value = Application.WorksheetFunction. _
      SumIf(Worksheets("Sheet 1").Range("A2:A" & l), _
      Cells(a, 1).Value, _
      Worksheets("Sheet 1").Range("C2:C" & l)) + _
      Application.WorksheetFunction. _
      SumIf(Worksheets("Sheet 2").Range("A2:A" & l), _
      Cells(a, 1).Value, _
      Worksheets("Sheet 2").Range("C2:C" & l))

      Next a


      Assuming your data inputs are in sheets "Sheet 1" and "Sheet 2", in columns A and C.






      share|improve this answer




























        0












        0








        0







        It's not a terribly efficient way of doing things, but if you really want to do your calculations this way, your code should look as follows:



        Dim l As Long

        l = Cells(Rows.Count, "A").End(xlUp).Row

        For a = 2 To l

        Cells(a, 3).Value = Application.WorksheetFunction. _
        SumIf(Worksheets("Sheet 1").Range("A2:A" & l), _
        Cells(a, 1).Value, _
        Worksheets("Sheet 1").Range("C2:C" & l)) + _
        Application.WorksheetFunction. _
        SumIf(Worksheets("Sheet 2").Range("A2:A" & l), _
        Cells(a, 1).Value, _
        Worksheets("Sheet 2").Range("C2:C" & l))

        Next a


        Assuming your data inputs are in sheets "Sheet 1" and "Sheet 2", in columns A and C.






        share|improve this answer















        It's not a terribly efficient way of doing things, but if you really want to do your calculations this way, your code should look as follows:



        Dim l As Long

        l = Cells(Rows.Count, "A").End(xlUp).Row

        For a = 2 To l

        Cells(a, 3).Value = Application.WorksheetFunction. _
        SumIf(Worksheets("Sheet 1").Range("A2:A" & l), _
        Cells(a, 1).Value, _
        Worksheets("Sheet 1").Range("C2:C" & l)) + _
        Application.WorksheetFunction. _
        SumIf(Worksheets("Sheet 2").Range("A2:A" & l), _
        Cells(a, 1).Value, _
        Worksheets("Sheet 2").Range("C2:C" & l))

        Next a


        Assuming your data inputs are in sheets "Sheet 1" and "Sheet 2", in columns A and C.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 3:49









        Pang

        6,9291664102




        6,9291664102










        answered Nov 25 '18 at 23:27









        Michal RosaMichal Rosa

        1,3191814




        1,3191814
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454497%2fsumif-utilizing-a-dynamic-column%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft