+ Reply to Thread
Results 1 to 15 of 15

Using Number of Rows in a Column to Make a Range for a Different Column

  1. #1
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Question Using Number of Rows in a Column to Make a Range for a Different Column

    Hi all,

    I am very new to VBA after spending the vast majority of my time in Excel writing formula's instead. (And I'm by no means amazing at that)
    However something I can do via formula, seems to be very hard for me to achieve in VBA.

    Basically I would like a macro that finds the last cell of a column, in my attached example workbook that's Row 26 in Column D, then uses that number 26 to make a new range. Eg E2:E26. (Where D, E, 2 and 26 are all variable)
    I have be advised by a friend of mine to make sure all variables are kept at the top of the code so they can easily be changed if needs be rather than trawling through code.
    Equally they advised me to make sure I refer to the workbook and sheet, so the macro can be used in other places and with consequences while running. (Similarly I avoid select and activate where possible)

    I have attached a sample workbook and included a basic example of what I'm after. (And one of my latest failings)

    Finally it's worth mentioning that I have tried this with loop and although it works, it's not practical to use in my original file which has over 1000+ rows so it would be a lot of volatile random numbers.
    I more hope to make a range based on the number of rows in a different column and then later refer to that new range to manipulate with copy etc.

    Cheers in advance
    EricDonk

    Edit: I have tried to find an answer to this using all sorts, but everything I saw online doesn't actually do what I'm after.
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Does this work for you?

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by Mumps1 View Post
    Try:
    Please Login or Register  to view this content.
    Thank you for the quick reply Mumps1. That's very nearly what I'm after except it counts the rows in Column E instead of column D so when the macro is run I only get 1 result.
    If possible I want the range to be based on the amount of rows in column D (as a variable) and input into Column E.

    Thanks for the code though. EricDonk
    Last edited by EricDonk; 01-25-2024 at 01:11 PM.

  5. #5
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by ByteMarks View Post
    Does this work for you?

    Please Login or Register  to view this content.
    Hi ByteMarks,

    Cheers for your code it does as half as intended but unfortunately relies on the sheet being active which it isn't.
    Is there a way to make this code work when run from another sheet/ workbook ?
    As I side note I won't need to clear the column because I will run over it each time and there will never be just a header row. (Appreciate the added error handling though)

    Thanks.
    EricDonk

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Try:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by Mumps1 View Post
    Try:
    Please Login or Register  to view this content.
    Thank you Mumps1, when your code is run from another sheet it overrides the header in E1? (I think because the lrow is not referencing the workbook or sheet)
    It does as intended when used on the same sheet but that won't always be the case.

    I might be able to play around from here but if you beat me to the solution I would be most grateful.

    Cheers for the help so far.
    EricDonk
    Last edited by EricDonk; 01-25-2024 at 01:15 PM.

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Try:
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    relies on the sheet being active which it isn't
    How is the sheet/workbook identified?
    Usually if something is the result of a button click which this appears to be, the button is on the sheet being processed and is therefore the active one.


    As I side note I won't need to clear the column because I will run over it each time and there will never be just a header row.
    If column D is shorter than it was before, part of column E won't be run over.

  10. #10
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by Mumps1 View Post
    Try:
    Please Login or Register  to view this content.
    This works really well, thank you so much for all the codes as well as your time Mumps1. This issue had really been getting on my nerves but you solved it so quickly. Cheers again.

    One last question and I'm fine if the answer is "why would you need that" or "It's not worth it" but at the moment I can amend the values "E" and "D" and start at row "2" to suit my needs but out of curiosity is it easy to make them variable too? (like I was attempting in my code... Eg using Dim or something?

    Thanks once more Mumps1, Rep added and marked as solved.
    EricDonk

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Would a prompt to enter the desired row number and column letter work for you?

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    See if this works for you. When prompted for the 'Header' column letter, that is the column that contains the numbers from 1 to 25. The macro assumes that those numbers will start in row 2 regardless what number you enter at the prompt. Let me know if both those numbers and the random numbers always start in the same row.
    Please Login or Register  to view this content.
    Last edited by Mumps1; 01-25-2024 at 01:50 PM.

  13. #13
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by ByteMarks View Post
    How is the sheet/workbook identified?
    Usually if something is the result of a button click which this appears to be, the button is on the sheet being processed and is therefore the active one.
    Cheers for getting back to me ByteMarks, I'm so new to VBA and am only going off what a friend said because they advised me tend to always mention the workbook and sheet, them put them as a variable so they can easily be changed in future.
    Also while reading up on VBA I saw online that using select and active should be avoided where possible, but again am new to this that I trust your experience and reputation.

    Quote Originally Posted by ByteMarks View Post
    If column D is shorter than it was before, part of column E won't be run over.
    This is a great observation and not one I'd thought of, however the list in the column D will never get shorter only stay the same or get longer.

    Thanks again for your code and input. EricDonk

  14. #14
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2302
    Posts
    36

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Quote Originally Posted by Mumps1 View Post
    Would a prompt to enter the desired row number and column letter work for you?
    Quote Originally Posted by Mumps1 View Post
    See if this works for you.
    Please Login or Register  to view this content.
    Sorry about the delay in getting back to you and thanks once more for the new code.
    Sadly I don't currently need the input Row/ Column although it's not a complete waste because I will keep it saved in case I need input options in the future, as it's not something I've look into or even played around with yet.

    That said I guess I could manipulate your code to take out the input option but still have a letter and number choice as a variable that I pick in the code and then just change them when I need to.
    So as a back story I'm basically I'm looking to future proof my code in case the columns move in future (even though I'm new to VBA this has already happened to me a lot!) and when i started I would use the range like 10 times in 1 code and have to go through and change it all. (That's what I'm trying to avoid here)

    You have been a great help so far. EricDonk

    Quote Originally Posted by Mumps1 View Post
    When prompted for the 'Header' column letter, that is the column that contains the numbers from 1 to 25. The macro assumes that those numbers will start in row 2 regardless what number you enter at the prompt. Let me know if both those numbers and the random numbers always start in the same row.
    Edit: And to actually answer you question if the row did ever move away from starting at 2, both the 'Header' Column and 'Random Number' Column, then they would move together. Eg. Both be 3 or both be 4 etc.
    Last edited by EricDonk; 01-25-2024 at 03:20 PM.

  15. #15
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Using Number of Rows in a Column to Make a Range for a Different Column

    Glad to help.

+ 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] Concatenate Text in Column B Split in Multiple Rows for Each Number in Column A
    By zaska in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2020, 03:51 AM
  2. [SOLVED] Loop rows and make a Range null if criteria met in defined Column
    By KEDENNIS in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2016, 09:55 AM
  3. [SOLVED] Copy rows/range and paste n number of times based on last used cell in Column A
    By skonduru in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2016, 02:30 PM
  4. Replies: 7
    Last Post: 08-09-2015, 05:07 PM
  5. [SOLVED] Delete rows if column A matches number and meets condition in another column
    By stokiejames09 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2014, 05:03 PM
  6. [SOLVED] count the number of times each string appears in a column and make a summary column
    By intothewild in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2012, 08:02 AM
  7. How to make a formulae repeat every x number of rows in the same column
    By speko191 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2011, 02:25 AM

Tags for this Thread

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