+ Reply to Thread
Results 1 to 6 of 6

Create A Looped If Structure

Hybrid View

nevi Create A Looped If Structure 02-27-2011, 01:57 PM
davesexcel Re: Create A Looped If... 02-27-2011, 02:23 PM
nevi Re: Create A Looped If... 02-27-2011, 02:37 PM
nevi Re: Create A Looped If... 02-27-2011, 02:39 PM
davesexcel Re: Create A Looped If... 02-27-2011, 03:23 PM
nevi Re: Create A Looped If... 02-27-2011, 03:40 PM
  1. #1
    Forum Contributor
    Join Date
    04-06-2006
    Posts
    131

    Talking Create A Looped If Structure

    Hey There!

    I think what I am trying to do here is fairly simple, but for the life of me I can not remember how to write a Loop statement. I'm pretty basic when it comes to VB and Macros and am trying to teach myself as I go...

    In a nut shell, I need a macro that will follow this logic:
    * Add the values in K22 and M22 together
    * If this value is 0, hide the row
    * If it is not 0, move on to the next row

    If needs to loop through this logic, checking every row from 22 to 1021, hiding all the rows where the value is 0 and leaving the rows that are not 0 displayed.

    I came up with this:
    If Range("K22").Value = "0" Or Range("M22").Value = "0" Then
            Rows("22").Select
            Selection.EntireRow.Hidden = True
    End If
    If Range("K23").Value = "0" Or Range("M23").Value = 0 Then
            Rows("23").Select
            Selection.EntireRow.Hidden = True
    End If
    If Range("K24").Value = "0" Or Range("M24").Value = 0 Then
            Rows("24").Select
            Selection.EntireRow.Hidden = True
    End If
    If Range("K25").Value = "0" Or Range("M25").Value = 0 Then
    ...
    so on and so forth...

    I don't particularily want to type this out 1,000 times though, and I seem to remeber that a Loop structure could do this for me, but I just don't recall how to do it.

    Thanx in adance for your help!!
    Last edited by nevi; 02-27-2011 at 03:19 PM.

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

    Re: Create A Looped If Structure

    Try this
    Sub Button1_Click()
    'If Range("K22").Value = "0" Or Range("M22").Value = "0" Then
    '        Rows("22").Select
    '        Selection.EntireRow.Hidden = True
    'End If
    'If Range("K23").Value = "0" Or Range("M23").Value = 0 Then
    '        Rows("23").Select
    '        Selection.EntireRow.Hidden = True
    'End If
    'If Range("K24").Value = "0" Or Range("M24").Value = 0 Then
    '        Rows("24").Select
    '        Selection.EntireRow.Hidden = True
    'End If
    
    
        Dim LstRws As Long, Rng As Range, c As Range
    
        LstRws = Cells(Rows.Count, "K").End(xlUp).Row
    
        Set Rng = Range(Cells(22, 11), Cells(LstRws, 11))
    
        Rng.EntireRow.Hidden = False
    
        For Each c In Rng.Cells
    
            If c + c.Offset(0, 1) = 0 Then c.EntireRow.Hidden = True
    
        Next c
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-06-2006
    Posts
    131

    Re: Create A Looped If Structure

    Worked like a charm, except that it left the last two rows for some reason. Also, does it take into account if the value in K is 0 but there is a value in M? I don;t see any references to M there, so I am worried that it does not...

    Thank!!

    I have another thing I'd like to do with this book (then I promise I will stop perstering ). I have no idea if this is even possible...

    Basically, the form this is creating will eventually be printed out, so I need to make it visually appealing as well. I will set up a seperate Command Button to print with (rather than using the normal CTRL-P), but it there a Macro that can tell it to count how many rows there are in (K22:K1021)+(M22:M1021), then start a new page for ever 35 rows, with rows 1 though 21 repeated at the top of each page?

  4. #4
    Forum Contributor
    Join Date
    04-06-2006
    Posts
    131

    Re: Create A Looped If Structure

    Figured out the whol K and M thing. Used column O to add them up, then just replaced all the K's in the script you gave me with an O. Works great - thanx a million!!

    :D

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

    Re: Create A Looped If Structure

    The code adds the two together, if they equal 0 then the row is hidden.

    You could even use the autofilter to hide the rows that have zeros.

  6. #6
    Forum Contributor
    Join Date
    04-06-2006
    Posts
    131

    Re: Create A Looped If Structure

    Not that I am an expert myself, but the people that will be using this form are some of the most computer illeterate I have ever encoutered... Asking them to use the Filters would be too much...

    I even had to put a radio button in here that with one click resaves the file with the current date and time, because getting to click Save As and change the file name was too complicated... They keep overwritting the historical data...

    LMAO

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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