+ Reply to Thread
Results 1 to 7 of 7

How can I repeat a code with different range and make it work?

  1. #1
    Registered User
    Join Date
    03-06-2018
    Location
    MX
    MS-Off Ver
    2010
    Posts
    17

    Question How can I repeat a code with different range and make it work?

    Hello,

    I am tryingo to do a code so when I double click a cell (A5) a checkmark appears and rows 6 to 8 get hidden. When double click again, checkmark disappears and rows 6 to 8 disappear.

    I got the code (I don't remember where I got it from) working with a few changes. However, I want to repeat the code for other cells. For example.


    I want people choose to double click cell A5 to hide rows 6 to 8, and/or double click cell D5 to hide rows 9 to 11 and/or double click cell F5 to hide rows 12 to 14, etc.

    I get the first one perfectly done! But when I try to copy paste the sub and change values, it just ignores it and does nothing.


    Is there a way to do it?

    Here is the code:
    Note: the "a" is the checkmark with font Marlett, and the "r" is an X in font Marlett. When you double click it changes from X to checkmark.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: How can I repeat a code with different range and make it work?

    Try this:

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: How can I repeat a code with different range and make it work?

    This will do D5 as well, just add another ElseIf for F5
    Please Login or Register  to view this content.

  4. #4
    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: How can I repeat a code with different range and make it work?

    Hi,

    You say you want to repeat for other cells but only give three examples. Is it only A5 D5 and F5 or are you looking for something more general?

    In which case is there any sort of relationship/rule that could be applied. e.g. can a function be applied to the column number in order to generate a row number?
    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.

  5. #5
    Registered User
    Join Date
    03-06-2018
    Location
    MX
    MS-Off Ver
    2010
    Posts
    17

    Re: How can I repeat a code with different range and make it work?

    Thanks all for the replies,

    I gave only three examples, but in realiy, I am writing a report with 7 categories, which will have many sub categories. These subcategories are the cells I want to hide because they will not apply to every report.

    So, for the sake of example, imagine that I am listing categories like fruits, animals, cars, etc.

    There will be a list for each category, so fruits will be like:

    Fruits:
    A5 D5 F5 I5 J5
    1 2 3 4 5

    1) Apple
    2) Banana
    3) Orange
    4) Grape
    5) Watermelon

    If they double click in cell A5, the row for Apple will be hidden, if they double click it again, it will show again. If Apple, Banana, and Grape don't apply to the report, they will double click A5, D5 and F5 to hide their corresponding rows. It will be like that for every category. Also, If they click the category. In my actual report, it has numbers and subtotals and all to make graphs, etc.

    Hope I got to explain myself better, and thanks again for all your help. I appreciate it a lot!

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: How can I repeat a code with different range and make it work?

    How about
    Please Login or Register  to view this content.
    As you haven't said what rows to hide for I5 & J5 I've guessed.
    You can expand this by adding for cells to the range in blue & the choose in red.

  7. #7
    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: How can I repeat a code with different range and make it work?

    Behind my question was do all the sub categories contain the same number of items. If they do then a there's a simple correlation beween the column being double clicked and which rows to hide.

    But aren't you missing a trick? Why not use the Data Grouping functionality which is designed for this sort of stuff. You don't need macros - or even formulae.

+ 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. Replies: 1
    Last Post: 08-14-2013, 12:01 PM
  2. how make vba code work over range of cells
    By sspreyer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2013, 11:42 PM
  3. Replies: 3
    Last Post: 03-26-2013, 06:06 PM
  4. Need help editing my code to make it repeat the process for every cell.
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-10-2012, 04:16 AM
  5. how to make code work
    By Oeysbrei in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 06:22 AM
  6. VBA code to make a macro repeat as long as there is data in the column
    By dkassin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2012, 06:42 PM
  7. *Add-In Help* Need help to make code into Add-In - List work
    By torontoguy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2011, 03:25 AM

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