Using Last Column Variable name instead of Column reference

  • Hello All,
    This query continues on from a previous post: http://www.ozgrid.com/forum/showthread.php?t=204475 which has been answered perfectly.


    The position of the last column and the number of columns that are totalled may change.
    Sumifs Range: Cells(i,11), Cells (i,[COLOR="#FF0000"]20[/COLOR]) - i would like to replace 20 with the last column
    Paste Range: Range("U" & i & ":Y" & i) - this would be the last column + 1 to totrange (or last column +5 cols)
    I have tried posting the variable name into the code (which was suggested elsewhere) but get an error on both lines.



    Many thanks for your help.

  • Re: Using Last Column Variable name instead of Column reference


    Try this

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Hello KjBox,


    Thank you for the fabulously swift reply. I have run the code but it stops on the & after lrow with a Type Mismatch Error.

    Code
    Cells(i, j) = WorksheetFunction.SumIfs("Range(Cells(i, 11), Cells(i, " & lrow & ")), Range(""K10:T10""), Cells(10, j)")


    I have the paste working by switching the row and col ref to Cells(i, icol +1)


    Many thanks
    Jan

  • Re: Using Last Column Variable name instead of Column reference


    I have had a look at your original thread. You have made numerous structural changes to your worksheet. Can you please attach the new workbook.


    Are you trying to make the code dynamic to account for any number of "Stages"?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Hi KjBox,


    The original post was an example of what I was trying to achieve. But for my work file I need to make it more dynamic. I have only very slightly modified the code to update some column and cell values as there were a few more columns before the data columns and a few more rows in the header.
    Yes I am trying to make the code more dynamic to account for additional stages which are added through a checkbox. I can update the original file to match the actual file if required. Thanks for your help. It is awesome.


    Cheers Jan

  • Re: Using Last Column Variable name instead of Column reference


    One change to the code that you have made, which I do not understand, is that you now start at Row 11 not Row 4.


    An updated file that matches the actual file would help.


    I am looking at doing an alternative method which would make the code completely dynamic.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Thanks for the file.

    Quote

    Columns A-J are mostly text and static data


    Do you mean Columns A to I? Your data that needs totalling starts in Column J on the sample workbook. Or should data for totalling start in Column K?


    What about Rows 1 to 10, do they contain text too?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Hello KjBox,


    My apologies. I should have shifted 1 more column to the right. Actual data starts summing from column K. Rows 1 to 10 are headers and logo.
    To upload actual file, I would need to desensitise the data which might take a little bit of time, but I am happy to do this if required. Pls let me know if that would be better.


    Cheers
    Jan

  • Re: Using Last Column Variable name instead of Column reference


    All should be OK with the sample file as it is (modified for data starting in Column K). If not then I will let you know.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Try the attached.


    I have made K10:O10 a new defined range (named "Headers"), this is used by the code to add the headers to the Totals columns in appropriate rows.


    I also added a button to the Totals Columns header, click it to populate the Totals Coumns. The code first clears anything in the Totals columns so you can update/edit the data then click the button again, no need to manually clear any Totals data.


    Code assigned to the button is:

  • Re: Using Last Column Variable name instead of Column reference


    To answer the question you asked in your original post regarding IF statements.


    If the result of an IF statement is included in the same line of code as the IF statement criteria then "End If" is not needed.


    So

    Code
    If i > 0 Then
        s = "Abc"
    End If


    can be written as

    Code
    If i > 0 Then s = "Abc"


    Also,

    Code
    If i > 0 Then
        s = "Abc"
    Else
        s = "Xyz"
    End If


    can be written as

    Code
    If i > 0 Then s = "Abc" Else s = "Xyz"


    Alternatively the last example could be written as

    Code
    s = IIf(i > 0, "Abc", "Xyz")


    The IIF Function is a special VBA Function which is like the IF worksheet function, they both have 3 elements: Criteria, Value if True, Value if False

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Using Last Column Variable name instead of Column reference


    Good Afternoon KjBox,
    Thank you so much for the script. It works perfectly. It may take me a little time to understand the code as I am quite new to VBA but it's all part of the learning journey. This site is a fabulous resource and I would have been struggling for ages without your support.
    Once again, thank you for your time and patience.
    Regards
    Jan :thumbup:
    Thanks also for the IF statement explanation.

  • Re: Using Last Column Variable name instead of Column reference


    You're welcome.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!