+ Reply to Thread
Results 1 to 8 of 8

calculate Range (X:y) based on a calculated X value for hiding rows

  1. #1
    Registered User
    Join Date
    03-06-2015
    Location
    Colorado
    MS-Off Ver
    2013
    Posts
    9

    calculate Range (X:y) based on a calculated X value for hiding rows

    I am trying to figure out how to calculate the beginning or end of a range of rows to hide using the VB code.
    I have a spreadsheet cell (A3) that has an input of 1-10.
    Based upon that input, there is a section of rows (A10-A19) with values that should show only the number of rows based on that the input.
    i.e. if A3=1, then only row 10 should show and 11-19 should be hidden. If A3=2, then 10 thru 11 should show and 12-19 should be hidden, etc.
    Can I use the following format? If so, how do I get the ranges to work as a calculation based on A3?

    Sheets("System Information").Range("10:???").EntireRow.Hidden = False
    Sheets("System Information").Range("???:19").EntireRow.Hidden = True

    Would the following work?

    Range ("A3") =X
    Sheets("System Information").Range("10:(10+X)").EntireRow.Hidden = False
    Sheets("System Information").Range("(10+X+1):19").EntireRow.Hidden = True


    Thanks in advance
    Last edited by biggjake; 10-27-2015 at 06:54 PM.

  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
    44,508

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    Maybe:

    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
    03-06-2015
    Location
    Colorado
    MS-Off Ver
    2013
    Posts
    9

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    Thanks.
    I had edited my original post but it doesn't look like it took.


    My edited version:

    I have a spreadsheet cell (A3) that has an input of 1-10.
    Based upon that input, there are 2 sections of rows (10-19) and (25-34) with values that the row should show only the number of rows based on that the input.
    i.e. if A3=1, then only row 10\25 should show and 11-19\26-34 should be hidden. If A3=2, then 10-11\25-26 should show and 12-19\27-34 should be hidden, etc.
    Can I use the following format? If so, how do I get the ranges to work as a calculation based on A3?

    Sheets("System Information").Range("10:???,25:???").EntireRow.Hidden = False
    Sheets("System Information").Range("???:19,???:34").EntireRow.Hidden = True

    Would the following work?

    Range ("A3") =X
    Sheets("System Information").Range("10:(10+X),25:(25+X)").EntireRow.Hidden = False
    Sheets("System Information").Range("(10+X+1):19,(25+x+1):34").EntireRow.Hidden = True
    Last edited by biggjake; 10-27-2015 at 08:02 PM. Reason: remove smilies

  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
    44,508

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    Would the following work?
    no

    Look at the code I have offered. Repeat the bit from With .Range("A10") down to the first End With but change A10 to A25

  5. #5
    Registered User
    Join Date
    03-06-2015
    Location
    Colorado
    MS-Off Ver
    2013
    Posts
    9
    Quote Originally Posted by TMS View Post
    no

    Look at the code I have offered. Repeat the bit from With .Range("A10") down to the first End With but change A10 to A25
    Ok, but note i removed the A before the 10 since i am doing a row. Would yours then be: Range("10")

  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
    44,508

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    Would yours then be: Range("10")
    no

    A range refers to a column and row ... and then you have .EntireRow

    Suggest you just try it. Right click on the tab that needs to run the code, select View Code and copy and paste the code there. Then change A3 and see what happens.

  7. #7
    Registered User
    Join Date
    03-06-2015
    Location
    Colorado
    MS-Off Ver
    2013
    Posts
    9

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    I fully appreciate the help.
    Sorry, I don't do this programming all the time so don't know all of it well.
    It is erroring on the: If Intersect(Target, Range("E11")) Is Nothing Then Exit Sub

    Here is the full code for the section I am working on. The Commented out Case Select is what I am trying to not have to do 10 times. There are several worksheets in the workbook with one that has all of the inputs and where the code is located. Note that e11 is a data validation box with 1 thru 10 listed. I was trying to get an idea on how to do it and then implement it, but that failed it seems.

    Sub MultiDomain()
    Select Case Range("E10").Value
    Case Is = "N"
    Range("e11").EntireRow.Hidden = True
    Range("e11") = 0
    Sheets("System Information").Range("B13:B22") = ""
    Sheets("System Information").Range("122:131,139:148").EntireRow.Hidden = True
    Sheets("DNS").Range("9:48,54:63,65:74,76:85,87:96,100:109,130:139,141:150,152:161,167:176,178:187").EntireRow.Hidden = True
    Sheets("Certificates").Range("7:16,18:27,36:65,72:81,83:92,94:103,105:114,117:126").EntireRow.Hidden = True
    If Range("e29") = 1 Then
    Sheets("System Information").Range("149:149").EntireRow.Hidden = False
    Sheets("DNS").Range("86:86,98:98,162:162").EntireRow.Hidden = False
    Sheets("Certificates").Range("17:17,28:28,70:70").EntireRow.Hidden = False
    Else
    Sheets("System Information").Range("149:149").EntireRow.Hidden = True
    Sheets("DNS").Range("86:86,98:98,162:162").EntireRow.Hidden = True
    Sheets("Certificates").Range("17:17,28:28,70:70").EntireRow.Hidden = True
    End If
    Case Is = "Y"
    AddDomains
    End Select
    End Sub
    '#########Additional SIP Domains############
    Sub AddDomains()

    ' Option Explicit
    '
    'Private Sub AddDomains(ByVal Target As Range)
    ' in worksheet class module for System Information worksheet
    Dim lTV As Long

    If Intersect(Target, Range("E11")) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    If Not IsNumeric(Target.Value) Then Exit Sub
    If Target.Value < 1 Then Exit Sub
    If Target.Value > 10 Then Exit Sub
    lTV = Target.Value

    With Me
    With Sheets("System Information").Range("13:13")
    .Resize(10).EntireRow.Hidden = False
    On Error Resume Next
    .Offset(lTV).EntireRow.Resize(10 - lTV).Hidden = True
    On Error GoTo 0
    End With
    End With
    'Select Case Range("e11").Value
    ' Case Is = 1
    ' Sheets("System Information").Range("B14:B22") = ""
    ' Sheets("System Information").Range("13:13").EntireRow.Hidden = False
    ' Sheets("System Information").Range("14:22").EntireRow.Hidden = True
    ' Sheets("DNS").Range("9:12,54:54,65:65,76:76,100:100,130:130,141:141,167:167,178:178").EntireRow.Hidden = False
    ' Sheets("DNS").Range("13:48,55:63,66:74,77:85,101:109,131:139,142:150,168:176,179:187").EntireRow.Hidden = True
    ' Sheets("Certificates").Range("7:7,36:38,71:72,82:83,104:105,117:117").EntireRow.Hidden = False
    ' Sheets("Certificates").Range("8:16,39:65,73:81,84:92,106:114,118:126").EntireRow.Hidden = True
    ' If Range("e29") = 1 Then
    ' Sheets("System Information").Range("122:122,139:139").EntireRow.Hidden = False
    ' Sheets("System Information").Range("123:131,140:148").EntireRow.Hidden = True
    ' Sheets("DNS").Range("87:87,98:98,152:152,162:162").EntireRow.Hidden = False
    ' Sheets("Certificates").Range("18:18,28:28,70:70,94:94").EntireRow.Hidden = False
    ' End If
    ' If Range("e29") = 2 Then
    ' Sheets("System Information").Range("122:122,139:139").EntireRow.Hidden = False
    ' Sheets("System Information").Range("123:131,140:148").EntireRow.Hidden = True
    ' Sheets("DNS").Range("87:87,152:152").EntireRow.Hidden = False
    ' Sheets("DNS").Range("98:98,162:162").EntireRow.Hidden = True 'STATIC ENTRY
    ' Sheets("Certificates").Range("18:18,94:94").EntireRow.Hidden = False
    ' Sheets("Certificates").Range("28:28,70:70").EntireRow.Hidden = True 'STATIC ENTRY
    ' End If
    ' If Range("e29") = 3 Then
    ' Sheets("System Information").Range("139:139").EntireRow.Hidden = False
    ' Sheets("System Information").Range("122:131,140:148").EntireRow.Hidden = True
    ' Sheets("DNS").Range("87:96,98:98,152:161,162:162").EntireRow.Hidden = True 'STATIC ENTRY
    ' Sheets("Certificates").Range("18:27,28:28,70:70,94:103").EntireRow.Hidden = True 'STATIC ENTRY
    ' End If
    End Sub

    Is the best\easiest I just need to write the one section 10 times?

    thanks.

  8. #8
    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
    44,508

    Re: calculate Range (X:y) based on a calculated X value for hiding rows

    Target is only relevant in an event handler, hence why you get the error. It is not defined.

    See the example workbook attached. I have put the code in place so that it works on the System Information worksheet. The code you have provided is much more complex than the question you have asked and I have not tried to address that. I would not know how to make that into generic code as I don't know what your rules are.

    Regards, TMS


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

+ 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] Frequency based on Calculated Date Range
    By antexity in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-20-2015, 11:26 AM
  2. Calculated column to only calculate populated rows?
    By brianlg in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-28-2013, 09:36 PM
  3. [SOLVED] Auto Hiding rows based on range/data present or not present.
    By raze in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 02-10-2013, 11:27 AM
  4. [SOLVED] need help re hiding rows in a range based on cell contents
    By jimredfield in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-30-2012, 06:53 PM
  5. Hiding a worksheet based on a calculated cell value
    By GuruWannaB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2008, 05:21 PM
  6. Hiding Rows Based based upon cell value being blank
    By leelee23 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2007, 04:48 AM
  7. Hiding Rows in a Range based on column A value
    By tig in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-09-2006, 01:10 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