+ Reply to Thread
Results 1 to 11 of 11

VBA Code to Use Column Dim in Excel Formula

  1. #1
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    26

    VBA Code to Use Column Dim in Excel Formula

    Good Afternoon,

    I am working on a macro that will take a large amount of data, pivot it by source code. What I am wanting to build is a formula that will use my below endColumn dim in the formula as the number of columns in this report could be variable as we clean up source codes.

    The below is the dim and the formula I am trying to use it in. I use offset to get to the cell I need to, that works fine. I want to amend the formula to accept the endColumn dim so the formulas will be variable.

    When I run this code I get a Application defined or object-defined error.

    Any help would be much appreciated!


    Dim endRow As Long
    Dim rangeOne As Range, rangeTwo As Range
    Dim endColumn As Long

    endRow = Sheets("CombinedPivot").Cells(Rows.Count, 1).End(xlUp).Row
    endColumn = Sheets("CombinedPivot").Range("A1").End(xlToRight).Column



    Selection.End(xlToRight).Offset(0, 1).Value = "Max of Source Code 1"
    Range("A1").Select
    Selection.End(xlToRight).Offset(1, -3).Value = "=IFERROR(LARGE(B2,&endColumn&,1),""N/A"")"

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,488

    Re: VBA Code to Use Column Dim in Excel Formula

    Try:
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    26

    Re: VBA Code to Use Column Dim in Excel Formula

    Good Morning and thank you for your response.

    Apologies on the delay of my response, was tied up yesterday and getting back to this project now.

    I made the change and I get the same application/object defined error.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,488

    Re: VBA Code to Use Column Dim in Excel Formula

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

  5. #5
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    26

    Re: VBA Code to Use Column Dim in Excel Formula

    Sample Data.xlsx

    I have attached sample data, and the formula's I am using. This report will gain/lose source codes over time, which is why I want to set the formula to be variable, dependent on how wide the spreadsheet is.

    I hope this helps to get this figured out. Below I included the code I am using, the bolded line is the line I updated with your suggestion.

    Appreciate the help, and will do better to provide more information moving forward.

    Thank you.



    Sub FINALIZE_SOURCE_DATA_BY_PART()
    Dim endRow As Long
    Dim rangeOne As Range, rangeTwo As Range
    Dim endColumn As Long

    endRow = Sheets("CombinedPivot").Cells(Rows.Count, 1).End(xlUp).Row
    endColumn = Sheets("CombinedPivot").Range("A1").End(xlToRight).Column


    Selection.End(xlToRight).Offset(0, 1).Value = "Count of Source Code 1"
    Selection.End(xlToRight).Offset(0, 1).Value = "Source Code 1"
    Selection.End(xlToRight).Offset(0, 1).Value = "Count of Source Code 2"
    Selection.End(xlToRight).Offset(0, 1).Value = "Source Code 2"
    Range("A1").Select
    Selection.End(xlToRight).Offset(1, -3).Value = "=IFERROR(LARGE(B2," & endColumn & ",1),""N/A"")"
    Selection.End(xlToRight).Offset(1, -2).Value = "=XLOOKUP(JC2,B2:JA2,$B$1:$JA$1,""N/A"")"
    Selection.End(xlToRight).Offset(1, -1).Value = "=IFERROR(LARGE(B2:JA2,2),""N/A"")"
    Selection.End(xlToRight).Offset(1, 0).Value = "=XLOOKUP(JE2,B2:JA2,$B$1:$JA$1,""N/A"")"

    End Sub

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,488

    Re: VBA Code to Use Column Dim in Excel Formula

    Try this:

    Please Login or Register  to view this content.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,347

    Re: VBA Code to Use Column Dim in Excel Formula

    To be on the safe side.
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    This
    Please Login or Register  to view this content.
    can also be written as
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    26

    Re: VBA Code to Use Column Dim in Excel Formula

    Thank you for the help TMS, it worked great!

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,488

    Re: VBA Code to Use Column Dim in Excel Formula

    You're welcome. Thanks for the rep.

    bakerman2 suggested this:
    Please Login or Register  to view this content.
    That is also what I would normally use and it is more reliable if there might be gaps in the data in row 1. If there are no gaps, the result is the same.

    The second suggestion is a nice touch.


    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon (Next to Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,347

    Re: VBA Code to Use Column Dim in Excel Formula

    You're welcome and thanks for Rep+.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,488

    Re: VBA Code to Use Column Dim in Excel Formula

    Thanks for the rep.

+ 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. Need Formula from I2 column for Each Federation Each Code
    By maddyrafi1987 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-20-2023, 08:25 AM
  2. writing vba code to fill in column of sheet, formula works vba code will not
    By pdx2245 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2019, 05:15 PM
  3. Need a VBA formula or code to extract a name from a column onto a new one.
    By obimon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-15-2018, 08:07 PM
  4. vba macro code for not show column formula in excel
    By rana19 in forum Excel General
    Replies: 5
    Last Post: 10-06-2015, 08:42 AM
  5. Replies: 1
    Last Post: 11-13-2013, 10:28 AM
  6. [SOLVED] Change my code into Excel formula code
    By DGARRETT1 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-04-2013, 07:51 PM
  7. Replies: 16
    Last Post: 06-25-2011, 05:14 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