+ Reply to Thread
Results 1 to 19 of 19

VB Range Code

  1. #1
    Registered User
    Join Date
    07-16-2018
    Location
    Wisconsin
    MS-Off Ver
    365
    Posts
    5

    VB Range Code

    Hello, I need some help. I never went to school for excel, more of a DIY person. Sorry in advance for lack of knowledge.

    I have a range formula that worked, but once i added another range it, it only picks up the second range now and not first range. I need it to filter out both columns. Example: if cell A4 has value but Q4 is blank, Row is hidden and vise versa.
    Thanks

    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Range Code

    Can you explain what the code is supposed to do?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: VB Range Code

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: VB Range Code

    Your problem (as far as I understand) is that you are redefining your r variable from the A column to the Q column. You either need to define it as a combined range, or set another variable for the q column and loop through both ranges. You can't define r as column A and then define it as column Q, otherwise it will only search Q.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VB Range Code

    Maybe this:

    Please Login or Register  to view this content.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VB Range Code

    Hi,

    You have only dimmed one r range. All you're doing is first setting it to A4:Q62 and then immediately resetting it to Q4:Q62. Doing it your way you'd need to Dim another range - say r2

    However you should avoid loops wherever possible since they are inefficient and slow where large ranges are involved. The fastst and most elegant way of doing stuff like copying, deleting, hiding..etc certain rows is to apply an Autofilter and then use a single line of code like:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I'd be inclined to add a helper column to identify the rows to hide. e.g.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and filter for the word "Hide" on the helper column.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VB Range Code

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    07-16-2018
    Location
    Wisconsin
    MS-Off Ver
    365
    Posts
    5

    Re: VB Range Code

    Its a schedule/calendar for work. Column A are employees, if the name is not in column a the row is hidden. Column Q represents the work weeks, some months have 5-6 weeks on the calendar month (Dec 2018 takes 6 rows in the calendar), depending if the month starts on a Saturday or Sunday, so it eliminates the excess weeks or add the extra week in.

    yah its not needed, but i enjoy making excel codes, its fun.

  9. #9
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: VB Range Code

    Norie, please correct me if I'm wrong - would that code not only hide if the entire row was blank?

    This is not meant as a slight - I am learning consistently and I try to understand everything I read in other's suggestions.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VB Range Code

    No, it only checks the values in 2 cells - the values from column A and column Q of the current row.

  11. #11
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: VB Range Code

    I believe the OP wants to hide the rows if Cell in Column A or Cell in Column Q is blank.

    if cell A4 has value but Q4 is blank, Row is hidden and vise versa.

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VB Range Code

    Quote Originally Posted by davesexcel View Post
    I believe the OP wants to hide the rows if Cell in Column A or Cell in Column Q is blank.
    I suspect you're right Dave, That's what I had assumed although I must admit the and vice-versa stumped me.

    Does that mean that the row is hidden
    if A4 has value and Q4 is blank, or A4 is blank and Q4 has value

    or the row is hidden
    if A4 has value and Q4 is blank but NOT if A4 is blank and Q4 has value


  13. #13
    Registered User
    Join Date
    06-21-2018
    Location
    Calgary, AB
    MS-Off Ver
    2013
    Posts
    94

    Re: VB Range Code

    Sorry Norie I should have clarified - your code only hides the row if the cells in both A and Q are blank?

  14. #14
    Registered User
    Join Date
    07-16-2018
    Location
    Wisconsin
    MS-Off Ver
    365
    Posts
    5

    Re: VB Range Code

    Thanks for the help, I haven't got it to work yet, most likely newbie error lol.
    So far with the new codes it still doesn't hide all the correct rows.


    The ideas is
    Visible Rows are - Cells in Column A & Q with values
    Hidden Rows are - Cells in Column A and/or Q with no value, get hidden.





    I did have this code working, but its completely different from the first. But i'd prefer the the one posted above.
    The issue was, It only refreshed when the changes were made on the sheet itself, but i wanted it to be able to refresh when changes were made on another sheet, the main sheet where everything is inputted.
    That's why i was using the code above, it was able to refresh when changes were made from my main page sheet. Plus i was trying to have less clutter with codes and it to be more seamless.

    week 1 is row 4-12
    week 6 is row 53-62

    I been battling this for a while now, many different attempts. But ill take any option that makes it work. thanks again for the help.

    Please Login or Register  to view this content.

  15. #15
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: VB Range Code

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.


    To add a file to a post

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VB Range Code

    Try this.
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    07-16-2018
    Location
    Wisconsin
    MS-Off Ver
    365
    Posts
    5

    Re: VB Range Code

    Here is my workbook, remember i have no schooling on this, so no laughing.
    Attached Files Attached Files

  18. #18
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VB Range Code

    As suggested in my post #6 put the following formula in AI4 and copy down to AI52
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Now your sheet activate event is

    Please Login or Register  to view this content.
    see attached
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    07-16-2018
    Location
    Wisconsin
    MS-Off Ver
    365
    Posts
    5

    Re: VB Range Code

    Thanks you for the help, it works perfect. I understand what your previous post meant now.

    Much appreciated.

+ 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. edit in code for populate range of cell in list box and code for delete any items
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2017, 03:33 PM
  2. [SOLVED] Code to open each WB and run code, by range value then save and close the wb
    By bezbid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2016, 11:05 AM
  3. Replies: 3
    Last Post: 01-06-2016, 10:04 AM
  4. [SOLVED] My code is not dynamic; it has a pre-defined range. Possible to change code?
    By mick0005 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-20-2015, 10:23 PM
  5. Re-code Userform to open range of sheets like previous sheet code
    By JJFletcher in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-15-2014, 02:31 AM
  6. Protect/Unprotect code causing out of range error in code
    By Petrolcb41 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2010, 02:34 PM
  7. Optimizing the Vlookup code in VB. Repitive code with different referencing range.
    By raknahs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2010, 05:03 PM

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